2013-02-18 100 views
1

下面是代碼.........我一個,得到錯誤 - 警告:mysql_fetch_assoc()預計參數1是資源

<?php 
include('../inc/php/inc/dbc.php'); 
$query = "SELECT * FROM available_fsv WHERE a_status = '1'"; 
$result_query = mysql_query($query); 

while($row = mysql_fetch_assoc($result_query)){ 

     $billingid = $row['billingid']; 

$query = "UPDATE available_fsv SET b_status = '1' WHERE billingid = '$billingid'"; 
$result_query = mysql_query($query); 
echo $result_query; 
} 
?> 

我收到錯誤..... ......

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\php\fsv_shutdown_cron.php on line 6 

數據庫結構like--

____________________________________________________ 
| id | a_status | b_status | billingid | 
|--------|------------|-------------|--------------| 
| 1 | 1  |  0  |  1  | 
|--------|------------|-------------|--------------| 
| 2 | 0  |  0  |  12  | 
|--------|------------|-------------|--------------| 
| 3 | 0  |  0  |  9  | 
|--------|------------|-------------|--------------| 
| 4 | 1  |  0  |  3  | 
|________|____________|_____________|______________| 

我想做的事是,如果a_status是1,那麼更新b_status爲1

我正在學習PHP,我知道這是一個愚蠢的問題,但請幫助我。在此先感謝.. :)

+0

你確定你已經連接到db?在做'include'後試試'echo mysql_error' – 2013-02-18 09:17:32

+0

請在phpmyadmin上運行這個查詢並檢查結果。 – vin 2013-02-18 09:21:55

+0

你把'$ query'和'$ result_query'搞亂了,它們在代碼中出現兩次,看到我的回答在 – 2013-02-18 09:22:41

回答

0

此調用mysql_query($query)返回FALSE,而不是結果集,這意味着您的查詢中有錯誤。使用以下代碼查看錯誤:

$result_query = mysql_query ($query); 
if ($result_query === FALSE) 
{ 
    echo (mysql_error()); 
    die (1); 
} 

看起來我錯了。當您執行更新查詢時,您確實覆蓋原始值$result_query

0

,你可以,如果你正在執行這個使用內聯IF

UPDATE tableName 
SET b_status = IF(a_status = 1, 1 , b_status) 

直接更新,你並不需要在數據庫上兩次溝通。

+0

可能是他想要的,但如果billingid在表中重複,則不會執行完全相同的操作。 – 2013-02-18 09:25:18

1

你搞亂了$query$result_query,他們在代碼

錯誤是在while($row = mysql_fetch_assoc($result_query)){線出現twice ..這裏你$result_query不過你update查詢的響應..

試試這個:

<?php 
include('../inc/php/inc/dbc.php'); 
$query = "SELECT * FROM available_fsv WHERE a_status = '1'"; 
$result_query = mysql_query($query); 

while($row = mysql_fetch_assoc($result_query)){ 
    $billingid = $row['billingid']; 
    $update_query = "UPDATE available_fsv SET b_status = '1' WHERE billingid = '$billingid'"; 
    $update_result_query = mysql_query($update_query); 
    echo $update_result_query; 
    echo "<br />"; 
} 
?> 
+0

謝謝隊友..:D非常感謝... – 2013-02-18 09:56:07

+0

它工作完美嗎? – 2013-02-18 09:57:30

+0

是的...它的工作.... – 2013-02-18 10:09:58

相關問題