tags:

views:

179

answers:

4

Hello.

I have changed some of my old queries to the Mysqli framework to improve performance. Everything works fine on localhost but when i upload it to the webserver it outputs nothing. After connecting I check for errors and there are none. I also checked the php modules installed and mysqli is enabled.

I am certain that it creates a connection to the database as no errors are displayed. (when i changed the database name string it gave the error)

There is no output from the query on the webserver, which looks like this:

$mysqli = new mysqli("server", "user", "password");

if (mysqli_connect_errno()) {
   printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
   exit;
}

// Query used  
$query = "SELECT name FROM users WHERE id = ?";

if ($stmt = $mysqli->prepare("$query")) 
{

    // Specify parameters to replace '?'
    $stmt->bind_param("d", $id);  

    $stmt->execute();

    // bind variables to prepared statement 
    $stmt->bind_result($_userName);

    while ($stmt->fetch()) 
    {
          echo $_userName;
    }


    $stmt->close();
 }
}

//close connection 
$mysqli->close(); 

As I said this code works perfectly on my localserver just not online. Checked the error logs and there is nothing so everything points to a good connection. All the tables exists as well etc. Anyone any ideas because this one has me stuck! Also, if i get this working, will all my other queries still work? Or will i need to make them use the mysqli framework as well? Thanks in advance.

A: 

Mysqli framework does not improve performance.
Error checking must be done not only after connect but after each query execute.

Check $stmt->error for errors
Or try to check against throwed exception

Col. Shrapnel
mysqli can improve performance if you call stored procedures with it. I've done plenty of benchmarks comparing sprocs v. inline sql calls and you'll get lots more throughput with lower overheads using them.
f00
@f00 how it can be that *call* method would affect *inner calculations*?
Col. Shrapnel
Thanks for the replies guys. I am using it to store queries that will be run many times, passing different user Id's etc, so it should help performance. Anyway, i checked for stmt errors and there were none output. This is a mystery to me. Please please someone help! Cheers
whamo
@whamo no, storing queries would not improve performance. and you will improve it much more, if you'll get rid of many queries with passing different user Id's. It must be one query and it will be real performance improvement. As of your question - it's simple: there is no data in the table match your query.
Col. Shrapnel
A: 

Ok Ive done some 'debugging' to see what is happening. There is data in the table i'm querying that matches the query. I did the following to check the prepare statement:

echo "debug 1";

if ($stmt = $mysqli->prepare("$shiftQuery"))  
{

echo "debug 2";
printf("Error: %s.\n", $stmt->error);

etc...
}

So basically it should output 'debug 1' before the statment is prepared (Which it does). Then after it should output 'debug 2' and any errors that occured.

The problem is here somewhere as it doesnt reach the debug 2 line in the IF statement. Since the connection details are fine, i cant really see why the $mysqli object wouldnt be created. That give anyone any further ideas???

thanks

whamo
+1  A: 

Each call to a mysqli/stmt method can fail. You should check each and every one.
Try it with error_reporting(E_ALL) and maybe display_error=On

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);

// passing database name as fourth parameter
$mysqli = new mysqli("server", "user", "password", 'dbname');
if (mysqli_connect_errno()) {
   printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
   exit;
}

// Query used  
$query = "SELECT name FROM users WHERE id = ?";

if ( false===($stmt=$mysqli->prepare("$query")) ) {
  echo 'mysqli::prepare failed: ', htmlspecialchars($mysqli->error);
  die;
}

// Specify parameters to replace '?'
$rc = $stmt->bind_param("d", $id);  
if ( !$rc ) {
  echo 'bind_param failed: ', htmlspecialchars($stmt->error);
  die;
}

echo '<pre>Debug: execute()</pre>';
$rc = $stmt->execute();
if ( !$rc ) {
  echo 'execute failed: ', htmlspecialchars($stmt->error);
  die;
}

echo '<pre>Debug: bind_result()</pre>';
// bind variables to prepared statement 
$rc = $stmt->bind_result($_userName);
if ( !$rc ) {
  echo 'bind_result failed: ', htmlspecialchars($stmt->error);
  die;
}

echo '<pre>Debug: fetch()</pre>';
while ($stmt->fetch()) 
{
  echo 'username: ', $_userName;
}
echo '<pre>Debug: stmt close()</pre>';
$stmt->close();

echo '<pre>Debug: mysqli close()</pre>';
$mysqli->close();
VolkerK
+1  A: 

MySQL user permission is host sensitive -- it may give user1 from localhost and user1 from other host different permission.

GRANT ALL ON *.* TO user@'%' IDENTIFIED BY 'password';
J-16 SDiZ