使用 mysqli 的方法是:
$mysqli->insert_id 是返回刚刚插入的那条记录的 id
SELECT LAST_INSERT_ID() 是返回最后插入的记录id
你可以用 对象,new mysql
<?php$servername = "localhost"
$username = "username"
$password = "password"
$dbname = "myDB"
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname)
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error)
}
$sql = "SELECT id, firstname, lastname FROM MyGuests"
$result = $conn->query($sql)
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"
}
} else {
echo "0 结果"
}
$conn->close()
?> <?php
$servername = "localhost"
$username = "username"
$password = "password"
$dbname = "myDB"
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname)
// Check connection
if (!$conn) {
die("连接失败: " . mysqli_connect_error())
}
$sql = "SELECT id, firstname, lastname FROM MyGuests"
$result = mysqli_query($conn, $sql)
if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"
}
} else {
echo "0 结果"
}
mysqli_close($conn)
?>
还有,mysql和mysqli使用上很大区别,比如:
mysqli_query($conn, $sql)和mysql_query($sql)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)