Can anyone see anything wrong with this code?

Wow! I must really be in trouble…Your program printed a totally blank screen for me… no echo no nothing…

But i continued to try different samples and moved to a new server that was not as secure as mine and I made a new db name with one table mytable… and i finally found one example that worked for me as shown below… I am completely amazed I never had any problem getting most examples to work for me… I tried your $LJRforms table and non $ LJRforms and neither worked so i tried the $mytable and no $ mytable… total blank screen… not even the word test showed so there may be a php error since it never printed the “testing word” and would explain the fully blank screen once that sucker hits a php error its dead…
I will search it closely for php errors but doubt if i will see one.
Here is the accidental one that worked…

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
echo "Testing.<br><br>";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

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

// $mysqli = new mysqli("localhost", "idxseopr_crm", "TangoZulu911@", "idxseopr_crm");

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

$cnt=0;

$query = "SELECT first_name, last_name FROM mytable";

$result = $mysqli->query($query);

/* fetch associative array */
while ($row = $result->fetch_assoc()) {

$cnt++;
print "$cnt ";
    printf("%s %s\n", $row["first_name"], $row["last_name"]);
    // printf("%s (%s)\n", $row["first_name"], $row["last_name"]);
    // the (%s)\n" adds the parenthesis around last name.. 1 Randall (Ivey) 
echo "<br>";
}




// $result = $mysqli -> query("SELECT first_name, last_name FROM mytable");
echo "<br>Printed rows are: " . $result -> num_rows;

Output of the accidental sample that worked out of hundreds of others…that failed.
By looking at what my puter can understand someone may be able to see a clue as to why
its so dang pick… never seen anything like it…
The fields in the mytable db is
first_name
last_name
email
phone
My query asked for only last and first name… so looks good…

Testing.

1 Randall Ivey
2 Sissy Joiner
3 Bobby Scraper
4 Sally Jally
5 James Chandler
6 Sally Jally
7 Susie Johnson
8 Lucy Gradious
9 Tammy Sinclair

Printed rows are: 9

Below i will send what i copied and pasted to see if i may have left out a crucial comma or ; but i copied it exact… but just in case… since the no print of even the word “Testing” suggests a php error…

<?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";
endif;  

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$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>';
  print_r($mysqli);
  echo '<hr>';

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

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



