【发布时间】:2018-07-30 14:52:33
【问题描述】:
我正在尝试为用户构建个人资料页面系统,我是 php 的新手。在下面的代码中,当我无法连接到我的数据库时,我收到了我指定回显的错误消息,即:
“无法连接到服务器”
我没有收到任何其他错误消息,只有这个。我似乎找不到问题,任何帮助将不胜感激。
这是我的代码:
<?php
include_once 'Header.php';
?>
<?php
if (isset($_GET['user_uid']))
$user_uid = $_GET['user_uid'];
mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
mysqli_select_db($conn, "users") or die ("could not connect to server");
$userquery = mysqli_query("SELECT * FROM users where user_uid='$user_uid'")
or die ("The Query could not be completed, contact an administrator");
if (mysql_num_rows($userquery) != 1) {
die ("that username could not be found");
}
while ($row = mysqli_fetch_array($userquery, MYSQL_ASSOC)) {
$first = $row['first'];
$last = $row['last'];
$city = $row['city'];
$country = $row['country'];
}
?>
<table width="398" border="0" align="center" cellpadding="0">
<tr>
<td height="26" colspan="2">Your Profile Information </td>
<td><div align="right"><a href="index.php">logout</a></div></td>
</tr>
<?php echo $first; ?>
<tr>
<td width="129" rowspan="5"><img src="<?php echo $picture ?>" width="129"
height="129" alt="no image found"/></td>
<td width="82" valign="top"><div align="left">FirstName:</div></td>
<td width="165" valign="top"><?php echo $first ?></td>
</tr>
<tr>
<td valign="top"><div align="left">LastName:</div></td>
<td valign="top"><?php echo $last ?></td>
</tr>
<tr>
<tr>
<td valign="top"><div align="left">City:</div></td>
<td valign="top"><?php echo $city ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Country:</div></td>
<td valign="top"><?php echo $country ?></td>
</tr>
</table>
<p align="center"><a href="index.php"></a></p>
<?php
include_once 'Footer.php';
?>
【问题讨论】:
-
这看起来像是来自
mysql_的拙劣转换。mysqli_connect()返回连接,所以存储它。它还选择数据库,因此不需要mysqli_select_db()。您还需要各种其他 mysqli_ 调用的连接句柄。 -
而且
mysql_num_rows也死了……这似乎仍然保留在该代码中。 -
您收到该错误消息是因为您的错误报告代码是
die ("could not connect to server")。您可能想要mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);代替 (source)。 -
我没有看到
$conn被定义在任何地方 - 除非它是Header.php的一部分。