Can anyone see anything wrong with this code?

I have tried tons of code like this to get it to work… pdo, object, you name it i have tried it…
It always worked for years until now… Can anyone see anything wrong with my code… if not can you give me some clues as to where to look to see if it maybe blocked you security… It does not give any errors and nothing is hitting the php error log. It just does nothing…
Any idea or suggestions would be appreciated.

echo "Testing.<br>";


$mysqli = new mysqli("localhost", $username, $password, $database);

$mysqli->select_db($database) or die( "Unable to select database");

// Perform query
if ($result = $mysqli -> query("SELECT * FROM LJRforms")) {
  echo "Returned rows are: " . $result -> num_rows;
  // Free result set
  $result -> free_result();


oh forgot… it dose print the word testing on screen so suggests that there are no php errors else would not print anything…

Did you check the error logs? There is nothing wrong with the code.

1 Like

Php’s error_reporting should always be set to E_ALL. When learning, developing, and debugging code/query(ies), display_errors should be set to ON, so that you get immediate feedback as to any problems. When running code on a live server, display_errors should be set to OFF and log_errors should be set to ON.

The above settings should be in the php.ini on your system, so that they can be set or changed at a single point. You may need to stop/start your web server in order to get any changes made to the php.ini to take effect. You should also use a phpinfo() statement in a .php script file to make sure that the settings are actually these values.

Next, you ALWAYS need error handling for statements that can fail. For database statements, the easiest way of adding error handling for all the statements that can fail - connection, query, prepare, and executed, is to use exceptions for errors and in most cases let php catch and handle the exception, where php will use its error related settings (see the above paragraphs) to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.) You would then remove the existing hodgepodge of database statement error handling, since it is no longer needed. When using exceptions for errors, your main code only deals with error free execution, since program execution transfers to the nearest correct type of exception handler upon an error, which will be php in this case.

As to the posted code -

  1. You are selecting the database when you make the connection. Why are you re-selecting it on the next line?
  2. You should list out the columns you are SELECTing so that you only retrieve the data that you want and your code will be self-documenting.
  3. There’s generally no need to free result resources or close database connections since php will automatically destroy all the resources used on a page when your script ends.

If you are just starting out, use the much simpler and more consistent PDO extension.


Is the mysqli extension installed?

You can run phpinfo to find out.

1 Like

That select_db Statement doesn’t make much sense. If you’re already selecting a database using the connection, why are you selecting it again? I don’t follow this logic you have unless you are selecting 2 different databases. It still doesn’t make sense to me though.

1 Like

Yes in fact nothing shows in the error logs…and it may be related to my tough security…
however when i added the words "yeah buddy as first line in the php file it then produced this in the error log… but no errors when the code is used as shown.
[01-Jul-2021 01:30:53 UTC] PHP Parse error: syntax error, unexpected ‘buddey’ (T_STRING) in /home/idxseopremium/public_html/_mysqli/open3.php on line 7

Seems odd that it found buddey but ignored the yeah…
Thank you so much for your response…

Yes and i inserted the phpmyinfo as you suggested and here is what i got…
mysqli: Zak Greant, Georg Richter, Andrey Hristov, Ulf Wendel
Client API library version mysqlnd 7.4.20
mysqli.default_port 3306
API Extensions mysqli,pdo_mysql
Thank you so much for your suggestions… I appreciate it very much…

The following are in the wp-config.php
define( ‘WP_DEBUG’, true );
define( ‘WP_DEBUG-LOG’, true );
define( ‘SAVEQUERIES’, true );
But when i go to wp-content and look around i do not see the standard error file that normally is associated here… and only very few thing kick off the regular php error log… as i showed one that when i added “yeah buddy” to the php file and i have shown that error log report here also.

I have tried adding this to the top of the php file to no avail.

ini_set('display_errors', 'On');
echo "<h2>PHP BEGINS.</h2>";

The echo php begins prints perfectly but all the other crap does not even show an error when i add ‘yeah buddy’ to the php file… but it does throw an error in the php error log…

For info:
I have tried adding this to the top of the php file to no avail.

ini_set('display_errors', 'On');
echo "<h2>PHP BEGINS.</h2>";

But thanks for all the suggestions as I have seen so much more response from this forum than from a hundred others… No… i did not post this in any other forum… just saying very rare to get this much response… and its very much appreciated…

Yes… error logs rarely show anything but further down this page i show what error came up in the error logs when i inserted a bad command like ‘yeah buddy’ in the top of the php file…
Thank you for your responce.

I tried pdo version and object version and everything i can think of is why i suspect the High level security system PxaxtxcxhxSxtxaxcxk i am using maybe be interfering… i have searched the htaccess to see what i can find and did find some stop showing errors which i have disabled…as shown here…

Even with this disabled I still see no debug file landing in the wp-content folder as it normally should.’ I removed that code as it was messing up my screen basically what the htaccess code was doing was killing the ability to write a debug file… removeing it did not allow a debug…

All the xxxx is my attempt to make it print so yu can see it… disregard all the xxx.
Anyway what it shows is they prevented logging to the debug file…

The double selection was my bad as i was combining code from two different sections and i liked that one since it had error checking. Should it still work if you do select the db twice… but either way no error of any source was showing… I will remove the seond line and see if anything changes.
Thanks for your response i really appreciate it.

