在PHP中回显MySQL查询

时间:2015-02-20 18:42:33

标签: php mysql

有人可以告诉我为什么这不起作用。当我在PHPMYADMIN中运行查询时,它会报告正确的值。

// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if (mysqli_connect_errno())
  {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($conn, "SELECT COUNT('id') AS RowCount FROM 'needs' WHERE ('status'=100)");


echo $result;

2 个答案:

答案 0 :(得分:1)

你正在结合mysqli和mysql_函数。要么使用mysqli,要么使用mysql_。但请注意,出于安全原因,不推荐使用mysql_函数。

答案 1 :(得分:-1)

您好,您需要此代码。

//If the query returns more 1 row you must use this.
...
$query = "USE database_name; SELECT COUNT(`id`) AS RowCount FROM `needs` WHERE (`status`=100);"

$result= mysql_query($query) or die(mysql_error());

if ($row= mysql_fetch_array($result))
   {  
     DO{
        echo $row["RowCount "];  
        }WHILE ($row=mysql_fetch_array($result));
    }
// now if you query returns only 1 wor use this.

$result= mysql_query($query) or die(mysql_error());
$rowQry= mysql_fetch_array($result);
echo $rowQry["RowCount "];

Luky