简体   繁体   English

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

[英]Can not connect to my database server using PHP

I'm trying to build a profile page system for users bare in mind I'm real new to php. 我正试图为用户创建一个个人资料页面系统,而我却是php的新手。 In the code below, I get the error message I specified it to echo, when i cannot connect to my database ie : 在下面的代码中,当我无法连接到数据库时,即得到我指定为回显的错误消息,即:

"could not connect to server" “无法连接到服务器”

I dont get any other error messages, only this. 我没有收到任何其他错误消息,仅此而已。 I cant seem to find the problem, any help would be appreciated. 我似乎找不到问题,将不胜感激。

Here is my code : 这是我的代码:

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

It is specifically dying because of $conn - that doesn't appear to be set anywhere. 由于$conn ,它特别快要死了-似乎没有在任何地方设置。

However in saying that, as has been pointed out, you don't need to use mysqli_select_db in mysqli as it's all done in the mysqli_connect function. 然而,在说,作为已经指出的那样,你不需要使用mysqli_select_dbmysqli因为这一切都在做mysqli_connect功能。

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

That is enough - you only need the mysqli_db to change the current DB, but even then it needs a valid link identifier (the result returned by mysqli_connect ). 这就足够了-您需要mysqli_db更改当前数据库,但是即使那样,它也需要有效的链接标识符( mysqli_connect返回的结果)。

So, that would be: 因此,将是:

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

...as long as $conn is valid . ...只要$conn 有效

Sorry guys, the whole layout I was using was wrong, for making a live profile page, this new layout fixed the problems :)... 抱歉,我使用的整个布局是错误的,为了制作实时个人资料页面,此新布局解决了问题:)...

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

I forgot the current user's ID's are stored in $_SESSION when they log in, and is carried page to page on session_start() 我忘记了当前用户的ID在登录时存储在$ _SESSION中,并在session_start()上逐页携带

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM