PDA

View Full Version : [SOLVED] PHP- SQL Problem



s.fox
December 23rd, 2008, 02:04 PM
Hi,

I feel like I am going insane. What on earth is wrong with the script I posted below? The script dies because of the SQL I am trying to run. The database login credentials are correct, as is the SQL itself.



$email = "example@example.com";

$host="localhost";

$username="username";

$password="pass";

$db_name="databaseName";

$connection = mysql_connect("$host", "$username", "$password")or die("cannot connect");
$query="SELECT userEmail,userID,userPassword,userUserName FROM user WHERE user.userEmail =
'$email'";

$st=mysql_query($query, $connection) or die("died because of $query");

$recs=mysql_num_rows($st) or die("died because of recs");

$row=mysql_fetch_object($st) or die ("died because of row");

Really stuck and very confused. Can anyone spot what the problem is?

Many thanks, Ash R

mike_g
December 23rd, 2008, 02:10 PM
You will need to select the databse you want to work with after connecting. See mysql_select_db (http://uk2.php.net/mysql_select_db)
Used like:

mysql_select_db($db_name);
Also, you can gett better debug output by using mysql_error() when you query. IE:

$st=mysql_query($query, $connection) or die(mysql_error());

s.fox
December 23rd, 2008, 02:51 PM
doh! I should have spotted that one. Now the script works how I want :)

Thanks for the tip about the mysql_error. I usually don't use it but I think i may start to with more regularity. Incidentally I finally managed to get full database access so I was able to create all my tables without to much hassle.

Cheers Mike for all the help you have given me over the last few days.

mike_g
December 23rd, 2008, 02:55 PM
No worries, have fun with it :)