[英]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.