2013-07-12 48 views
-1

我遇到麻煩的行數從PHP MySQL查詢回來。如何返回行數

的鏈接test page和PHP代碼如下:

<?php 

$con=* TESTED AND WORKING * 

// Check connection 

if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 


$retDiv = ""; 

$counter = 0; 

$result = mysqli_query($con,"SELECT * FROM `store` ORDER BY `num` DESC "); 
$num_rows = mysql_num_rows($result); 

while($row = mysqli_fetch_array($result)) 
{ 
    if ($counter>=5) 
    { 
     break; 
    } 
    else 
    { 
     $retDiv = "<div id=\"hs" . $row['num'] . "\" class=\"store-item " . $row['Artist'] . "\">\n"; 
     $retDiv = $retDiv . "<div id=\"". $row['Release'] . "-title\" class=\"album-title\">\n"; 
     $retDiv = $retDiv . "<h1 align=\"center\" style=\"text-transform:uppercase;\">" . $row['Release'] . "</h1></br>\n"; 
     $retDiv = $retDiv . "<h3 align=\"center\">" . $row['ReleaseName'] . "</h3>\n"; 
     $retDiv = $retDiv . "</div>\n"; 
     $retDiv = $retDiv . "<div id=\"" . $row['Release'] . "-description\" class=\"description\">\n"; 
     $retDiv = $retDiv . "<table width=\"100%\">\n"; 
     $retDiv = $retDiv . "<td width=\"50%\" style=\"padding-left:57px;padding-right:57px;padding-top:57px;padding-bottom:20px;\">\n"; 
     $retDiv = $retDiv . "<a id=\"" . $row['Release'] . "DivLink\" href=\"javascript:;\" onmousedown=\"toggleDivStore('" . $row['Release'] . "-expand','" . $row['Release'] . "DivLink','" . $row['Release'] . "','" . $row['Release'] . "b','" . $row['Release'] . "')\"><img name=\"" . $row['Release'] . "-art\" src = \"images/" . $row['Release'] . ".jpg\" class=\"album-art\" onmouseover=\"this.src='images/" . $row['Release'] . "b.jpg'\" onmouseout=\"this.src='images/" . $row['Release'] . ".jpg'\"></a>\n"; 
     $retDiv = $retDiv . "<td width=\"50%\" style=\"padding:20px;padding-top:0px;padding-bottom:0px;padding-left:0px;\">\n"; 
     $retDiv = $retDiv . "<p style=\"\">" . $row['Description'] . "</p>\n"; 
     $retDiv = $retDiv . "</td>\n"; 
     $retDiv = $retDiv . "</table>\n"; 
     $retDiv = $retDiv . "</div>\n"; 
     $retDiv = $retDiv . "<div class=\"album-expand\" id=\"RELEASE-expand\" style=\"display:none;\">\n"; 
     $retDiv = $retDiv . "</div>\n"; 
     $retDiv = $retDiv . "<div =\"" . $row['Release'] . "PaypalContainer\" class=\"album-paypal-container\">\n"; 
     $retDiv = $retDiv . $row['Paypal1']; 
     $retDiv = $retDiv . "</div>\n"; 
     $retDiv = $retDiv . "</div>\n"; 

     echo $retDiv; 
    } 
    $counter = $counter+1; 
} 


echo "<div id=\"retNum\"><script type=\"text/javascript\">numReleases =" . $num_rows . ";numPages=Math.ceil(numReleases/5)-1;</SCRIPT></div>"; 

mysqli_close($con); 

如何行數爲$num_rows返回retNum的Javascript?

+0

'mysql_num_rows($結果);'用'mysqli_num_rows($結果);' – crush

+0

謝謝!我應該刪除這個問題嗎? – cthemc

回答

0

你在混合mysqli(注意i)和mysql(注意一個i)的LACK。這兩個庫及功能機構都互操作:

$num_rows = mysql_num_rows($result); 
       ^--- missing an `i` here 
+0

謝謝馬克!我應該刪除這個問題嗎?第一次抱怨一個愚蠢的問題。我不知道有什麼區別 – cthemc

+0

這取決於你。雖然,刪除一個問題得到的答案在某種程度上影響了你的帳戶。 –

+0

好吧,我會離開它並標記答案。再次感謝 ! – cthemc

0

當您使用mysqli_query()方法得到的結果從數據庫設置,可以通過調用mysqli_num_rows(獲得行從結果數))方法,該方法取mysqli_result設定爲參數,並沒有使用mysql_num_rows(。檢查mysqli_num_rows()

0

而不是使用PHP邏輯打出來,爲什麼不加「LIMIT 5」你的SQL查詢?

+0

我試過,但我喜歡返回表中的行總數返回的JavaScript。我想我可以單獨使用計數。感謝您的輸入! – cthemc

+0

爲了降低MySQL服務器上的負載,您可以使用「SELECT COUNT(*)FROM'store'」獲得的總記錄數,然後使用LIMIT子句限制後續頁面。 – dar7yl