这段代码有什么问题?

时间:2014-04-29 14:10:06

标签: php mysql sql

我写这个但它不起作用,找不到错误。 这个服务器端代码得到变量$ cpu& $ display并从数据库中选择使用它。当变量不重要时,将发送“*”。

<?php
if (isset($_REQUEST['action']))
{
    $action = $_REQUEST['action'];
}
else
{
    echo "Invalid Data";
    exit;
}

if ($action == "read")
{
    readData();
}

function connectToDatabase()
{
    $connection = mysqli_connect("localhost", "root", "", "project_pro");
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    return $connection;
}

function readData()
{
    $connection = connectToDatabase();
    $cpu = $_REQUEST['cpu'];
    $display = $_REQUEST['display'];

这是存在问题的部分:

 $sql = "Select * From phones WHERE";

if ($cpu == "*")
{
}
else
{
    $sql+= " phone_cpu='$cpu'";
}

if ($display == "*")
{
}
else
{
    $sql+= " AND phone_display='$display'";
}

$output = array();

while ($row = mysqli_fetch_array($result))
{
    $record = array();
    $record['phone_id'] = $row['phone_id'];
    $record['phone_cpu'] = $row['phone_cpu'];
    $output[] = $record;
}

echo json_encode($output);
mysqli_close($connection);
}

1 个答案:

答案 0 :(得分:7)

PHP中的连接运算符是.而非+。因此,将+=更改为.=