2012-04-09 13 views
0

私はコーディングn00bです。私はPHP Whileループヘルプ - 「警告:mysqli_fetch_assoc()は、パラメータ1がmysqli_result ......と期待しています」

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/jpl8ce/public_html/php_grocery1.php on line 20 

ここでは私の.phpと私がやっていると行く.htmlファイルがあるがこれを開こうとすると、私は、このエラーメッセージが出続けます。基本的には、店舗の営業時間、住所、ユーザーが入力したアイテムを持つストアの名前を印刷しようとしている食料品データベースです。

<html> 
<head> 
<title>Grocery Results!</title> 
</head> 
<body> 
<h1> 
<?php 
$connect = mysqli_connect("localhost","jpl8ce","[email protected]","grocery"); 
$item_name = $_post["input1"]; 

$query_String = " SELECT Stores.Name, Price, Address, Open_Time, Close_Time 
FROM (Stores JOIN Carry ON Stores.Store_ID = Carry.Store_ID) JOIN Items ON Carry.Product_ID = Items.Product_ID 
WHERE Items.name = $item_name 
ORDER BY Close_Time DESC"; 


$result = mysqli_query($connect, $query_String); 

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

echo '<P>'.$row["Stores.Name"].', '.'</P>'; 
//echo '$' number_format($row["Price"],2).', '; 
echo '<P>'.$row["Address"] .', '.'</P>'; 
echo '<P>'.$row["Open_Time"] .', '.'</P>'; 
echo '<P>'.$row["Close_Time"].'</P>'; 
} 

mysqli_close($connect); 
?> 
</h1> 
</body> 
</html> 

は、ここに私のHTMLコード

<html> 
<head> 
<title>Grocery Database Search</title> 
</head> 
<body> 
<H1> Grocery Database Search! </H1> 
<IMG SRC='grocery_cart_2.jpg'/> 
<P> Use this Search Engine to input a name of an item. 
After clicking the Search button, the price of the item, as well as the address and hours of the store 
the item is located at will be displayed. </P> 

<form action="php_grocery1.php" method="POST"> 
<p>Item: <input type="text" name="input1"/></p> 
<p><input type="submit" value="Search!"/></p> 
</form> 
</body> 
</html> 

おかげで再び男です!

+2

**検索を使用してください** http://stackoverflow.com/search?q=mysqli_fetch_assoc%28%29+expects+parameter+1 + to + be + mysqli_result%2C + boolean + given – deceze

+1

[警告の問題:パラメータ1がmysqli_resultになると予想されます](http://stackoverflow.com/questions/2077263/warning-problem-expects-parameter-1-to-be-mysqli-result) – deceze

+0

こんにちは、私はそれが重複するので、これに対して別の質問を開くことはできません。しかし私はどんな場所でも答えを見つけられませんでした。私はウェブ上で見つけたすべてをやろうとしましたが、それは助けになりませんでした。私に何ができる?私はまだフェッチエラーを取得しています。 – aleXela

答えて

1

は、私はあなたのコードでいくつかの問題を参照してください。

  • すべてのスーパーグローバルは大文字でなければなりません。 $_post["input1"];$_POST["input1"];に置き換えます。
  • あなたのクエリはSQLインジェクション攻撃に対して脆弱です。ユーザーが送信したデータをクエリに配置する場合は、mysql_real_escape_string()see PHP doc here)のようなエスケープ機能を使用します。正確に問題を検出するために
  • また、SQLクエリに渡された文字列は単一引用符内でなければなりませんが(\'は、以下の引用符をエスケープに注意してください)
  • (おそらくこのように、あなたはエラー処理コードを追加する必要があり、何が起こっています:。!

    <html> 
        <head> 
         <title>Grocery Results!</title> 
        </head> 
        <body> 
         <?php 
          if(!$connect = mysqli_connect("localhost","jpl8ce","[email protected]","grocery")) 
           die('Error connecting to DB!: ' . mysql_error()); 
    
          $item_name = $_POST["input1"]; 
    
          $query_String = 'SELECT Stores.Name, Price, Address, Open_Time, Close_Time 
           FROM (Stores JOIN Carry ON Stores.Store_ID = Carry.Store_ID) JOIN Items ON Carry.Product_ID = Items.Product_ID 
           WHERE Items.name = \'' . mysql_real_escape_string($item_name) . '\' ORDER BY Close_Time DESC'; 
    
          if(!$result = mysqli_query($connect, $query_String)) 
           die('Error on query: ' . mysql_error($connect)); 
    
          while ($row = mysqli_fetch_assoc($result)) { 
           echo '<P>' . $row["Stores.Name"] . ', ' . '</P>'; 
           //echo '$' number_format($row["Price"],2).', '; 
           echo '<P>' . $row["Address"] . ', ' . '</P>'; 
           echo '<P>' . $row["Open_Time"] . ', ' . '</P>'; 
           echo '<P>' . $row["Close_Time"] . '</P>'; 
          } 
    
          mysqli_close($connect); 
         ?> 
        </body> 
    </html> 
    

・ホープ、このことができます

+1

あなたがすでに 'mysqli'を使っているならば、あなたは自由にバインド変数を取得します。 mysql_real_escape_stringの代わりにそれらを使用してください。さらに、出力前にデータをエスケープする必要があります。この場合、 'htmlspecialchars'を使用してください。 – DCoder

関連する問題