0

Based on earlier Q&A in the forum I still cannot solve my issue on this matter.

/* connect to database */
$mysqli = mysqli_connect ('<<server>>', '<<user>>', '<<password>>', '<<database>>');    
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {echo "<br>Successfully connected to database";}

/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("SELECT * FROM wp_wc_order_stats"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
} else {echo "<br>Successfully prepared query";}

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}  else {
    echo "<br>Successfully executed query";
    echo "Returned rows are: " . mysqli_num_rows($stmt);
    $row = mysqli_fetch_assoc($stmt);
}

/* explicit close recommended */
$stmt->close();

As an answer I get:

Successfully connected to database  
Successfully prepared query  
Successfully executed query  
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in /var/www/domains/judosensei.nl/www/test2.php on line 80  
Returned rows are:  
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given in /var/www/domains/judosensei.nl/www/test2.php on line 81

the database/this table is not empty. I removed where clauses and parameters to make it more simple to try to get a result. Probably I am overlooking the issue here, but staring and googling doesn't help anymore.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Try doing `$result = $stmt->get_result();` (after your `$stmt->execute()`) and then use `$result` in `mysqli_num_rows()` and `mysqli_fetch_assoc()`. Those functions expects a mysqli_result, but you're passing mysqli_stmt instead. Tbh, there's no reason to use `prepare()` over `query()` if you're just making a hard coded query without any parameters. – M. Eriksson Feb 13 '22 at 10:26
  • You should pass the query result to the mysqli_num_rows() function. Here, you are passing the query. – Roby Raju Oommen Feb 13 '22 at 10:28
  • What is that other Q&A you are referring to? – Your Common Sense Feb 13 '22 at 10:47
  • See here how to properly get your data https://stackoverflow.com/a/60020281/285587 – Your Common Sense Feb 13 '22 at 11:11
  • @M.Eriksson , you and Roby both saved my day! In the past I used $result = mysqli_query ($conn, "SELECT * FROM <> where user_id=$user_id");" therefor I didn't see I had no result field but the query. Many thanks!
    – Wilfred Derksen Feb 13 '22 at 13:21
  • @YourCommonSense, I meant other questions on the errormessage for mysqli_num_rows and "myslqi_fetch_assoc". The answers I found sofar I didn't see/recognise the solution. – Wilfred Derksen Feb 13 '22 at 13:24
  • That's because you didn't follow them. No answer would tell you to put stmt as a parameter. On a side note, there is no point in the num_rows at all. It is useless – Your Common Sense Feb 13 '22 at 13:28

0 Answers0