php查询不起作用

oknwwptz  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(431)

我已经运行这个代码好几天了。我已经在phpmyadmin中测试了我在数据库中的查询,它工作了,我到数据库的连接显示它已连接,但是当我运行代码时它不工作。有什么想法吗?
我的问题是:

SELECT * 
FROM customer 
ORDER BY customer.CUST_LAST_NAME

下面是我的php代码:

<!doctype html>
<?php
require_once('./connection.php')
?>

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

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

<body>
 <?php
    //query
    $resultset = $mysqli->query ("SELECT * FROM customer ORDER BY customer.CUST_LAST_NAME;");
    echo $resultset->num_rows;
 ?>
</body>
</html>
dbf7pr2w

dbf7pr2w1#

您总是需要按照下面php文档中的示例进行错误处理。
这样您就可以调试问题,即:
它无法连接到数据库
您的查询语法有错误
http://php.net/manual/en/mysqli.error.php

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if (!$mysqli->query("SET a=1")) {
    printf("Errormessage: %s\n", $mysqli->error);
}

/* close connection */
$mysqli->close();
?>
5tmbdcev

5tmbdcev2#

此代码已根据您的需要进行了调整。它将显示您想要的行计数以及客户的姓氏。如果它能解决你的问题,你可以把它标为正确答案。如果我不在这里,我会进一步帮助你。
将数据库凭据更改为您的凭据

<!doctype html>
<?php
//require_once('./connection.php')

$dbhost = 'localhost:3306';
         $dbuser = 'root';
         $dbpass = '';
         $dbname = 'angular';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
         echo 'Connected successfully<br>';

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

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

<body>
 <?php
    /*query
    $resultset = $mysqli->query ("SELECT * FROM customer ORDER BY customer.CUST_LAST_NAME;");
echo $resultset->num_rows;*/

$sql = 'SELECT * from customer order by customer.CUST_LAST_NAME';
         $result = mysqli_query($conn, $sql);

echo $result->num_rows;

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
               echo "<br><br>customer lastName: " . htmlentities($row["CUST_LAST_NAME"], ENT_QUOTES, "UTF-8"). "<br>";
            }
         } else {
            echo "0 results";
         }
         mysqli_close($conn);

    ?>
</body>
</html>

相关问题