selecting the unique id for mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
voiay002
Forum Newbie
Posts: 4
Joined: Wed Apr 20, 2005 7:28 pm
Location: South Australia

selecting the unique id for mysql

Post by voiay002 »

Hi

Can anyone see a syntax prob here?


Code: Select all

$fname = $HTTP_POST_VARS["fname"];

  $sname = $HTTP_POST_VARS["sname"];

    echo $fname;
    echo $sname;

  $db = mysql_connect("localhost", "root");

  mysql_select_db("tester",$db);

  $personid = mysql_query("select personid 
           from people where 
           $fname = fname and $sname = sname",$db);
fname and sname exist but it doesnt assign the personid from the table to
$personid.

I have switched fname = $fname and sname = $sname as well, used '' and "" around the variables. It doesnt want to get the id.

this is the file after submission from a form. AS i want to get the id created in the dB of the entry i just made to use. Is there another way to get the id?

thanks


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Last edited by voiay002 on Wed Apr 20, 2005 10:25 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. you have an error in you SQL. echoing mysql_error() will give you a hint.
    I'll give you one too: you need to quote something. (and sanitize it)
  2. once the query is correct, $personid will be a resource identifier to the database's result set. You will need to use a fetch function such as mysql_fetch_assoc() to get the record found.
voiay002
Forum Newbie
Posts: 4
Joined: Wed Apr 20, 2005 7:28 pm
Location: South Australia

thanks

Post by voiay002 »

Yeah the error is
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
So personid contains nothing.

Thanks

Could you also tell me what sanitize means?
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

no... that DOESN'T mean that personid contains nothing... it means that there is something wrong in your query statement.


therefore, he meant for you to try this man :

Code: Select all

$personid = mysql_query("select personid from people where $fname = fname and $sname = sname",$db) or die(MySQL_Error());
notice the Or Die(MySQ_Error()) after your query...

lemme give you a hint : your first major mistake is after where.

your second major mistake is after and
voiay002
Forum Newbie
Posts: 4
Joined: Wed Apr 20, 2005 7:28 pm
Location: South Australia

Post by voiay002 »

Thanks again

I have removed the syntax error (I think), all it returns is 'Resource id #3' for the echo of $personid no matter how many entries in the table and nothing for $id.
I think that 'Resource id #3' refers to the column number which in this case is fname.

Code: Select all

$personid = mysql_query("select * from people where 'fname' = '$fname' and 'sname' = '$sname'",$db)or die(MySQL_Error());

    echo $personid ;

  $myrow = mysql_fetch_array($personid);

  $id = $myrow['personid'];

    echo $id;
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

$personId is correct and has nothing to do with your rows.
$persondId contains a refrence to the results of your query and not the actual data.

What does $id contain when you echo it????
voiay002
Forum Newbie
Posts: 4
Joined: Wed Apr 20, 2005 7:28 pm
Location: South Australia

Post by voiay002 »

Thankyou for your reply.

$id echos nothing
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

single quotes in mysql, are string markers (typically). The query you posted previously has mysql compare the string 'fname' to '$fname', not the field fname to the string $fname.

` vs. '
Post Reply