// Perform query
  if ($result = $mysqli -> query("SELECT * FROM `$mytable`")) {
    echo '<br>line: ' .__line__ .'<br>';
    echo '<hr><b> $result ==> </b>'; 
    print_r($result);
    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();

Not sure why the if 10 statement is around my uses and pass but i removed it temp to try and no help… Also i was not aware of ever putting a $ sign in front of the table name… but i tried it both ways with and without… can you explain that… always looking to learn some thing new…|

Thank you so very much for all the extra effort you put in… its greatly appreciated…

_________OUICK UPDATE before you start pulling your hair out… mine is all gone ______
So hold off till i check… here is what it did after i removed all of the program after this point:

<?php declare(strict_types=1); error_reporting(-1); // MAXIMUM ERROR REPORTING ini_set('display_errors', 'true'); echo "
 Testing.
"; if(10) : $hostname = 'localhost'; $username = "myusername"; $password = "mypassword"; $database = "mydatabase"; endif; mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = new mysqli($hostname, $username, $password, $database); // Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'myusername'@'localhost' (using password: YES) in /home/crontest/public_html/Test/open7.php on line 17 Based on this error.. i am confirming that mysqli in in my php info... I had checked it on the site i was working but now that have moved to a new less secure site need to check again.. will let you know in 10 min... Nope looks like the mysqli is loaded... heck.. ## mysqli |MysqlI Support|enabled| | --- | --- | |Client API library version|mysqlnd 5.0.12-dev - 20150407 - $Id: 7cc7cc96e675f6d72e5cf0f267f48e167c2abb23 $| Failing that command may be the reason its failing many of the other examples i tried.d.

I am desperate i will run anything to get the tools together to do some inserting into the db…
Do you have a full PDO example that i could try… When i search for samples i think i may be mixing like the db connect and then the actual commands like insert or get… and that may be part of my problem… Never had problems like this… my mysql is like 5. something…
Is that important?

The above is weird operation. You should have gotten a fatal runtime error containing that same information, not just a php Warning (just tested using a copy/paste of the posted code.) This is acting like the mysqli_report() had no effect or isn’t actually in the code that’s running on the server. AFAIK, there’s no way, with that mysqli_report setting, for you to get just a php Warning. Wondering what php version you are using and where you got it from?

This means that the database credentials you are using doesn’t match any database user that’s been created and given permissions on the database server. While you should create a specific user, with only the necessary permissions, you can for debugging, use the root user and root password.

Yes. This would be a common problem in any attempt to connect and you would only know this if you have error handling for the connection that successfully gives you the actual raw error information. In the above mass of trial/error, you indicated that one combination worked? What were the username/password values in the case and are they the same values you used for the Access denied … error?

Some of the weirdness up to this point could be due to mysqli issues. Most of the contributing forum members have moved on and are using the much simpler, better designed, universal, PDO extension. The following is typical connection/configuration settings for the PDO extension -

$DB_HOST = ''; // database host name or ip address
$DB_USER = ''; // database username
$DB_PASS = ''; // database password
$DB_NAME = ''; // database name
$DB_ENCODING = 'utf8'; // db character encoding. set to match your database table's character set

$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // set the error mode to exceptions
			PDO::ATTR_EMULATE_PREPARES => false, // run real prepared queries
			PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // set default fetch mode to assoc
			];

$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options);

Once you have a PDO connection, in $pdo, you can use the pdo methods, such as ->query(), ->prepare() to run or prepare a query. These return a PDOStatement object. You can use the pdostatement methods to execute a prepared query and deal with the result from a query.

Assuming the sql statement at the start of this thread is what you want to execute and loop over the result from -

// note: list out the columns you are selecting
// almost every select query needs an ORDER BY term
$sql = "SELECT * FROM LJRforms"; 
$stmt = $pdo->query($sql);
// since this query will return a set of zero or more rows of data, fetch all of them into an appropriately named php variable, then test/use this variable at the point of producing the output
$ljr_data = $stmt->fetchAll();


// at the point of testing/looping over the data
if(!$ljr_data)
{
	echo "<p>There is no data to display.</p>";
}
else
{
	echo "Number of returned rows: " . count($ljr_data);
	foreach($ljr_data as $row)
	{
		echo 'whatever you want using elements in $row...';
	}
}

Not sure why it was completely dead when first loaded… but i loaded your pgm again and here is the result… I can see that it pulled out the num_rows =9 on mine which is correct and it pulled out field count of 4 which is correct…
and here it is… so looks like your code works fine on my machine… is it pdo, object or what I need stick with one… i may be mixing pdo code and object or one of the other methods…
From the looks of your print out if i use the correct commands for Insert, and a few other commands it will work…
Thanks for all your help…

 Testing.


