2015-04-23 72 views
-2
<?php 

    $connection = mysqli_connect("localhost", "gari" , "gari" , "onlinemarket"); 

    $call = "SELECT * FROM products WHERE id ='". 4 ."'"; 
    $result = mysqli_query($connection , $call); 

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

     $call = "UPDATE products SET stock = '". 666 ."' WHERE id ='". 4 ."'"; 
     $result = mysqli_query($connection , $call); 

    } 
    mysqli_close($connection); 
?> 

真正的問題是在$row = mysqli_fetch_array($result),收到這樣的:錯誤在PHP mysqli_fetch_array()

mysqli_fetch_array()預計參數1被mysqli_result,在C中給出布爾 :\ XAMPP \ htdocs中\第12行的tiendaonline \ php \ prueba.php我不知道 知道如何使用mysqli_error()來獲取錯誤。

謝謝。

+1

把$ if結果放在一個if if條件來檢查你的查詢是否執行。 –

回答

0

試試這個,

<?php 

    $connection = mysqli_connect("localhost", "gari" , "gari" , "onlinemarket"); 

    $call = "SELECT * FROM products WHERE id ='4'"; 
    $result = mysqli_query($connection , $call); 

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

     $call1 = "UPDATE products SET stock = '666' WHERE id ='4'"; 
     $result1 = mysqli_query($connection , $call1); 

    } 
    mysqli_close($connection); 

?> 

希望這有助於。

+0

檢查編輯的代碼!這對我來說可以! –

0

看起來像內循環取代$result,while循環沒有意義,如果你不使用$row內循環。請嘗試以下代碼

<?php 

$connection = mysqli_connect("localhost", "gari" , "gari" , "onlinemarket"); 

$call = "SELECT * FROM products WHERE id ='". 4 ."'"; 
$result = mysqli_query($connection , $call); 

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

$call = "UPDATE products SET stock = '". 666 ."' WHERE id ='". 4 ."'"; 
$resultUpdate = mysqli_query($connection , $call); 

} 
mysqli_close($connection); 
?> 

如果您只是需要更新記錄,則不需要選擇查詢,然後使用以下代碼。

$call = "UPDATE products SET stock = '". 666 ."' WHERE id ='". 4 ."'"; 
$resultUpdate = mysqli_query($connection , $call); 
mysqli_close($connection); 
0

嘗試這樣,

$resultUpdate = mysqli_query($connection, $call) 
     or die("Error: " . mysqli_error($connection)); 
0

在while循環,要重設$結果和由於在循環中你正在做一個更新查詢數據庫,輸出將是無論是真或假。 並且在循環的下一次迭代中,mysqli_fetch_array找不到任何要迭代的內容。嘗試這樣的:

. 
. 
. 
$result_select = mysqli_query($connection , $call); 
while ($row = mysqli_fetch_array($result)) 
{ 

    $call = "UPDATE products SET stock = '". 666 ."' WHERE id ='". 4 ."'"; 
    $result_update = mysqli_query($connection , $call); 
}