Page 1 of 1

Reading mySQL works then won't

Posted: Sun Sep 04, 2005 4:30 am
by lparnau
A little help please :oops:

When I use this peice of code to query my db it works fine:

Code: Select all

$sql = "SELECT * FROM twisted WHERE LKey=$id";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);     // NO ERROR LINE 26

    $id = $myrow["LKey"];

    $First = $myrow["Fname"];

But if I change the WHERE from the LKey which is the primary to Username which is a unique field I keep getting this error:

Code: Select all

$sql = "SELECT * FROM twisted WHERE Username=$id";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);     // ERROR HERE LINE 26

    $id = $myrow["LKey"];

    $First = $myrow["Fname"];

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /xxxxxx/local/home/xxxxxx/xxxxxxxx.com/rs/admin222.php on line 26

Can any one tell me why I can not use the Username field or any other field I would need to use.

This is driving me nuts

Thank You

Len

Posted: Sun Sep 04, 2005 4:37 am
by s.dot
Well in your first query, you have WHERE LKey = $id

That returns results, so obviously LKey is equal to $id.

However, when you try to find usernames that are equal to $id, it can't find any.

You should print your $id variable to the browser, and you will see that it's not equal to any usernames in your database.

$id is most likely a number in your database, whereas the username would most likely be a name

so you should have a variable that stores the person's username as well, then you'd be able to query for it.

Such as $username = "jimbob";

then search for WHERE username = $username

if that makes sense :P

Corrected Code

Posted: Sun Sep 04, 2005 5:18 am
by lparnau
That is what I did I left the form var. the same name "id" and I only changed the one field in the WHERE statement.

CORRECTED CODE:

Code: Select all

$sql = "SELECT * FROM twisted WHERE Username=$username";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);  // ERROR HERE 
    $id = $myrow["LKey"];

    $First = $myrow["Fname"];
Same error and yes everything is being passed right (form field is correct) contents are the same. I just dont know.

Posted: Sun Sep 04, 2005 8:38 am
by feyd
Username is a string. A database will require that a string is in quotes to help it figure out how to handle it.


Moved to databases.