【问题标题】:Can not connect to my database server using PHP无法使用 PHP 连接到我的数据库服务器
【发布时间】: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 的一部分。

标签: php html mysql


【解决方案1】:

特别是因为 $conn 而死去 - 这似乎没有在任何地方设置。

不过,正如已经指出的那样,您不需要mysqli 中使用mysqli_select_db,因为这一切都在mysqli_connect 函数中完成。

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

这就足够了 - 你需要mysqli_db更改当前数据库,但即便如此它也需要一个有效链接标识符(mysqli_connect返回的结果)。

所以,那就是:

mysqli_select_db($conn, "users") or die ("could not connect to server");

...只要$conn 有效

【讨论】:

    【解决方案2】:

    对不起,我使用的整个布局是错误的,为了制作实时个人资料页面,这个新布局解决了问题:)...

    <?php
     include_once 'RoadieHeader.php';
     ?>
    
    <?php
    
    //PULLING OUT USERNAME//
    $result = mysqli_query($conn, "select user_uid from users where user_uid 
    ='".$_SESSION['u_uid']."'");
    
    $row = mysqli_fetch_array($result);
    
    $uid = ucfirst($row['user_uid']); 
    
    //PULLING OUT EVERYTHING!//
    $result_f =  mysqli_query($conn, "select user_first from users where 
    user_first ='".$_SESSION['u_first']."'");
    
    $row_f = mysqli_fetch_array($result_f);
    
     $first = ucfirst($row_f['user_first']);
    
    $last = ucfirst($_SESSION['u_last']);
    
    $city = ucfirst($_SESSION['u_city']);
    
    $country = ucfirst($_SESSION['u_country']);
    
     $about =  ucfirst($_SESSION['u_about']);
    
    ?>
    
    
    <table width="398" border="0" align="center" cellpadding="0">
      <tr>
         <td height="26" colspan="2"><?php echo $uid ?></td>
        <td><div align="right"><a href="index.php">logout</a></div></td>
      </tr>
      <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>
    
      <tr> 
        <td valign="top"><div align="left">About me: </div></td>
        <td valign="top">
          <?php echo $about ?>
         </td>
      </tr>
    
    </table>
    <p align="center"><a href="index.php"></a></p>
    
    
      <?php
      include_once 'RoadieFooter.php';
     ?>
    

    我忘记了当前用户的 ID 在他们登录时存储在 $_SESSION 中,并在 session_start() 上逐页进行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2017-08-19
      • 2020-10-24
      • 2018-10-27
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多