You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP 5.6升级至7.1故障:mysql转mysqli后预订网站仅显示顶部内容

Fixing MySQLi Migration Issues When Moving from PHP 5.6 to 7.1

Hey Jacob, let's break down why your find-and-replace of mysql_* to mysqli_* isn't working fully—this is a super common pitfall for folks upgrading from the old deprecated MySQL functions to MySQLi. The problem isn't just renaming functions; MySQLi has different syntax requirements that your code is missing. Here's how to troubleshoot and fix this:

1. You're Missing Connection Parameters in MySQLi Function Calls

Unlike the old mysql_* functions which used a global connection by default, every procedural mysqli_* function requires you to pass your database connection as the first parameter. For example:

Old mysql code:

$result = mysql_query("SELECT * FROM bookings");

Correct mysqli procedural code:

// Assuming $conn is your mysqli connection object from mysqli_connect()
$result = mysqli_query($conn, "SELECT * FROM bookings");

Or if you prefer the object-oriented approach (often cleaner):

$result = $conn->query("SELECT * FROM bookings");

Forgetting this parameter will throw an error that halts your script—this is likely why only the top of your page loads (the script dies before rendering the rest).

2. Enable Error Reporting to See Exact Issues

PHP 7.1 might suppress errors by default in your server config, so you can't see what's breaking your code. Add these lines at the very top of your script to enable full error display:

error_reporting(E_ALL);
ini_set('display_errors', 1);

This will show you specific errors like "mysqli_query() expects at least 2 parameters" or "undefined variable: conn", which point directly to what needs fixing.

3. Update Result Set Handling Functions

Functions for fetching data also have syntax changes:

  • mysql_fetch_assoc($result) → mysqli_fetch_assoc($result) (procedural) or $result->fetch_assoc() (OO)
  • mysql_num_rows($result) → mysqli_num_rows($result) (procedural) or $result->num_rows (OO)
  • mysql_fetch_array($result) → mysqli_fetch_array($result) (procedural) or $result->fetch_array() (OO)

For example, old code:

while ($row = mysql_fetch_assoc($result)) {
    // Do something with $row
}

Becomes:

// Procedural
while ($row = mysqli_fetch_assoc($result)) {
    // Do something with $row
}

// Or object-oriented
while ($row = $result->fetch_assoc()) {
    // Do something with $row
}

4. Verify Your Connection Setup

Make sure your MySQLi connection is properly established and error-checked. Old mysql code might have looked like this:

mysql_connect('localhost', 'user', 'pass');
mysql_select_db('booking_db');

Your MySQLi connection should include error handling and can directly specify the database:

// Procedural
$conn = mysqli_connect('localhost', 'user', 'password', 'booking_db');
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Object-oriented
$conn = new mysqli('localhost', 'user', 'password', 'booking_db');
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

A failed connection (even if you think it works) will cause all subsequent MySQLi calls to fail.

5. Double-Check Connection Closing

You mentioned updating the close command, but ensure you're using the right syntax:

  • Old mysql_close() → mysqli_close($conn) (procedural) or $conn->close() (OO)
    Just make sure you're closing the connection after all database operations are done, not before.

Next Steps:

  1. Add the error reporting lines to your script and reload the page—this will give you the exact error message and line number.
  2. Fix the specific issue (most likely missing connection parameters in a mysqli_query() call).
  3. Repeat until all errors are resolved.

This should get your booking site running fully on PHP 7.1!

内容的提问来源于stack exchange,提问作者Jacob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:24:15