不断查询失败

时间:2018-08-18 14:50:42

标签: php mysqli

请注意,我不太了解PHP。这是一个学校项目,他们让我上了另外一堂课,而不是我原本想学的php。

这就是说,我正在尝试运行以下查询,但查询一直失败,但我不知道为什么。我一直在逐步学习教程。数据库正在连接,但查询仍然失败。

<!doctype html>
<?php
$servername = "localhost";
$username = "krystalb_krystal";
$password = "";
$dbname = "krystalb_Sales_Tracking";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";


?>
<html>
<head>
<meta charset="UTF-8">

<title>Untitled Document</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>

<body>
 <?php
    //query
    $sql="SELECT * FROM customer ORDER BY customer.CUST_LAST_NAME;";
    $res=mysqli_query($conn, $sql);
    if(!$res)
    {
        die("Query failed!");
    }
    while($row=mysqli_fetch_row($res))
    {
        var_dump($row);
        echo "<br /><hr /><br />";
        mysqli_free_result($res);
    }
    ?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

试试看;

<?php
$servername = "localhost";
$username = "krystalb_krystal";
$password = "";
$dbname = "krystalb_Sales_Tracking";

$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_select_db($conn, $dbname) or die('Unable to select Database');
global $conn;  //Just in-case you need to use the connection inside a function, class, etc