[英]False value in sql query in php function only works in quotationmarks
我必須開發一個appStore插件,一個查詢無法正常工作。
功能
function addItem($installed){...
添加商店項目(一些html的東西)通過回聲進入div
<div class="flexRow order2 topLine">
<?php
addItem("false");
?>
</div>
和
<div class="flexRow order2 topLine">
<?php
addItem(true);
?>
</div>
數據選擇的查詢如下:
$sql = "SELECT * FROM shopitems Where installed = ". $installed ;
我的問題:正如您所看到的,錯誤的布爾值在引號中。 否則它將無法工作。 真實陳述可以是引號也可以不是。 我的查詢有問題嗎?
“已安裝”列來自布爾值。
網站上顯示的警告php如下:
mysqli_num_rows()期望參數1為mysqli_result,第31行的C:\\ xampp \\ htdocs \\ setupItems.php中給出布爾值
0結果
31號線至33號線:
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
<?php
整個功能
function addItem($installed )
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "appmarket";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
/*if($installed == true){
$sql = "SELECT * FROM shopitems Where installed = true ";
} else if ($installed == false){
$sql = "SELECT * FROM shopitems Where installed = false ";
}else{
error_log("forgot to enter installed bool");
}*/
$sql = "SELECT * FROM shopitems Where installed = ". $installed ;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo'<div id="1" class="blockItem order2"> <!-- this div contains an Table for the Content of the downloadabel app-->
<div>
<table id=itemTable style="table-layout:fixed"><!--this table contains the header and the version number-->
<tr>
<th id="itemHeader" class="itemheader"><h2>' . $row["Header"] . '</h2></th>
<td id="itemAppVersion" class="version">' . $row["Version"] . '</td>
</tr>
</table>
<table id=itemTable style="table-layout:fixed"><!--this table contains the image and the description-->
<tr class="tableformat">
<td id="itemAppThumbnail" width="120px">
<img class="itemThumbnail" src="Images/' . $row["Imageurl"] . '">
</td>
<td id="itemAppDescription" style="width: 200px;">
<a class="whiteAnchor" href="Downloadable/' . $row["Downloadurl"] . '" download="logo">
' . $row["Content"] . '
</a>
</td>
</tr>
</table>
</div>
</div>'
;
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
?>
問題源於不使用參數化語句而不檢查數據類型。
僅供參考,你應該看看的輸出
echo true;
打印“1”
echo false;
什么都不打印。
現在,在您的情況下,您嘗試將布爾值連接到字符串。 這是不可能的,因此PHP隱含地嘗試將您的布爾值轉換為字符串本身,就像回顯一樣。 false,在這種情況下,不會轉換為"false"
或"0"
,而是轉換為""
,這會導致您的查詢為:
"SELECT * FROM shopitems Where installed = "
這是無效的SQL。
作為快速解決方案,您可以使用
"SELECT * FROM shopitems Where installed = ". ($installed?"1":"0")
明確地將您的布爾值“強制轉換”為“0”並產生有效查詢。 從長遠來看,您應該查看參數化語句
有關更多信息,您還應該看看類型雜耍
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.