2017-03-03 95 views
0

我正在從數據庫中提取圖像。我寫了下面給出的PHP腳本,只有while循環工作中的「echo」語句,但變量不顯示來自數據庫的值。換句話說,只有html正在工作。Mysqli準備查詢不起作用

這裏沒有任何錯誤。 我不知道這是什麼問題。

幫幫我。 謝謝!

$db_username = 'xxxxx'; 
$db_password = 'xxxx'; 
$db_name = 'xxxx'; 
$db_host = 'localhost'; 
$item_per_page = 9; 

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name); 
if ($mysqli->connect_error) { 
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); 
} 

$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); 

if(!is_numeric($page_number)){ 
    header('HTTP/1.1 500 Invalid page number!'); 
    exit(); 
} 

$position = (($page_number-1) * $item_per_page); 

$results = $mysqli->prepare("select * FROM inserting_discount_product ORDER BY id DESC LIMIT ?, ?"); 

$results->bind_param("dd", $position, $item_per_page); 

$results->execute(); //Execute prepared Query 

$results->bind_result($pro_id, $pro_title, 
$pro_cat,$pro_desc,$pro_price,$pro_img,$pro_discount_price,$pro_discount_percent 
age,$product_code); 


while($results->fetch()){ 
       echo " 
       <div id='$pro_id' class='col-md-3 col-sm-4 col-xs-12'> 

       <h3 style='color:black'; align='center'>$pro_title</h3> 
       <div> 

       <img src='../admin_area/product_images/product_pics/$pro_img' width='180' height='180' /> <br> 
       </div> 


       <p style='color:black;margin-right:12px'><b>Price: $ $pro_discount_price </b> 

       <span style='color:black;text-decoration:line-through;margin-left:15px'> 
       <b style='color:red'>Price: $ $pro_price</b> 
       </span> 

       </p> 

       <a href='details.php?pro_id=$pro_id' style='margin-right:10px' class='detail_hover'><b>Details</b></a> 
       <span style='font-weight:bolder'>$pro_discount_percentage %</span> 
       <a href='index.php?add_cart=$pro_id&product_code=$product_code' style='color:orange;margin-left:10px' class='basket-logo'> 
       <span class='fa fa-shopping-cart fa-2x'></span></a> 


        </div> 

       "; 

} 

回答

0

將您的prepare語句放入IF中是一種很好的做法,因爲如果失敗,prepare可以返回一個布爾值。

if($results = $mysqli->prepare("select * FROM inserting_discount_product ORDER BY id DESC LIMIT ?, ?")) 
     { 
      $results->bind_param("dd", $position, $item_per_page); 

      $results->execute(); //Execute prepared Query 

      $results->bind_result($pro_id, $pro_title, 
      $pro_cat,$pro_desc,$pro_price,$pro_img,$pro_discount_price,$pro_discount_percentage,$product_code); 

      while($results->fetch()) 
      { 
       // ... stuff 
      } 
      $results->close(); 
     } 
     else { echo $mysqli->error; } 

您應該也可能顯式聲明在SELECT中的返回列而不是使用*。在這一行中,您可能會在表中添加一列,並且如果您正在執行SELECT *,則所有代碼都會中斷,因爲查詢將返回的列數多於bind_result中的變量數。

+0

與您建議的一個relacing語法後加入的,而不是列名「*」我得到這個錯誤 「用盡67108864個字節允許內存大小(試圖分配4294967296個字節)」 –

+0

好吧,我已經看到了在此之前的錯誤。原因是你的表的定義,你試圖選擇的一個列是一個非常大的類型。我在使用LONGTEXT類型代替TEXT時得到了它。 –

-1

在這裏看到:

What is the difference between single-quoted and double-quoted strings in PHP?

這與你的echo語句您的報價問題。你有一個雙引號和單引號的組合。在PHP單引號將顯示的東西,因此你的變量不會被顯示。因此,您的回聲聲明需要分解爲多個串接,以便正確顯示您想要的內容。請注意,PHP使用點運算符來連接。

例如:

echo "<div id='".$pro_id."' class='col-md-3 col-sm-4 col-xs-12'> 

的原因有沒有任何錯誤,因爲它假定所有的變量,你是呼應了應呼應純文本所以它字面上寫「$ pro_id」來html作爲元素的id而不是變量是什麼。

或者,您的標記是很多html。您可以將您的整個while循環爲HTML和使用PHP的簡碼,讓您的變量,在環如下:

while($results->fetch()){ 
    //End php here, go into HTML markup. 
    ?> 

    <div id='<?=$pro_id?>' class='col-md-3 col-sm-4 col-xs-12'> 

    ... 

    <?php 
//Don't forget to add the closing brace to the while loop back in <?php 
} 

此方法使用PHP的速記符號在這裏看到:在HTML標記這回聲PHP變量的頁面,而。這減少了你必須處理的引號和字符串的數量,並且更容易處理,因爲這是直接的HTML。如果大多數內容將使用HTML(它是),並且您只需添加一些PHP變量,那麼我肯定會推薦關閉您的PHP?>並使用簡寫。

+0

只是爲了記錄:沒有編輯可以挽救這個答案,這與問題*和*本身是錯誤的無關 –