我很高兴成为剪切和粘贴用户。
我尝试做的是为数据库用户创建一个基本的管理员查找。
我没有收到DB的错误消息,但没有显示结果。
我尝试做的是使用表单发布方法从管理员根据用户ID查找用户信息的表单获取User_ID的DB信息。
这是我的php
<?php
$con=mysqli_connect("dbhost","username","password","dbase");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT 'User_ID', 'Username', 'Email', 'Group_ID' * FROM table
WHERE User_ID= $User_ID ");
echo $row['User_ID'] . " " . $row['Username'] . " " . $row['Email']. " " . $row['Group_ID'];
echo "<br>";
?>
非常感谢您的帮助
杰夫
答案 0 :(得分:1)
此查询有很多错误:
$result = mysqli_query($con,"SELECT 'User_ID', 'Username', 'Email', 'Group_ID' * FROM table
WHERE User_ID= $User_ID ");
1)未定义的变量$User_ID
2)您在查询中使用*和列名称
3)使用列的引号。相反,你可以使用backtits。
将您的查询更改为:
$result = mysqli_query($con,"SELECT `User_ID`, `Username`, `Email`, `Group_ID` FROM table
WHERE User_ID= $User_ID ");
答案 1 :(得分:0)
$User_ID
变量?正确代码:
<?php
$con=mysqli_connect("dbhost","username","password","dbase");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT User_ID, Username, Email, Group_ID FROM table
WHERE User_ID= '$User_ID'");
while($row=mysqli_fetch_array($result)){
echo $row['User_ID'] . " " . $row['Username'] . " " . $row['Email']. " " . $row['Group_ID'];
echo "<br>";
} /* END OF WHILE LOOP */
?>
您也可以使用预备语句来阻止SQL injections。如果您在预备声明:
中使用它,它会是什么样子<?php
$con=mysqli_connect("dbhost","username","password","dbase");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ($stmt = $con->prepare("SELECT User_ID, Username, Email, Group_ID FROM table WHERE User_ID=?")) {
$stmt->bind_param("s", $User_ID); /* LETS ASSUME THAT THE SUBMITTED DATA IS STORED INTO $User_ID VARIABLE */
$stmt->execute();
$stmt->bind_result($userid,$username,$email,$groupid);
while ($stmt->fetch()) {
printf ("%i %s %s %i<br>", $userid,$username,$email,$groupid); /* YOU CAN REPLACE THE NECESSARY STRING FORMAT IF YOU NEED TO */
} /* END OF WHILE LOOP */
$stmt->close();
}
$con->close();
?>