كيف احصل على اخر id في الجدول بدون استخدام insert من قاعدة البيانات ؟

السلام عليكم

لدي استفسار عن طريقة الحصول على اخر id في الجدول بدون استخدام insert من قاعدة البيانات

كنت استعمل هذه الطريقة لكنها تضيف بيانات جديدة

$link = mysqli_connect("localhost", $username, $password, $username);

if ($link === false) {
die("ERROR: Could not connect. ".mysqli_connect_error());
}


$sql = "INSERT INTO movies (img_title, img_link, imgid) VALUES ('xx', 'xxx', 'xxxx')";

if (mysqli_query($link, $sql)) {

$last_id = mysqli_insert_id($link);
echo "Records inserted successfully. Last inserted ID is: ".$last_id;
} else {
echo "ERROR: Could not able to execute $sql. ".mysqli_error($link);
}

mysqli_close($link);

شكرا

يرجى الدخول لحسابك أو تسجيل حساب لتستطيع إضافة تعليق
حساب جديد دخول

التعليقات

Alwalid أضف ردا

$link = mysqli_connect("localhost", $username, $password, $username);

if ($link === false) {

die("ERROR: Could not connect. ".mysqli_connect_error());

}

$sql = "SELECT id FROM movies ORDER BY id DESC LIMIT 1";

$result = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {

$last_id = mysqli_fetch_assoc($result);

echo "the last ID in table is : " . $last_id["id"];

}

mysqli_close($link);

احسنت شكرا لك على المساعدة

استعمل SELECT