无法使用PHP连接到我的数据库服务器

时间:2018-07-30 14:52:34

标签: php html mysql

我正在尝试为用户创建一个个人资料页面系统,我根本不了解php。在下面的代码中,当我无法连接到数据库时,我得到了错误消息,将其指定为echo:

  

“无法连接到服务器”

我没有收到其他任何错误消息,仅此而已。我似乎找不到问题,将不胜感激。

这是我的代码:

<?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';
?>

2 个答案:

答案 0 :(得分:0)

具体$conn而死-似乎没有在任何地方设置。

但是,正如已经指出的那样,您不需要mysqli_select_db中使用mysqli,因为它们都是在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有效

答案 1 :(得分:0)

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

<?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()上逐页携带