2013-02-12 101 views
0

我想使用php從mysql表中獲取數據。請,誰能告訴我?這段代碼有什麼問題?什麼是正確的代碼,以從MySQL數據庫獲取數據:我怎樣才能從數據庫獲取數據在PHP中使用MySQL?

<?php 
    $dbhost = 'localhost'; 
    $dbuser = 'root'; 
    $dbpass = ''; 
    $connect=mysql_connect("localhost","root",""); 

    // connect to databsase 

    mysql_select_db("form1",); 

     enter code here 

    // query the database 

    $query = mysql_query("SELECT * FROM users WHERE name = 'Admin' "); 

    // fetch the result/convert resulte in to array 

    WHILE ($rows = mysql_fetch_array($query)): 

     $rows = $rows['Name']; 
     $address = $rows['Address']; 
     $email = $rows['Email']; 
     $subject = $rows['Subject']; 
     $comment = $rows['Comment'] 

     echo "$Name<br>$Address<br>$Email<br>$Subject<br>$Comment<br><br>"; 

     endwhile; 

     ?> 
+0

什麼錯誤,你得到些什麼? – amitchhajer 2013-02-12 07:19:19

+0

您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068)並應使用[現代替換](http://php.net/manual/en/ mysqlinfo.api.choosing.php)。 – Quentin 2013-02-12 07:19:59

+0

當你使用'mysql_connect(「localhost」,「root」,「」);''時,聲明上述變量的原因是什麼?並告訴我們你從這段代碼中得到的結果 – 2013-02-12 07:21:06

回答

0

嘗試

$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ")or die(mysql_error());

,並檢查是否該拋出任何錯誤。

然後使用while($rows = mysql_fetch_assoc($query)):

最後顯示爲

echo $name . "<br/>" . $address . "<br/>" . $email . "<br/>" . $subject . "<br/>" . $comment . "<br/><br/>" . ;

不要用戶mysql_*作爲其棄用。

4

php中的變量區分大小寫。請以下替換while循環:

while ($rows = mysql_fetch_array($query)): 

      $name = $rows['Name']; 
      $address = $rows['Address']; 
      $email = $rows['Email']; 
      $subject = $rows['Subject']; 
      $comment = $rows['Comment'] 

      echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>"; 

      endwhile; 
0

使用此代碼

while ($rows = mysql_fetch_array($query)): 

    $name = $rows['Name']; 
    $address = $rows['Address']; 
    $email = $rows['Email']; 
    $subject = $rows['Subject']; 
    $comment = $rows['Comment']; 

    echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>"; 

    endwhile; 

    ?> 
0
  1. 選擇帶有標識mysql_select_db數據庫( 「form1的」,$連接);

  2. 你會得到語法錯誤嗎?如果請放一個;在$ comment = $ rows旁邊['Comment']。

  3. 而且變量應區分大小寫這裏

1

試試這個

<?php 
// 1. Enter Database details 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = 'password'; 
$dbname = 'database name'; 

// 2. Create a database connection 
$connection = mysql_connect($dbhost,$dbuser,$dbpass); 
if (!$connection) { 
    die("Database connection failed: " . mysql_error()); 
} 

// 3. Select a database to use 
$db_select = mysql_select_db($dbname,$connection); 
if (!$db_select) { 
    die("Database selection failed: " . mysql_error()); 
} 

$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' "); 

while ($rows = mysql_fetch_array($query)) { 
    $name = $rows['Name']; 
    $address = $rows['Address']; 
    $email = $rows['Email']; 
    $subject = $rows['Subject']; 
    $comment = $rows['Comment'] 

    echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";  
} 

?> 

未測試! *更新!

0

如果這是你的代碼,那麼你會得到一個錯誤,因爲你在循環中重新分配$ row,所以你永遠無法迭代結果。更換

$rows = $rows['Name']; 

$name = $rows['Name']' 

所以,你的代碼看起來像

WHILE ($rows = mysql_fetch_array($query)): 

    $name = $rows['Name']; 
    $address = $rows['Address']; 
    $email = $rows['Email']; 
    $subject = $rows['Subject']; 
    $comment = $rows['Comment']; 

而且我假設在用戶表中的列名的名稱,地址,電子郵件等,並不是姓名,地址,電子郵件。請記住,每個變量名稱/字段名稱都區分大小寫。

0

你的語法是錯誤的......正確的編碼是:

<?php 
mysql_connect("localhost","root",""); 
mysql_select_db("form1"); 
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' "); 
while($rows = mysql_fetch_array($query)) 
{ 

    $rows = $rows['Name']; 
    $address = $rows['Address']; 
    $email = $rows['Email']; 
    $subject = $rows['Subject']; 
    $comment = $rows['Comment'] 
    echo $rows.'</br>'.$address.'</br>'.$email.'</br>'.$subject.'</br>'.$comment; 
} 
    ?> 
1

更改「而」來「而」。因爲PHP是區分大小寫的像c/C++。

0

代碼:

while ($rows = mysql_fetch_array($query)): 
     $name = $rows['Name']; 
     $address = $rows['Address']; 
     $email = $rows['Email']; 
     $subject = $rows['Subject']; 
     $comment = $rows['Comment'] 
     echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>"; 
endwhile; 
1
<table border="1px"> 

    <tr> 
     <th>Student Name</th> 
     <th>Email</th> 
     <th>password</th> 
    </tr> 

     <?php 

      If(mysql_num_rows($result)>0) 
      { 
       while($rows=mysql_fetch_array($result)) 
       { 

     ?> 
    <?php echo "<tr>";?> 
        <td><?php echo $rows['userName'];?> </td> 
        <td><?php echo $rows['email'];?></td> 
        <td><?php echo $rows['password'];?></td> 

    <?php echo "</tr>";?> 
     <?php 
       } 
      } 

    ?> 
</table> 
    <?php 
     } 
    ?> 
+1

添加一些解釋並回答這個答案如何幫助OP解決當前問題 – 2016-10-21 05:15:38

相關問題