0

I'm trying to get the count result returned from an sql query

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;

but it shows me this error

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

can any one give me any solution to solve this problem

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
EMIN
  • 737
  • 1
  • 6
  • 9
  • possible duplicate of [Why is PDO fetchColumn() not working here](http://stackoverflow.com/questions/16233323/why-is-pdo-fetchcolumn-not-working-here) – Kypros Oct 28 '14 at 08:51
  • 1
    Well, yes, `mysql_stmt` indeed does not have a method called `fetchColumn`, period. That's not how mysqli works. Please RTFM to see examples of how mysqli is actually used: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – deceze Oct 28 '14 at 09:19
  • First you are not using pdo in your sample so you should remove that tag from your question. `mysqli` is the correct tag. Indeed I also find the `fetch_column` is missing from my mysqli version (Mariadb 10.6.4; mysqlnd 7.4.25). The [documentation](https://www.php.net/manual/en/mysqli-result.fetch-column) is clear that this works from 8.1.0 only. – theking2 Dec 01 '21 at 16:01

4 Answers4

2

There is no fetchColumn method in mysqli, this method belongs to another driver, PDO.

but you can easily simulate it with 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;
Dharman
  • 30,962
  • 25
  • 85
  • 135
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

There is no mysqli_stmt::fetchColumn() in MySQLi seeDocumentation This is a PDO method.

For MySQLi use mysqli_stmt_num_rows ( )

$rdv_verif=$statement_check->mysqli_stmt_num_rows();
echo $rdv_verif;
david strachan
  • 7,174
  • 2
  • 23
  • 33
-2

You need to use $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;
Kypros
  • 2,997
  • 5
  • 21
  • 27
Nebojsa Susic
  • 1,220
  • 1
  • 9
  • 12
-2

As seen in a similar question:

Why is PDO fetchColumn() not working here


If you want to get the number of rows returned use rowCount

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

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

Which is the most appropriate way of getting the number of rows that you need, so in your case:

$rdv_verif=$statement_check->rowCount(); 
Community
  • 1
  • 1
Kypros
  • 2,997
  • 5
  • 21
  • 27