我的虚拟主机已从PHP5迁移到PHP7,现在我无法正确访问MySQL数据库。我已经使用www.w3schools.com修复了该代码,但是我无法使其正常工作。
第一个是原始代码,第二个是我尝试修复的代码。第二个代码看起来很长。这是因为当我添加新代码并添加大量回声以查看代码在崩溃之前进行多远时,我注释掉了旧代码。
我不是PHP专业人员。我只了解了使我的网站做我想要的事情所需要的。它包括很多谷歌搜索“如何使用PHP”。
感谢您为克服这一变化所提供的帮助。
----这是PHP5的原始代码------------------------------------- -
<?php
// Set database varables
include("../member/config-DB.php");
$db_name="cindy0_TQPMmembers"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select database");
// get sort column
$sort=$_GET['sort'];
if ($sort=="") $sort="FirstName";
// read data for First and Last name
$query="SELECT * FROM $tbl_name WHERE status='new' ORDER BY $sort ASC";
$result=mysql_query($query);
// Count number of rows in database table
$num=mysql_numrows($result);
// Close connection to server
mysql_close();
?>
----------原始代码结尾--------------------------------- ---------
------------我试图使代码适用于PHP7 ---------------------------
<?php
// Set database varables
include("../member/config-DB.php");
// provide database and table name
$db_name="cindy0_TQPMmembers"; // Database name
$tbl_name="members"; // Table name
//echo "<BR> :: DB name = $db_name ::";
//echo "<BR> :: table name = $tbl_name ::";
// Create connection
$conn = new mysqli($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "<BR> Connected successfully";
// select database
//mysql_select_db("$db_name")or die("cannot select database");
echo "<BR> :: past select database ::";
// get sort column
$sort=$_GET['sort'];
if ($sort=="") $sort="FirstName";
echo "<BR> :: sort = $sort ::";
// read data
// $query="SELECT * FROM $tbl_name WHERE status='new' ORDER BY $sort ASC";
// $result=mysql_query($query);
$sql = "SELECT * FROM $tbl_name WHERE status='new' ORDER BY $sort ASC";
$result = $conn->query($sql);
echo "<BR> :: past read data ::";
// Count number of rows in database table
//$num=mysql_numrows($result);
//$num=$result->num_rows;
//echo "<BR> :: number of rows = $num ::";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "results = $result->num_rows= <br>";
}
} else {
echo "0 results";
}
$conn->close();
echo "<BR> :: connect closed ::";
?>
--------------我尽力使代码正常工作--------------------
我连接到数据库好。之后,事情就出了问题。 $ result-> num_rows应为1,但始终返回为0。