line: 18
mysqli Object
(
    [affected_rows] => 0
    [client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: 7cc7cc96e675f6d72e5cf0f267f48e167c2abb23 $
    [client_version] => 50012
    [connect_errno] => 0
    [connect_error] => 
    [errno] => 0
    [error] => 
    [error_list] => Array
        (
        )

    [field_count] => 0
    [host_info] => Localhost via UNIX socket
    [info] => 
    [insert_id] => 0
    [server_info] => 5.7.34
    [server_version] => 50734
    [stat] => Uptime: 1070084  Threads: 2  Questions: 100900795  Slow queries: 0  Opens: 3416874  Flush tables: 1  Open tables: 2000  Queries per second avg: 94.292
    [sqlstate] => 00000
    [protocol_version] => 10
    [thread_id] => 1423336
    [warning_count] => 0
)

line: 26
 $result ==> mysqli_result Object
(
    [current_field] => 0
    [field_count] => 4  correct for my site...
    [lengths] => 
    [num_rows] => 9 correct for my site...
    [type] => 0
)

line: 31

 Returned rows are: 9
1 Like

I am on a roll now Thanks…
I added the following to your code and it printed all your stuff plus mine at the end… Now if i find an insert command that works with your code I will be on a real roll…
The code I added is the example code that I found on the web and it worked on my site and that is the only example i could find till you sent me your code… Thank you…

    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();

// My added code below..
echo '<br>line: ' .__line__ .'<br>';
$query = "SELECT first_name, last_name FROM mytable";
$result = $mysqli->query($query);
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$cnt++;
print "$cnt ";
    printf("%s %s\n", $row["first_name"], $row["last_name"]);
    // printf("%s (%s)\n", $row["first_name"], $row["last_name"]);
    // the (%s)\n" adds the parenthesis around last name.. 1 Randall (Ivey) 
echo "<br>";
}
// $result = $mysqli -> query("SELECT first_name, last_name FROM mytable");
echo "<br>Printed rows are: " . $result -> num_rows;
    echo '<br>line: ' .__line__ .'<br>';

my output at end of your code… GREAT!
Plus I learned a new way to add line numbers to my code when the editor does not provide them as i use notepad a lot when in a hurry… Thanks for that alsol

 $result ==> mysqli_result Object
(
    [current_field] => 0
    [field_count] => 4
    [lengths] => 
    [num_rows] => 9
    [type] => 0
)

line: 31

 Returned rows are: 9
line: 45

**My output for code at at the end of your code.**

1 Randall Ivey
2 Sissy Joiner
3 Bobby Scraper
4 Sally Jally
5 James Chandler
6 Sally Jally
7 Susie Johnson
8 Lucy Gradious
9 Tammy Sinclair

Printed rows are: 9

line: 59

1 Like

Check out the following site which has some very good examples:

https://phpdelusions.net/mysqli

1 Like

The relevant script:

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

Instead of using TRUE or FALSE I rely upon PHP to change the integer 0 to the bool FALSE and anything else to TRUE. The if statement acts as a toggle. Deleting the 1 leaves the integer 0 and sets the if statement to FALSE;

The $ preceding the $tablename allows easily changing the table name in case the script was ever used inside a function.

I forgot to mention that when debugging and a blank screen appears I add the following line at the top of the script the script will stop and show the line number.

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

// the following line to be moved down until the page fails to render
echo '<br> line: ' .__line__; die; // halts further script

If there are PHP startup errors then a blank screen will appear. I then copy the file and start with the only the first few lines, get the line number to appear… then proceed to add more script until it renders a blank screen… until I find the error.

It is preferable to have a localhost development environment with the following default setting:

file: /etc/php/8.0/apache2/php.ini

; display_startup_errors = Off
display_startup_errors = On

The preceding ; makes the statement into a remark and inoperative.
After setting apache2 must be restarted from the Command Line:

systemctl restart apache2

1 Like

Thanks but that is where I got most of the samples i tried to run… I did find one that i could run… it is the example I showed… I could not get anything out of the PDO example which was nice but would not run for me… your example plus one I got from delusions is all i have that works so far.

My guess is i will have to go thru a dozen Insert examples before I find one that works… But anyway thanks for all your help…

1 Like

Thanks everyone for all the help… I finally got everything i need working… this has been the most helpful forum i have ever used…
tutorialrepublic.com is right down my alley… I love thier PDO what they do different is give the full code from connect to finish… for example if they are showing insert… they do not assume your are smart enough to make the connection and just give you the low down… each and every sample is a start to finish… but this forum is patient and superb… Thanks again…
This topic is complete…

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.