2013-11-20 30 views
0

IM試圖從數據庫數據的文本字段:ヶ輛()預計參數1是字符串

$fothersq=("SELECT others FROM january"); 
$fothers=mysql_query($fothersq); 
<input type="text" placeholder="0" name="febothers" size="11" value="<?php if(@$fothers){echo htmlentities(@$fothers);} ?>"> 

,但我得到的文本字段此錯誤信息。


Warning: htmlentities() expects parameter 1 to be string, resource given in C:\xampp\htdocs\CashFlow\febprev.php on line 116

也是另一個文本字段我用這個編碼只是爲了測試:

<input type="text" name="febbonus" size="11" placeholder="0" value="<?php if(@$fbonus){echo (@$fbonus);} ?>"> 

,我得到這個錯誤:

Resource id #7

什麼想法?

+2

請[RTFM用於'mysql_query'](http://php.net/mysql_query)及其實施例。 – deceze

+2

避免使用'@'符號。它已經讓你感到困惑。 –

回答

2

的錯誤告訴你問題是什麼:

Warning: htmlentities() expects parameter 1 to be string, resource given in C:\xampp\htdocs\CashFlow\febprev.php on line 116

這:

Resource id #7

無論這些錯誤的告訴你,mysql_query返回的資源。這一點不是一個字符串。您需要來處理該資源&就可以對其執行操作。嘗試這個。

$fothersq=("SELECT others FROM january"); 
$fothers=mysql_query($fothersq); 
while ($row = mysql_fetch_assoc($fothers)) { 
    echo sprintf('<input type="text" placeholder="0" name="febothers" size="11" value="%s">', (!empty($row['others'] ? htmlentities($row['others']) : ''))); 
} 

另外請注意,我把<input type="text"> HTML元素在echo既然你提供的代碼變得毫無意義,因爲直PHP或HTML或兩者的混合。

編輯:基礎上的評價是原來的海報可能是由三元運算符混淆,這裏是我的回答修改後的版本將達到同樣的目的,但在一個稍微簡單的辦法:

$fothersq=("SELECT others FROM january"); 
$result=mysql_query($fothersq); 
while ($row = mysql_fetch_assoc($result)) { 
    $fothers = ''; 
    if (!empty($row['others']) { 
    $fothers = htmlentities($row['others']); 
    } 
    echo '<input type="text" placeholder="0" name="febothers" size="11" value="' . $fothers . '">'; 
} 
要糾正
+1

IMO,sprintf和三元運算符使這個解決方案對於遇到這個基本問題的開發人員來說更加複雜。 – vonUbisch

+0

好點。在我的原始解決方案上添加了一個額外的變體來比較/對比 – JakeGould

+0

感謝您的意見。我通過這個得到了我想要的: $ fothersq =(「SELECT others FROM 1 year」); $ fothers = mysql_query($ fothersq); $ row = mysql_fetch_array($ fothers); 「> – user2971776

1

因爲我們是把這個

$fothers=mysql_query($fothersq); 

現在$fothers不會是一個字符串。它將是resource

但是您將$fothers傳遞到htmlentities()這需要一個字符串。

1

點:

  • ヶ輛()函數將字符轉換爲HTML entities.So它需要一個字符串凡在你的情況下,你的傳球資源。

  • 因爲您沒有取東西而得到Resource id #7。執行後,使用mysql_fetch_array來獲取該行。

嘗試這種情況:

$fothersq=("SELECT others FROM january"); 
$fothers=mysql_query($fothersq); 
while ($fetch = mysql_fetch_array($fothers)) { 
?> 
<input type="text" name="febothers" size="11" value="<?php echo (isset($fetch[0])) ? htmlentities("$fetch[0]") : ''?>"> 
<?php 
} 
相關問題