簡體   English   中英

mysql_num_rows如何在mysqli中使用?

[英]How mysql_num_rows use in mysqli?

我有搜索查詢mysql,但由於SQL INJECTION我將我的代碼更改為MySqli。 我用了

if(mysql_num_rows($result)>= 1)

之前我把它改成了

if(($result->num_rows)>= 1)

我的問題是,即使在查詢中有匹配值,它總是回顯沒有結果。 這是為什么?

<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$result = $mysqli->query="SELECT *,SUM(unit_cost*quantity) AS total_amount FROM procurement WHERE counter LIKE '%".$search."%' 
OR item_description LIKE '%".$search."%' OR fund_source LIKE '%".$search."%' OR quantity LIKE '%".$search."%' OR mode_of_procurement LIKE '%".$search."%' 
OR division LIKE '%".$search."%' OR section LIKE '%".$search."%' GROUP BY counter";

if(($result->num_rows)<= 0){
echo "</br><div style='margin:0 auto;background:#fff;padding:3px;color:#2086e4;font-size:11px;font-weight:bold;text-align:center;'>
No Result for :<font color='red' style='text-transform:uppercase'>&nbsp; &nbsp;".$search."</div>";

//echo 'No Result for <font color="red">'.$search.'</font>';
}
if(($result->num_rows)>= 1){
echo'</br><div style="margin:0 auto;background:#fff;padding:3px;color:#2086e4;font-size:11px;font-weight:bold;text-align:center;">
Result for :<font color="red" style="text-transform:uppercase">&nbsp; &nbsp;'.$search.'</font></div>';
    echo"<div id='id3' style='margin-top:10px;'><table id='tfhover' class='tftable' border='1 style='text-transform:uppercase;'>
        <thead>
        <tr>
        <th></th><th>Item Description</th>
        <th>Fund Source</th>
        <th>Unit</th>
        <th>Unit Cost</th>
        <th>Quantity</th>
        <th>Total Amount</th>
        <th>Mode of Procurement</th>
        <th>Supplier</th>
        <th>P.O #</th>
        <th>P.O Date</th>
        <th>Division</th>
        <th>Section</th>
        </tr></thead><tbody>";

    while($row = $result->fetch_assoc()){
echo'<tr>
        <td><a href="'.$_SERVER['PHP_SELF'].'?pn='.$row["counter"].'" onclick="return confirm(\'Really want to delete?\');"><img src="images/del.png" border"0" width="15" height="15"></a></td>
        <td>'.$row['item_description'].'</td>
        <td>'.$row['fund_source'].'</td>
        <td>'.$row['unit'].'</td>
        <td>'.$row['unit_cost'].'</td>
        <td>'.$row['quantity'].'</td>
        <td>'.$row['total_amount'].'</td>
        <td>'.$row['mode_of_procurement'].'</td>
        <td>'.$row['supplier'].'</td>
        <td>'.$row['po_number'].'</td>
        <td>'.$row['po_date'].'</td>
        <td>'.$row['division'].'</td>
        <td>'.$row['section'].'</td></tr></tbody>';
}
}
else{
}
?>

您正在$mysqli對象上設置query屬性,而不是調用query() 方法 采用

$result = $mysqli->query("some sql");

代替。

此外,以這種方式使用任何數據庫模塊都可以(並且可能會)導致SQL注入。 您仍在按字符串連接構建查詢字符串; 無論是mysqlmysqli還是其他東西,通過這條路線你總會有SQL注入漏洞。

你只是設法弄錯了一切。

首先,您沒有使您的代碼安全。 “Mysqli”不是一個神奇的頌歌,只是通過它的存在使一切安全。 為了確保您的代碼安全,您必須使用預准備語句

其次,您根本不需要任何行數。 你應該放棄將HTML與SQL混合的舊方法,並學習使用一些模板 使用模板,您無需專用功能即可知道是否有任何行。

step1:回聲如下:

echo $result = $mysqli->query="SELECT *,SUM(unit_cost*quantity) AS total_amount FROM procurement WHERE counter LIKE '%".$search."%' 
OR item_description LIKE '%".$search."%' OR fund_source LIKE '%".$search."%' OR quantity LIKE '%".$search."%' OR mode_of_procurement LIKE '%".$search."%' 
OR division LIKE '%".$search."%' OR section LIKE '%".$search."%' GROUP BY counter";

comnt所有其他行並運行,復制輸出並粘貼到phpmyadmin的sql區域。 如果顯示任何結果,則表示僅與numrow條件相關的問題。 在此之前,請確保您有條件顯示數據。 (如果dar在SQL中沒有輸出,並且您有要為搜索鍵顯示的數據,則PHP中的SQL提取語句出現問題)

2:現在改變第一個回聲並將其添加到numnow obj中,如: echo $xxx=$result->num_rows; 做同樣的程序

if($xxx==0) {Action 1} 
elseif($xxx>=1){Action 2}
else{Action 3}

快樂的編碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM