PHP查询不起作用

时间:2018-08-18 07:43:04

标签: php mysql

我已经连续几天运行这段代码了。我已经在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>

2 个答案:

答案 0 :(得分:0)

此代码已进行调整以满足您的需要。它会显示您想要的行计数编号以及客户的姓氏。如果它解决了您的问题,则可以将其标记为正确答案。如果它在这里无法ping通,我将为您提供进一步的帮助。

将数据库凭据更改为您的

<!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>

答案 1 :(得分:-1)

您总是需要按照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();
?>