Query failing, please help

I wonder if somebody can help me please, I’ve got a website that has been working without any problems for over a year then the other day it stopped. I discovered that my hosts had changed things and the root directory was no longer the same. I updated this and the site is now back but all DB query’s are failing and I don’t know why. If I take the select query and run it direct in PHPMyAdmin then I get results but not he site I don’t so would be grateful for any assistance please.

I’m using this to connect:

function dbselect( $query,$dblink ) {
    if( $result=$GLOBALS[$dblink]->query( $query ) ) {
      while( $row=$result->fetch_array( MYSQLI_ASSOC ) ) { $dbdata[]=$row; }
      if( !isset( $dbdata ) ) { $dbdata=false; }
      if( is_object( $result ) ) {
        $result->close();
      }
      return( $dbdata );
    } else {
        logdberr( $query,'Query failed ('.$GLOBALS[$dblink]->error.')' );
        exit();
      }
    }
  mysqli_close($dbLinkInt);
  }

It fails every time but if I run select * from feeds where product_feed_id=5671 in PHPMyAdmin I get the product I’m after.

If it helps the version is 5.5.60-MariaDB

Thanks in advance

validate what is coming into the function, because the code there offers no clue.

function dbselect( $query,$dblink ) {
    echo $query;
    if( $result=$GLOBALS[$dblink]->query( $query ) ) {

$dblink is this:

$dbLinkInt=new mysqli( "p:".'dbaddress','username','password','databasename' );

and the $query is:

select * from feeds where product_feed_id=5671

Why are you prepending “p:” in front of your dbaddress? I cant think of a scenario in which that makes a valid server name, unless you’ve got some very specific host dns going on.

echo $dbLinkInt;

If you get a Resource ID, it’s made a connection. If you get nothing, your connection’s failed before you even send the query.

Okay I may have done it wrong (I did logdberr( $query, echo $dbLinkInt;' );) , but I didn’t get anything at all.

I’m using this to create the connection but the error I get relates to the select query because it’s Query failed rather than Connecting to internal database….

  $dbLinkInt=new mysqli( "p:".$dbAddrInt,$dbUserInt,$dbPassInt,$dbBaseInt );
  if( $dbLinkInt->connect_error ) {
      logdberr( 'Connecting to internal database...','Connect Error ('.$dbLinkInt->connect_errno.') '.$dbLinkInt->connect_error );
      exit();
    }
  }
  mysqli_close($dbLinkInt);

Okay.

What error are you getting out of the query statement? Query failed …with what error message?

This is everything I get from logdberr( $query,'Query failed ('.$GLOBALS[$dblink]->error.')' );:

Query: select * from feeds where product_feed_id=5671

Error: Select error - Query failed ()

POST Dump:
array(0) {
}

GET Dump:
array(3) {
[“prodUSN”]=>
string(4) “5671”
[“brand”]=>
string(11) “Nutrition”
[“product”]=>
string(18) “Bundle”
}

Cookie Dump:
array(5) {
[“_ga”]=>
string(27) “GA1.3.2001560339.1539086925”
[“_gid”]=>
string(26) “GA1.3.834113878.1551722150”
[“route”]=>
string(32) “2143aadb7c3e8431720d407fdc271ec9”
[“__atuvc”]=>
string(22) “14|6,3|7,36|8,1|9,9|10”
[“__atssc”]=>
string(27) “twitter;2,reddit;1,google;1”
}

If $dblink is your database mysqli object, why are you querying something else?

if( $result=$GLOBALS[$dblink]->query( $query ) ) {

wouldnt you mean

if( $result=$dblink->query( $query ) ) {

? By definition, $dblink should be a string.

I tried that but just got a 500 error and nothing loaded. I’ve checked the console log and it says ‘Resource has no content’ I’m really desperate to get this sorted :frowning:

mkay. Let’s try this. Don’t change these, dont put them inside another function that’s going to do things to them. copy and paste directly after the opener of your dbselect function.

var_dump($dblink);
echo "<br>===<br>";
var_dump($GLOBALS[$dblink]);
echo "<br>===<br>";
var_dump($query);
echo "<br>===<br>";
var_dump($GLOBALS[$dblink]->query($query));
echo "<br>===<br>";
var_dump($GLOBALS[$dblink]->error());
echo "<br>===<br>";
$stmt = $GLOBALS[$dblink]->query($query);
var_dump($stmt->num_rows);

I got this:

string(9) "dbLinkInt" 
===
object(mysqli)#1 (19) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50560) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["stat"]=> NULL ["sqlstate"]=> NULL ["protocol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL } 
===
string(269) "SELECT product_name, weight, weight_id, serving, price, product_brand, protein_source, protein_100, fat_100, calories_100, carbs_100, aw_deep_link, product_feed_id, image_url, best_seller from feeds WHERE enabled=1 and price >= and price <= ORDER BY " 
===
NULL 
===

Thank you for helping with this.

Well thats not the query you told me you were debugging. That query clearly has errors in its where clause…and order by, for that matter.

Sorry that was from the homepage, I forgot I originally said about another page. This is from the page I first mentioned:

string(9) "dbLinkInt" 
===
object(mysqli)#1 (19) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50560) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["stat"]=> NULL ["sqlstate"]=> NULL ["protocol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL } 
===
string(46) "select * from feeds where product_feed_id=5671" 
===
NULL 
===

and where are the last 2 lines? You’ve pasted the first 4.

That’s everything from the page

Check the page’s source? I gave you 6 lines of code separated by 5 separators, and you’ve seen only 4 lines of code and 4 separators, which… isnt possible?

This is what I have:
### database select function dbselect( $query,$dblink ) { var_dump($dblink); echo "<br>===<br>"; var_dump($GLOBALS[$dblink]); echo "<br>===<br>"; var_dump($query); echo "<br>===<br>"; var_dump($GLOBALS[$dblink]->query($query)); echo "<br>===<br>"; var_dump($GLOBALS[$dblink]->error()); echo "<br>===<br>"; $stmt = $GLOBALS[$dblink]->query($query); var_dump($stmt->num_rows); if( $result=$GLOBALS[$dblink]->query( $query ) ) { while( $row=$result->fetch_array( MYSQLI_ASSOC ) ) { $dbdata[]=$row; } if( !isset( $dbdata ) ) { $dbdata=false; } if( is_object( $result ) ) { $result->close(); } return( $dbdata ); } else { if( $GLOBALS['dbdebug'] ) { die( 'Select error (debug) - Query failed:<br /><br />'.$query.'<br /><br />('.$GLOBALS[$dblink]->error.')' ); exit(); } else { logdberr( $query,'Select error - Query failed ('.$GLOBALS[$dblink]->error.')' ); // header( "Location: /error/dberror.php" ); exit(); } } mysqli_close($dbLinkInt); }

The page is here: https://www.xxxxxxxx.com/product.php?prodUSN=5 if it helps

It definitely helps.

Add the following to the top of your page:

<php declare(strict_types=1); 
error_reporting(-1); 
ini_set('display_errors', 'true');

Okay I’ve added that now

Oh of course, blind me.

Why are you closing the connection at the end of your select function? If you have to run more than one select, you would have to open the database connection again.

The error messages on your page tell me that the database connection is closed when you attempt to run this query.