php函数中的sql查询中的false值仅适用于引号

时间:2016-11-24 14:09:38

标签: php mysql sql web

我必须开发一个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 ;

我的问题: 如您所见,false布尔值在引号中。否则它就不会起作用。真实陈述可以是引号也可以不是。我的查询有问题吗?

列&#34;已安装&#34;来自布尔值。

网站上显示的警告php如下:

  

mysqli_num_rows()期望参数1为mysqli_result,boolean   在第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);
}



?>

1 个答案:

答案 0 :(得分:3)

问题源于不使用参数化语句而不检查数据类型。

仅供参考,您应该查看

的输出
echo true;

打印&#34; 1&#34;

echo false;

没有打印任何内容。

现在,在您的情况下,您尝试将布尔值连接到字符串。这是不可能的,因此PHP隐含地尝试将您的布尔值转换为字符串本身,就像回显一样。 在这种情况下,false不会转换为"false""0",而是"",这会导致您的查询:

"SELECT * FROM shopitems Where installed = "

这是无效的SQL。

作为快速修复,您可以使用

"SELECT * FROM shopitems Where installed = ". ($installed?"1":"0")

显然&#39;演员&#39;你的布尔值为&#34; 0&#34;并产生有效的查询。 从长远来看,你应该考虑parameterized statements 有关详细信息,您还应该查看type juggling