My code
$query = "select * from others";
But it takes previous id . when I insert the first row , it takes the id value will be 0 , then again insert the second row, it takes the id value will be 1.
Please Use my simple code it will be helpful for you
$selectquery="SELECT id FROM tableName ORDER BY id DESC LIMIT 1";
$result = $mysqli->query($selectquery);
$row = $result->fetch_assoc();
echo $row['id'];
Related
In this code, after insert values to DB.I am doing select query for selecting invoiceNo($sql1= "select invoiceNo from invoices order by invoiceID desc limit 1"; ).Instead of selecting from DB how to get InvoiceNo?
For eg: Assume two users are there.Two users inserts InvoiceID at a same time.While doing "select invoiceNo from invoices order by invoiceID desc limit 1";this will get last coming invoiceID .I need to get specific invoiceID (for particular user) .How to get it?
$query = "select * from invoices order by invoiceID desc limit 1";
$result = $link->query($query);
$row = $result->fetch_assoc();
$invoiceNo = $row["invoiceNo"];
$getinvoiceNo = str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT); //inserting like 0000
$sql = "INSERT INTO invoices (invoiceNo)
VALUES ('$getinvoiceNo')";
if ($link->query($sql) === TRUE) {
//echo "1";
$sql1 = "select invoiceNo from invoices order by invoiceID desc limit 1";
$last_id = mysqli_insert_id($link, $sql1);
$result1 = mysqli_query($link, $sql1);
$row1 = mysqli_fetch_array($result1);
echo json_encode($row1);
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysql_close($link);
If i understand your question correctly, you are concerned about possible data corruption from the concurrent update of the record.
I think you should give a look to mysql SELECT ... FOR UPDATE syntax, it should do what you ask: lock the selected row until an update is fired. Then the lock will be released.
For example:
SELECT table_field FROM table_name WHERE table_id_field = id_param FOR UPDATE
will lock the selected row until
UPDATE table_name SET table_field = table_field + 1 WHERE table_id_field = id_param
If you're looking to prevent collisions in invoice numbers, all you need to do is create your table as
CREATE TABLE invoices (
invoiceID INTEGER NOT NULL AUTO_INCREMENT,
other columns . . .
PRIMARY KEY (invoiceID)
);
Then when you do your INSERT, don't insert the invoiceID and let MySQL do it.
This will ensure that each new invoice has a unique invoiceID.
Hi i have a php form and part of this is to get the last "shiftID" row entry in my table and put this into a variable so i can later add 1 to said variable. However the result of the following code returns the information linked below. How do i get the last "shiftID" number by itself into a variable.
<?php
session_start();
include 'dbh.php';
$start = $_GET['starttime'];
$finish = $_GET['finishtime'];
$dat = $_GET['date'];
$id = $_GET['userid'];
$shiftidd = $conn->query("SELECT shiftID FROM shift_user ORDER BY shiftID DESC LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0]);
//$result = $conn->query("INSERT INTO shift (shiftStart, shiftFinish, shiftDate)
//VALUES ('$start', '$finish', '$dat')");
//$sql = $conn->query("INSERT INTO shift_user (shiftID, userID) VALUES ('$shiftidd', '$id')");
//header("Location: shifts.php");
?>
Web page result:
"connected 41"
I'm looking for the number "4" but I'm guessing the "1" is the affected row along with the result? but how do i get rid of the "1"?
Thanks in advance.
1 is a result of print_r() which is superfluous here. Use either echo or print_r, but not both
You can do it like. After you insert query executed use the following function
mysql_query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
$id = mysql_insert_id();//will return you the last id inserted into table//
But if you want to find the last id of some table then you have to use the follwoing code:
$shiftidd = $conn->query("SELECT MAX(shiftID) FROM shift_user LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0])///////it will be the last id of the table////////
if you want to find next id please try this
$shiftidd = $conn->query("SELECT MAX(shiftID)+1 FROM shift_user LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0])///////it will be the next id of the table///////////
This is what i tried and it works fine..
$sql = "SELECT * FROM patient where id = (SELECT max(id) FROM patient)";
result = mysql_query($sql);
if (mysql_num_rows($result) > 0)
{
// output data of each row
while($row = mysql_fetch_assoc($result))
{
$patientid=$row["id"];
$patientname=$row["name"];
$patientrefer=$row["referto"];
$patientdisease=$row["disease"];
}
}
else
{
echo "0 results";
}
but whenever i replaced the query with
$sql = "SELECT * FROM patient where id = LAST_INSERT_ID()";
It always return 0 results.
In order to get last (latest) record from your table, you can do descending ORDER BY together with LIMIT:
SELECT * FROM patient ORDER BY id DESC LIMIT 0,1
You don't need LAST_INSERT_ID in that case at all. Moreover with concurrent inserts you cannot ensure that user's last insert is really the latest one by using LAST_INSERT_ID.
I want to get ids in my table then update
id row is auto increment
if (isset($_POST['reply'])) {
$reply = $_POST['reply'];
$answers = $db->query("SELECT * FROM table_name");
while($answers_ = $answers->fetch_object()){
if($get_answer = $db->query("UPDATE table_name SET answer ='$reply' WHERE question_id = '$answers_->question_id' LIMIT 1")){
echo "done";
}
}
}
I know my code is wrong its update all rows with the same value i want to update only one row
use mysqli_insert_id() — Returns the auto generated id used in the last queryn
$get_answer = $db->query("UPDATE table_name SET answer ='$reply' WHERE question_id = '$answers_->question_id' LIMIT 1")
$last_id = mysql_insert_id();
I am trying to get the last value of a field during a new registration.
before insert data into the table, I want to create a user id number according to the last registered user's id number. to do that I use this:
//to reach the last value of userID field;
$sql = "SELECT userID FROM loto_users ORDER BY userID DESC LIMIT 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$value = $row['userID'];
echo "$value"; //not resulting here
}
$userID = $value+1;
so, the userID becomes 1.
The weird thing is, I could capable to use exact same code in another php file and works fine.
I would like to say that, rest of the code works fine. No problem with db connections or any other things you can tell me.
Note that: When I run the same query line in the mysql interface, I can get the value I want. I mean $sql line.
Your problem is in this code:
{
$svalue = $row['userID'];
----^
echo "$value"; //not resulting here
}
$userID = $value+1;
Change to $value.
But the right answer is to define userID to be auto-incrementing. That way, the database does the work for you. After inserting the row, you can do:
SELECT LAST_INSERT_ID()
To get the last value.
I solved the problem. Here;
$sql = "SELECT userID FROM loto_users ORDER BY userID DESC LIMIT 1";
$result = mysql_query($sql);
$user_info = $result->fetch_assoc();
$value = intval($user_info["userID"]);
$userID = $value+1;
Thanks everyone.
If you mark the userID field as autoincrement in you mysql table.
You won't need to set the userID and db increase the userID for you. You can get the assigned userID using the mysql_insert_id() function. Here is an example from php.net
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
Here is another example for your case
mysql_query("INSERT INTO 'loto_users'('username',...) values('usernameValue',...)");
echo "New User id is ".mysql_insert_id();