Problem converting MySQL to MySQLi

Hi PHPHelp,

I have a PHP/Mysql application which I have moved from an old server to a new one that has PHP7. I know this mean I must change from mysql to mysqli. Following some tutorials, I have sucessfully updated my mysql code. Most of my application works with the exception of the reading of a tables’ contents. I have created a test script using the connection code (it works), the code that reads the number of tables (that works also) but the code that reads the contents of a table doesn’t work. I have attached the script - conntest.php. Can you see why the table reading code produces a blank screen.

Regards

Gary

ps. apoligies, the layout of the code been lost. Should I have attached instead?

========= CODE ===========

<?php
$dbname = 'csg';
$dbuser = 'root';
$dbpass = 'xxxxxxx';
$dbhost = 'localhost';

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully</br>";

// Check Open dB
mysqli_select_db($dbname, $conn);

// Read number of tables
$test_query = "SHOW TABLES FROM $dbname";

$result = mysqli_query($conn, $test_query);

$tblCnt = 0;

while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}

// Read contents of table 'user'
$sql1 = "SELECT * FROM tbl_user";

$result1 = mysqli_query($conn, $sql1);
$resultcheck = mysqli_num_rows($result1);

while($row = mysqli_fetch_array($result1)) {
  $rowCnt++;
  echo $row[0]."<br />\n";
}
if (!$rowCnt) {
  echo "There are no rows<br />\n";
} else {
  echo "There are $rowCnt rows<br />\n";
}

if ($resultcheck > 0) {
	while($row = mysqli_fetch_assoc($result1)) {
		echo $row['user_name'] . "<br>";
	}	
}

mysqli_close($conn);
?>

========= CODE ===========

First thing to do is turn on error reporting. If you get a blank page, that is indicative of a fatal error that you aren’t seeing.

Hi astonecipher,

Good idea. I didn’t think of that. I have added the error reporting as follows, but I still get a blank screen. I don’t want to edit php.ini as I have some other apps on the same server

Cheers

Gary

<?php $dbname = 'csg'; $dbuser = 'root'; $dbpass = 'xxxxx'; $dbhost = 'localhost'; // Set error reporting error_reporting(E_ALL & ~E_NOTICE); // Create connection $conn = mysqli_connect($dbhost, $dbuser, $dbpass); //mysqli_connect($dbhost, $dbuser, $dbpass);

Forgot to add:

ini_set(‘display_errors’, 1);

Now I can see the errors :):grinning:

Your are a life saver! The code works. I had:

// Check Open dB
mysqli_select_db($dbname, $conn);

When I should have had:

mysqli_select_db($conn,$dbname);

Many thanks

Gary

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service