2013-07-24 136 views
-3

嘿,我是新來的PHP/MySQL,我試圖執行一個非常簡單的PHP代碼,將顯示錶的內容。我覺得代碼是完美的,我沒有收到錯誤消息,但由於某種原因,它不起作用。我知道你們討厭調試這樣的問題,但如果你可以幫助我會很感激的,但是 。這裏的PHP。使用php連接到mysql中的數據庫使用php

<?php 

$conn=mysql_connect("localhost","demo","abc") or die(mysql_error()); 
    mysql_select_db("practice"); 

    $sql="SELECT*FROM contact"; 

    $result=mysql_query($sql,$conn) or die(mysql_error()); 

    while($row=mysql_fetch_assoc($result)){ 
    foreach($row as $name => $value){ 
     print "$name: $value <br>\n"; 
       } //end foreach 
       print "<br /> \n"; 
       } //end while 



        ?> 
+0

'我覺得代碼perfect'你使用mysql_ *它是如何完美。 –

+1

使用mysqli稍微更完美 – tenstar

+0

那麼你的SQL語句有語法錯誤。你沒有辦法得到任何錯誤。 – thatidiotguy

回答

1

您使用的是舊版本的MySQL庫,它是一個沒有任何

獲得舒適與Mysqli Extension您所有的數據庫訪問需求。我甚至會爲你重構這一點。

$conn = new Mysqli('localhost', 'demo', 'abc', 'practice'); 

$sql = "SELECT*FROM contact"; 

$results = $conn->query($sql); 

while($row = $results->fetch_assoc()) 
{ 
    var_dump($row); 
} 

編輯:JimiDini發佈了一個你應該閱讀的鏈接。 http://phptherightway.com/

0

試試這個

 // Report simple running errors 
    error_reporting(E_ERROR | E_WARNING | E_PARSE); 

    // or if you want to enable all PHP error reports, use this code below and comment out the one above 
    //error_reporting(-1); 

    $dbhost = 'localhost';// Server name (usually localhost) 
    $dbuser = 'user';// SQL Username (Make sure the user has access to the database!). 
    $dbpass = 'password';// SQL Password. 
    $dbase = 'db name';// SQL Database Name. 

    //connection to the database 
    $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error()); 


    $sql = "SELECT * FROM `contact`"; 

    $result = mysql_query($sql); 

    while($row = mysql_fetch_assoc($result)){ 
     print_r($row); 
    } 

,但你應該使用mysqli未來

+0

是的,當我運行該代碼,我什麼也得不到。不是一個錯誤消息,只是一個空白的屏幕。我想我應該切換到mysqli ....我認爲這個問題必須與數據庫/用戶配置,而不是代碼。感謝您的幫助! – user2458161

+0

你的代碼連接到數據庫?嘗試並回顯$ sql語句。它走得那麼遠?你的顯示錯誤消息是否在php.ini中啓用? –

+0

不,它不會回顯$ sql語句。我在哪裏可以找到php.ini文件夾。我用xampp來安裝所有的東西 – user2458161