调用未定义的方法mysqli_stmt :: fetchColumn()

时间:2014-10-28 08:42:56

标签: php pdo mysqli

我试图获取从sql查询返回的行数

require_once("inc/db_const.php");
$date=$_GET['date_rdv']; 
$mysqli= new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if($mysqli->connect_errno)
{
   echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
   exit();
}
$query_check="SELECT count(*) FROM planning_cl WHERE date_plannig = ?";
$statement_check = $mysqli->prepare($query_check);
$statement_check->bind_param('s',$date);
$statement_check->execute();
$rdv_verif=$statement_check->fetchColumn();
echo $rdv_verif;

但它显示了我的错误

r: Call to undefined method mysqli_stmt::fetchColumn()

任何人都可以给我解决这个问题的任何解决方案

4 个答案:

答案 0 :(得分:2)

mysqli中没有fetchColumn方法,该方法属于另一个驱动程序,PDO。

但是您可以使用mysqli_fetch_row()[0]轻松地模拟它:

$sql = "SELECT count(*) FROM planning_cl WHERE date_plannig = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s',$date);
$stmt->execute();
$rdv_verif = $stmt->get_result()->fetch_row()[0];
echo $rdv_verif;

答案 1 :(得分:1)

MySQLi中没有mysqli_stmt::fetchColumn()看到Documentation这是 PDO 方法

对于MySQLi,请使用mysqli_stmt_num_rows ( )

$rdv_verif=$statement_check->mysqli_stmt_num_rows();
echo $rdv_verif;

答案 2 :(得分:0)

您需要使用$statement_check->fetch();

require_once("inc/db_const.php");
$date=$_GET['date_rdv']; 
$mysqli= new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if($mysqli->connect_errno)
{
   echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
   exit();
}
$query_check="SELECT count(*) as cnt FROM planning_cl WHERE date_plannig = ?";
$statement_check = $mysqli->prepare($query_check);
$statement_check->bind_param('s',$date);
$statement_check->execute();
$line=$statement_check->fetch(PDO::FETCH_ASSOC);
$rdv_verif = $line['cnt']
echo $rdv_verif;

答案 3 :(得分:0)

如同类似问题所示:

  

Why is PDO fetchColumn() not working here


如果要获取返回的行数,请使用rowCount

// ...
$rows = $stmt->rowCount();

echo 'Rows found '.$rows;
// ...

获取所需行数的最合适方法是哪种,所以在您的情况下:

$rdv_verif=$statement_check->rowCount();