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

Trying to read the status of a user in my mysql databse from php

正在尝试从 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

你混淆了 mysql 和 mysqli

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

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

MySQLi stands for MySQL improved. It's an object-oriented interface to the MySQL bindings which makes things easier to use.

我建议你使用 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();