0

My first post here!

I wrote a job management portal for a client about 6 years ago and it hasn't really been updated at all since then. Yesterday it sort of stopped working and the shared hosting provider states no updates had been performed on their server to kill it. I narrowed it down to all of the database queries failing if they don't have bind parameters but they work if they do have bind parameters. Weird. Here is a simple script I wrote to show what's happening:

    <?php

$mysqli_connection = new MySQLi('localhost', '********', '******', '*********');
if ($mysqli_connection->connect_error) {
   echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
   echo "Connected<br />";
}

// this returns nothing
if($stmt = $mysqli_connection->prepare("SELECT id FROM users where username = 'dan'"))
{
    $username = 'dan';

//    $stmt -> bind_param("s", $username);
    $stmt -> execute();
    $stmt->store_result();
    $stmt -> bind_result($user_id);
    while ($stmt -> fetch())
        echo "result1: " . $user_id . "<br />";

    $stmt->close();
}

// this returns 1 (the ID) 
if($stmt = $mysqli_connection->prepare("SELECT id FROM users WHERE username = ?"))
{
    $username = 'dan';

    $stmt -> bind_param("s", $username);
    $stmt -> execute();
    $stmt->store_result();
    $stmt -> bind_result($user_id);
    while ($stmt -> fetch())
        echo "result2: " . $user_id . "<br />";

    $stmt->close();
}

// They should both output the ID, why is the first one failing?

?>

I moved the entire site over to my AWS EC2 server and it worked fine, it only fails like this on the shared hosting server where it currently resides. Does anyone know why or how this is possible? I've never seen anything like this before.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Daniel
  • 1
  • There is no need to prepare a query that has no parameters. I've never heard of this happening though – GrumpyCrouton Nov 05 '20 at 18:21
  • You can currently see it at sbs.sleepboutique.ca/test.php but we're in the process of pointing the subdomain to the EC2 instance so that may not be visible after 24 hours. – Daniel Nov 05 '20 at 18:21
  • I would suggest checking for [mysqli errors](http://php.net/manual/en/mysqli.error.php) when thee prepare fails, so you can find out why it failed instead of guessing. – aynber Nov 05 '20 at 18:26
  • 2
    Just a quick heads up: next time describing a problem to a complete stranger, avoid such wording as "it fails". Instead, provide as much description as possible - what output do you see, what exact error message do you get, what's in the error log, what debugging steps you took, etc. – Your Common Sense Nov 05 '20 at 18:31
  • There is no param specified in the query that needs binding – Desire Kaleba Nov 05 '20 at 18:35
  • Thanks for the advice, here is some more information. The $stmt->fetch loop doesn't run at all because the query returns no results, that's how it fails. I put an echo statement inside the loop but nothing gets printed because that loop doesn't run. I also changed the query to just "select id from users" and that also finds no results. I can only get it to enter the while loop if I provide a bind parameter. There aren't any errors because it doesn't error out, it just doesn't find any results in the database. It runs just fine. – Daniel Nov 05 '20 at 18:45
  • Two persons have mentioned the query preparation is not necessary but are you saying this could cause the empty query result? I'm not sure if you're telling me that's the cause or just general advice that it isn't necessary. – Daniel Nov 05 '20 at 22:32
  • So does anyone know why it's doing this? – Daniel Nov 09 '20 at 00:35
  • Actually I figured it out. An update was done on MariaDB a few days ago and everyone has been having a similar issue. The fix is to update PHP ( I think I read it was from 7.2 to 7.3 but I may be misremembering ). That's why it's only happening on that one server. – Daniel Nov 09 '20 at 01:19

0 Answers0