2015-09-19 82 views
3

當表格有10行時,我的PHP查詢返回零結果。實際顯示的是PHP表格格式代碼的一部分。當表有數據時,PHP MySQL查詢返回零結果

實際輸出:

0) { // output data of each row while($row = mysql_fetch_assoc($result)) { echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. " 
"; } } else { echo "0 re 
sults"; } mysql_close($conn);? 

HTML/PHP代碼:

<div class="about-text"> 

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "db"; 

// Create connection 
$conn = mysql_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysql_connect_error()); 
} 

$sql = "SELECT * FROM tablename"; 
$result = mysql_query($conn, $sql); 

if (mysql_num_rows($result) > 0) { 
    // output data of each row 
    while($row = mysql_fetch_assoc($result)) { 
     echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>"; 
    } 
} else { 
    echo "0 results"; 
} 

mysql_close($conn); 
?> 

</div> 
+0

您必須停止使用mysql_ - http://php.net/manual/en/mysqlinfo.api.choosing.php – hanshenrik

+0

退房http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – hanshenrik

回答

0

傳遞連接資源/實例作爲第一個參數是mysqli extension,你應該使用它的風格,而不是通過資源作爲第二個參數(可選) - 這是多麼老,現在deprecated mysql_* extension做它。 mysql_ *將會是be removed with the upcoming php version 7

<?php 
$mysqli = mysqli_connect('localhost', 'root', '', 'db'); 
if ($mysqli->connect_errno) { 
    die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error); 
} 

$sql = "SELECT `in`,`Name`,`Email` FROM tablename"; 
$result = mysqli_query($mysqli, $sql); 
if (!$result) { 
    die('query failed: '.$mysqli->error); 
} 

$row = mysqli_fetch_array($result); 
if (!$row) { 
    echo '0 results'; 
} 
else { 
    do { 
     echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>\r\n"; 
    } 
    while(NULL!=($row=mysqli_fetch_array($result))); 
} 
0

可以使用mysqli extension

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "your_db"; 
// Create connection 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
$sql = "SELECT * FROM mytable"; 
$result = mysqli_query($conn, $sql); 
if (mysqli_num_rows($result) > 0) { 
// output data of each row 
    while($row = mysqli_fetch_assoc($result)) { 
     echo "in: " . $row["id"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>"; 
    } 
} else { 
    echo "0 results"; 
} 
mysqli_close($conn); 
?> 
+0

在PHP支持的mysqli擴展4.4.4?這就是我正在使用的服務器上正在運行的內容。 – treponema

+0

Refere http://php.net/manual/en/book.mysqli.php –

0

試試這個代碼。這是您的confic.php文件:

<?php 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "database_name"; 

$connection = mysql_connect($hostname, $username, $password, $dbname); 

if (!$connection) { 

    die('Could not connect: ' . mysql_error()); 
} 

做出新的PHP文件,包括您的配置文件:

<?php 
include('config.php'); 
$sql = "SELECT * FROM table_name"; 
$result = mysql_query($sql); 

if (mysql_num_rows($result) > 0) { 

    while($row = mysql_fetch_assoc($result)) { 

     ?> 
      <div class="about-text"> 
       <?php 
        echo "In: ".$row["in"]."Name: ". $row['Name'].","."Email: ".$row['Email']."</br>"; 
       ?> 
      </div> 
     <?php 
    } 
}else{ 
    echo "There is no Result!!!!"; 
} 
mysql_close($connection); 

我認爲這會幫助你。