尝试从 php 读取我的 mysql databse 中的用户状态



尝试从 php 读取我的 mysql 数据库中用户的状态

$conn = mysqli_connect($servername, $username, $password, $dbname);
// Find status for row with Username of the Url?username=
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]."'";
$result = mysql_query($sql) or die('Error connecting to database');
$username = mysql_result($result, 0, "Status");
echo 'Username Status is ' . $Status;
mysqli_close($conn);

?>

结果我得到的是这样的:

Fatal error: Uncaught Error: Call to undefined function mysql_query() in /storage/ssd4/269/2113269/public_html/teststat.php:11 Stack trace: #0 {main} thrown in /storage/ssd4/269/2113269/public_html/teststat.php on line 11

你的 mysqli语法不正确,因为mysqli 与 mysql语法不同。

MySQLi 代表 MySQL 改进。它是一个面向对象的接口 MySQL绑定使事情更易于使用。

我建议你使用 mysqli 版本,不要将 mysql 语法与mysqli语法混淆。

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row or remove while loop if you wish
while($row = $result->fetch_assoc()) {
echo 'Username Status is ' . $row['Status'] ;
}
} else {
echo "0 results";
}
$conn->close();

你混淆了mysql和mysqli

$result = mysql_query($sql) or die('Error connecting to database');
$username = mysql_result($result, 0, "Status");

相关内容

  • 没有找到相关文章

最新更新