调用成员函数query()WAMP

时间:2016-05-09 10:12:17

标签: php mysql

这是我的 PHP 代码:

include ('dbconnect.php');
   $sql = "SELECT * FROM products";
   $result = $conn->query($sql);

   if($result -> num_rows >0){
     while($row = $result -> fetch_assoc()){
echo "$row[productID]"."$row[productName]";

}else {
echo "0 results";
}
    $conn -> close();

这是我的dbconn.php代码。我不知道我的错误在哪里,如果它在dbconn.php或其他 php 页面。

<?php

$conn = mysql_connect("localhost","root","")
or die (mysql_error());

$conn = mysql_select_db("shoppingcart",$conn) or die (mysql_error());

?>

1 个答案:

答案 0 :(得分:0)

试试这个,避免使用mysql而不是mysqli

include ('dbconnect.php');

   $sql = "SELECT * FROM your_table";

   $result = mysqli_query($conn, $sql);

   $count = mysqli_num_rows($result);
   if($count > 0) {
        while($row = $result->fetch_assoc()){
            echo $row['column1'] . $row['column2'] . "<br/>";
        }
   }

$conn -> close();

这是连接代码

$conn = mysqli_connect("localhost","root", "" ,"your_db");
if(!$conn) {
    die() . mysqli_error();
} else {
    //echo "connection successfull";
}