我正在計算AGE DATE字段中的DATE,然後我想將它推入基於DOB的正確年齡的AGE。所以,當我調試的出生日期計算年齡的作品,但它不能更新AGE代碼:試圖在一個SQL後更新SQL
<?php
$servername = "localhost";
$username = "usernameexmaple";
$password = "passworking";
$dbname = "dbnameworking";
// Create connection
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id as ID, YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5)) as age
FROM regio_users";
$sql2 = ("UPDATE regio_users SET age = '$newage' WHERE id ='$newid' ");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$newage = $row['age'];
$newid = $row['ID'];
$sql2 = ("UPDATE regio_users SET age = '$newage' WHERE id ='$newid' ");
$result2 = $conn->query($sql);
if ($result2){
echo "done"."<br>";
}
}
}
else {
echo "0 results";
}
$conn->close();
?>
它回聲做了每一個ID,但不是在所有更新任何東西。
'$ result2 = $ conn-> query($ sql2);'使用'$ sql2'運行更新查詢 – Saty
非常感謝,在我發佈之前,請再次閱讀我的腳本。對不起,浪費時間,並非常感謝你爲我趕上它。 –