This indicates that both the mysqli and PDO_Mysql extensions are installed.

As to the php syntax error showing up in the log file. This indicates that at least that level of error_reporting is set and log_errors is set to ON.

The reason that adding the error_reporting/display_errors settings in your code doesn’t show the php syntax error is because your code never runs upon a syntax error, so those two lines of code are never executed to alter those settings. I would leave those lines in for the time being, so that you don’t need to keep checking the error log to see any run-time errors.

The reason you are not seeing any result from the rest of your code, when you remove the line causing the php syntax error, is because of the logic being used in the file. You have an if() conditional statement around the ->query() call, which is apparently failing due to an error, but because the code is not doing anything when the if() is false, you are not getting any output.

You need to do this -

You would then remove that if() conditional logic from the code.

If you had the same conditional statement around the ->query() call, you won’t get any output when the query fails due to an error.

For the PDO version of the code, the connection always uses an exception for an error. If you follow he advice I gave about using exceptions for all database statements that can fail, you would enable exceptions for errors when you make the database connection. This would be causing a php uncaught exception error (assuming that error_reporting is set to E_ALL), which would cause the actual error information to be either displayed or logged, depending on the display_errors and log_errors settings.

1 Like

I rem out the extra select with no change… but thanks for pointing that out… every day I learn a tiny bit more… but I still suspect a security problem… but cannot figure out why no error… but that is life i guess… Thanks for the help.

I tested your code and the only thing I can say is maybe the table is misspelled or doesn’t exist. Double check your table spelling.

1 Like

I rem’d out the extra selection and removed the if as you suggested and here is what i got… looks like we are finally getting some action: Thank you so much… hope i am responding to the correct person… getting confusing…
Notice : Trying to get property ‘num_rows’ of non-object in /home/myusername/public_html/_mysqli/open3.php on line 25
Returned rows are:

Here is the complete file now…

ini_set('display_errors', 'On');
echo "Testing.<br>";

$mysqli = new mysqli("localhost", $username, $password, $database);
// $mysqli->select_db($database) or die( "Unable to select database");
// Perform query
// if ($result = $mysqli -> query("SELECT * FROM LJRforms")) {
//   echo "Returned rows are: " . $result -> num_rows;
// }

$result = $mysqli -> query("SELECT * FROM LJRforms");
echo "Returned rows are: " . $result -> num_rows;

// $mysqli->close();

If i could get it to pull the rows or anything from the db… i think i can handle if from there in case my code is wrong and you want to mod it to pull one or two fields i have included a couple of fields from the DB… I see someone just asked about confirming the table… table confirmed as LJRforms.
Wait one while i get a couple fields.
id, category_id, is_published, name, description
Would changing form * to like id, category_id make it simpler to test? LIke maybe

$result = $mysqli → query(“SELECT id, description FROM forms”);
echo "Returned rows are: " . $result → num_rows;
Nope: tested that and gave same error
Notice : Trying to get property ‘num_rows’ of non-object in /home/idxseopremium/public_html/_mysqli/open3.php on line 18
Returned rows are:

Non-object: am i mixing two different types of code here… some object and some pdo…
Thanks for your help.

IF a few fields will help anyone with other idea here are a few…
LJRforms dbase fields = id, category_id, is_published, name, description

confirmed… LJRforms with fields id, category_id, is_published, name, description
its been 2 years but i have done a lot of this type coding and never a problem is why I wonderd if some sort of hi tech security is holding me back… I may move to one of my low cost servers and try the same code… it cant be this hard… thanks for your input…

This is actually a follow-on error. The actual problem is that the sql query failed for some reason. To enable exceptions for errors for the mysqli extension (everyone here was hoping you would switch to the much better PDO extension) add the following line of code before the point where you make the database connection -

1 Like

@shirleymanson ,

If you are still having problems then try this script which I have tested:

<?php declare(strict_types=1);
error_reporting(-1); // MAXIMUM ERROR REPORTING
ini_set('display_errors', 'true');

echo "<pre> Testing.<br>";

if(10) :
  $hostname = 'localhost';
  $username = "myusername";
  $password = "mypassword";
  $database = "mydatabase";

$mysqli = new mysqli($hostname, $username, $password, $database);
  # $mysqli->select_db($database) or die( "Unable to select database");
  echo '<pre>'; // add linefeds 

  echo '<br>line: ' .__line__ .'<br>';
  echo '<hr>';

  // Perform query
  if ($result = $mysqli -> query("SELECT * FROM `$LJRforms`")) {
    echo '<br>line: ' .__line__ .'<br>';
    echo '<hr><b> $result ==> </b>'; 
    echo '<hr>';

    echo '<br>line: ' .__line__ .'<br>';
    echo "<br><b> Returned rows are: </b>" 
         .$result -> num_rows;
    echo '<hr>';

    // Free result set
    # $result -> free_result();
// $tmp = $mysqli->close();


1 Like

This sounds like your query is returning false. The only 2 data type that comes from a query selection is either an object or Boolean. That’s why it’s saying you’re trying to get a property from a non-object variable. Only 1 explanation is that it’s returning false and therefore num_rows doesn’t exist in the context of a Boolean. Which goes back to my statement about checking for misspelling of the database and table.

Also, you should try using a local database just to make sure it’s not a security permission problem.

1 Like