2017-12-26 15 views
-1

を変更した後、私はMySQLのテーブルのすべての最初の10の項目を返すようにしたい表示されます無効な引数は()SQL構文

警告:私は私のページを実行すると

class Products { public function __construct() { $this->db = new DBH(); $this->db = $this->db->dbConnect(); } public function GetProduct($id_number,$result_num) { if(!empty($id_number)&&!empty($result_num)) { $pro = $this->db->prepare("select * from products LIMIT " . $id_number . ", " . $result_num); $pro->execute(); $pro_array = array(); while($row = $pro->fetch()) { $pro_array[] = $row; } return $pro_array; } } } 

が、私はこのエラーを取得するforeachのために供給無効な引数()このエラーを返す とのforeach()はこれです:

foreach($productShow as $product){ 
    echo " 
    <tr> 
     <td>".$product['product_id']."</td> 
     <td><a href='product.php?product_id=".$product['product_id']."'>".$product['product_title']."</a></td> 
     <td><a href='product.php?product_id=".$product['cat_id']."'>".$product['category_name']."</a></td> 
     <td><a href='product.php?product_id=".$product['brand_id']."'>".$product['brand_name']."</a></td> 
     <td>".$product['date']."</td> 
     <td><a href='deleteproduct.php?id=".$product['product_id']."'><span class='label label-danger'>REMOVE</span></a></td> 
    </tr>"; 
} 

奇妙な事実は、私が$productSet->GetProduct(0,10);$productSet->GetProduct(1,10);にこのエラーを変更するたびに消えると、ということである第一1つのショーを除くすべての製品アップ。

テーブルからすべての項目を戻したいので、最初の項目も結果として表示されるように、0から行を開始する必要があります。このエラーメッセージが表示されないときにこれを行う方法は?

+2

if(!empty($ id_number))$ idが '0'ならば、それは空と同じです – halojoy

+0

@halojoyに 'empty 'ではなく' isset'を追加することを追加します – Sablefoste

+0

$ ($ row = $ result-> fetch()) '? – RamRaider

答えて

0

あなたの問題は、PHPが値0を空とみなしていることにあります。 PHP空のドキュメントから:

The following things are considered to be empty:

"" (an empty string) 
0 (0 as an integer) 
0.0 (0 as a float) 
"0" (0 as a string) 
NULL 
FALSE 
array() (an empty array) 
$var; (a variable declared, but without a value) 

代わりの!empty($id_number)$id_number >= 0を試してみてください。