Page 1 of 1

Query returns "Resource Identifier #6" instead of

Posted: Wed May 14, 2003 5:37 pm
by Moonspell
I have a script where I insert data for a customer into a table. The table has a primary key which automatically updates (INT NOT NULL AUTO_INCREMENT). I want to insert a row into the table, and then find out what ID I just inserted.

Here's the first query which inserts data into the ClientSales table.

$secondquery = "INSERT INTO ClientSales (ClientNameID, SalesFName, SalesLName) values ('$theclientid', '$FName','$LName')";
mysql_query($secondquery);

This table has the primary, unique key. The data goes in perfectly, no errors or problems. Now, since I just entered data, I want to find out what number MySQL automatically generated. So, I run this script

$findtheid = "SELECT ClientSalesID FROM ClientSales WHERE SalesFName = '$FName' AND SalesLName = '$LName'";
$findtheidresult = mysql_query($findtheid);

$findtheidresult returns Resource Identifier #6 instead of the data I'm tryin got call. Where is my error?

Posted: Wed May 14, 2003 10:04 pm
by Jade
Quick question...

is this by any chance in a while loop? You might want to try turning you error message on. I get this message all the time, and its usually when i have a script in a while loop where i've named two varibles the exact same thing by accident.

Jade

Posted: Thu May 15, 2003 12:43 am
by volka
mysql_query() returns a resource identifier not the values themself. You have to use mysql_fetch_... functions to retrieve the values, e.g. mysql_fetch_array() (there are examples on that page)

Re: Query returns "Resource Identifier #6" instead

Posted: Thu May 15, 2003 2:50 am
by twigletmac
Moonspell wrote:This table has the primary, unique key. The data goes in perfectly, no errors or problems. Now, since I just entered data, I want to find out what number MySQL automatically generated.
For this you don't need a second query, you can use mysql_insert_id().

Mac

Posted: Thu May 15, 2003 1:51 pm
by Moonspell
volka wrote:mysql_query() returns a resource identifier not the values themself. You have to use mysql_fetch_... functions to retrieve the values, e.g. mysql_fetch_array() (there are examples on that page)
so:

$result = mysql_query($query);
$arrayresult = mysql_fetch_array($result);

Will that return the data?

Posted: Thu May 15, 2003 2:10 pm
by Jim
Generally putting that array into a loop and setting variables equal to table data is what I do.

$query = mysql_query();

while ($info = mysql_fetch_array($query)) {

$id = $info['id'];
$otherstuff = $info['cat'];

}


There's a good tutorial for this at http://www.phpcomplete.com, I know.