Reading mySQL works then won't

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
lparnau
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 3:31 pm

Reading mySQL works then won't

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
lparnau
Forum Newbie
Posts: 15
Joined: Tue Aug 12, 2003 3:31 pm

Corrected Code

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply