PHP:为什么在使用';而';显示结果?
当我使用“while”显示结果时,为什么查询中的第一条记录不显示 例如,PHP:为什么在使用';而';显示结果?,php,mysql,Php,Mysql,当我使用“while”显示结果时,为什么查询中的第一条记录不显示 例如,user_id 1登录,联系人id5和6显示在我的回音末尾,带有: echo "contact is " . $row["contact_id"]; 这是我的密码: <?php require('dbConnect.php'); //I have a contacts table like this : //contact_auto_inc user_id contact_id /
user_id 1
登录,联系人id5和6显示在我的回音末尾,带有:
echo "contact is " . $row["contact_id"];
这是我的密码:
<?php
require('dbConnect.php');
//I have a contacts table like this :
//contact_auto_inc user_id contact_id
// 1 1 3
// 2 1 5
// 3 2 1
// 4 3 5
// 5 3 2
// 6 1 6
//The user logs in and their user_id is displayed at the top of the page with code like the following :
//use the variables we created earlier
session_start();
$user_id = $_SESSION['user_id'];
echo "user id is " . $user_id . "<br>";
//So if user_id 1 logs in, we see at the top of the page "user id is 1"
//I want to display the list of contacts for the logged in user_id
$select_from_contacts_table = "SELECT * FROM contacts WHERE user_id = '$user_id'";
//get the result of the above
$result=mysqli_query($con,$select_from_contacts_table);
//get every other record in the same row as that particular user_id
$row = mysqli_fetch_assoc($result);
// get the matching contact_id in each row
//$contact_id = $row["contact_id"];
while($row = mysqli_fetch_assoc($result)) {
//$contact_id=$rows['contact_id'];
echo "contact is " . $row["contact_id"] . "<br>";
}
?>
因为这一行
$row = mysqli_fetch_assoc($result);
您没有显示这一行。因为这里有这一行
$row = mysqli_fetch_assoc($result);
您没有显示此行。每次调用都是指针的减量。删除第一个呼叫:
$row = mysqli_fetch_assoc($result);
在此之后,您再次在
循环中调用同一行。每次调用都是指针的减量。删除第一个呼叫:
$row = mysqli_fetch_assoc($result);
在此之后,您将再次调用中的同一行,而循环。您正在获取(并覆盖…)循环前的第一行。警告:使用mysqli
时,您应该使用和将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将$\u POST
或$\u GET
数据直接放入查询中,如果有人试图利用您的错误,这可能非常有害。您正在获取(并覆盖…)循环前的第一行。警告:使用mysqli
时,您应该使用并将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将$\u POST
或$\u GET
数据直接放入查询,如果有人试图利用您的错误,这可能非常有害。