Hi all, the following PHP MysSQLi query works in XAMPP but not in WAMP:

    <?php
    include('../../inc/config.php');
    $mysqli = mysqli_connect($config['host'], $config['user'], $config['pass'], $config['db']);
    $responseQuery = "SELECT tr.id as trid,tr.topicid,tr.usersid,DATE_FORMAT(tr.date_added,'%b %d, %Y %H:%i') 
                      AS format_date_added,tr.date_added,tr.response,tr.status,
                      t.id, t.usersid as creatorid,
                      u.id, u.authuser, u.memtype, u.fn,u.ln,u.avatar 
                      FROM topic_response tr
                      LEFT OUTER JOIN topic t ON t.id=tr.topicid
                      LEFT OUTER JOIN users u ON u.id=tr.usersid 
                      WHERE tr.topicid='".$_GET['id']."' 
                      ORDER BY tr.date_added";
    $responseData = $mysqli->query($responseQuery);
    $responseCount = mysqli_num_rows($responseData);

In xampp the script works a treat. In WAMP I get this very common error:

 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in ... on line 14

That error is talking about this line:

 $responseCount = mysqli_num_rows($responseData);

I have the MySQLi extension, I've checked that in my php.ini file and it's enabled. Any ideas people? I've just made the switch from XAMPP to WAMP as XAMPP was having a few issues randomly and corrupting my databases. WAMP works great... apart from this issue.

Michael

What is actually causing the error is hard to say, but the main problem here is the lack of error handling in your code. You are assuming that the $mysqli->query() call on line 13 is always successful. If that is not true, which seems to be the case now, when you pass the invalid result set to mysqli_num_rows on line 14, you get a warning like that.

Always, always, verify the results of a call to an external resource, like a databases or a file, before continuing as if it was successfully used. That is extremely important.

Also, I'd suggest not mixing the MySQLi OOP and functional stuff. Either stick to OOP or functional, for the sake of consistency.

Hi Atli,

In regards to the mix of OOP and procedural I'm not concerned one bit. The code was written 100% procedural and I was in the process of switching everything over to OOP. I understand what you're saying completely, but it's unrelated to the issue at hand.

In regards to verifying the results of a call are you simply referring to:

if($responseData){
      //code
  }else{
      //code
  }

If so then again I understand what you're saying and again it has nothing to do with the issue at hand. This code was in place, I did however remove it in order to dig into what exactly wasn't working in the change from XAMPP to WAMP.

Thanks for your response though,
Michael

Removing the error handling while trying to debug the error makes no sense to me. You should be using it to find out exactly what's happening. Normally, when dealing with MySQL results, you'd do something akin to:

$result = $mysqli->query($sql);
if ($result) {
    // Do stuff with the result.
}
else {
    trigger_error("Query failed: " . $mysqli->error, E_USER_ERROR);
}

Which would then log or print out the exact reason why the query failed.

You are running this on XAMPP and WAMP on the same machine? Do they share the same MYSQL? If not, check your config, specifically the port.

No prit they're on two different machines :) I should have said that

Like @prit said, are you sure you have the right port? Are you sure this port is open? I Persume that you're using local machines (i.e. not a server) for this?

The port is not the issue, or he'd be getting a "Call to a member function on a non-object" error on the query call, not the given error on the num_rows call. If the port is wrong MySQLi would fail to connect, making $mysqli FALSE when the function approach is used. (It would also fail with the OOP approach, just with a different error message.)

What you need to do in order to debug this is get the error message MySQL is returning back, the way I demonstrated in my last post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.