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?
Query returns "Resource Identifier #6" instead of
Moderator: General Moderators
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)
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Re: Query returns "Resource Identifier #6" instead
For this you don't need a second query, you can use mysql_insert_id().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.
Mac
so: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)
$result = mysql_query($query);
$arrayresult = mysql_fetch_array($result);
Will that return the data?
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.
$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.