简体   繁体   中英

how to get the auto-increment primary key of a table

I have a small MySQL database of sellers where there is a table, in which each seller has an auto-increment primary key (and there is not any other unique key attribute). Now I want to retrieve each seller data from the php script

// Nothing wrong with this code
    $stmt = $this->conn->prepare("SELECT * FROM seller WHERE id= ? LIMIT 1");
    $stmt->bind_param("i", $ud);
    $stmt->execute();
    $result = $stmt->get_result();

But.. to retrieve data with this query, I need to have the primary key ( ie autoincrement id of the seller ). How would I know what is the primary key of the record inserted?

You would get the ID from your own data. For example, let's say you have a page where you list your Widget records. The database query on that page might be something like:

SELECT id,name FROM widgets

Then you'd display the output in a loop. Within this output, you might have a link such as:

echo '<a href="edit.php?id=' . $row['id'] . '">Edit</a>";

Which would output something like:

<a href="edit.php?id=123">Edit</a>

When clicking that link, your browser would then go to edit.php with the query string data id=123 . So on that page you could get that query string value:

$_GET['id']

That value would be the ID of the specific record you want to display. Use that as the parameter in your query:

SELECT * FROM widgets WHERE id=?

Or, in your specific code, that's the value you'd pass to your function:

getUserData($_GET['id'])

However you design it, the point is that you get the IDs from your data. You don't need to intuitively know what they are.

  1. As you are trying to retrieve seller data from MySQL server based on an auto_increment id attribute. I assume that initially you are unaware of any details inside the table beforehand.

    This can be handled using PHP by, initially retrieving all id's in the table, along with a partially identifying attribute like seller name and thereafter, fetching all details of the required seller using the selected id.

     if(!$con) die(); function fetchId(){ $result = mysqli_query($con,"SELECT id, name FROM seller"); if($result && mysqli_num_rows($result)>0){ while($row = mysqli_fetch_assoc($result)){ echo "ID: ".$row["id"]." NAME: ".$row["name"]; retrieveData($row["id"]); } } } function retrieveData($id){ $result = mysqli_query($con,"SELECT * FROM seller WHERE id=".mysqli_real_escape_string($con,$id)); if($result && mysqli_num_rows($result)==1){ $row = mysqli_fetch_assoc($result) print_r($row); } } mysqli_close($con); 
  2. Using auto_increment for an attribute like primary key is not a wrong approach when needing to retrieve data, but some partial rows containing the key are to be pre-fetched before the desired row can be individually accessed using it.

insert_id attribute of the prepared statement saved me. Now just after my insert statement I can get the last inserted primary key of the table-

$stmt->execute();
$sid = $stmt->insert_id;

This is equivalent to the LAST_INSERT_ID() function in mysql. Make sure you use this in the same session, just after insertion of the table row. Otherwise, it returns a null set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM