mysql_query SELECT没有给出想要的结果

时间:2013-01-27 18:22:22

标签: php mysql

以下代码始终显示

    rows = 0

即使该表在字段'to'中包含Ravi。有谁知道这段代码有什么问题?

    <?php
    $response = array();
    $con = mysql_connect("localhost","root","");
    if(!$con) {
        die('Could not connect: '.mysql_error());
    }
    mysql_select_db("algopm1",$con);
    //if (isset($_POST['to'])) {
        $to = "Ravi";
        $result = mysql_query("SELECT *FROM `events` WHERE to = '$to'");
        if (!empty($result)) {
            if (mysql_num_rows($result)>0) {
                $result = mysql_fetch_array($result);
                echo $result["to"] + " " + $result["from"];
            } else {
                echo 'rows = 0';
            }
        } else {
            echo 'empty for Ravi';
        }
    //} else {
    //}
    ?>

5 个答案:

答案 0 :(得分:6)

to在MySQL中是reserved word,如果您想使用它,则必须将其包含在反引号中:

.... WHERE `to` = ...

答案 1 :(得分:0)

我没有查看您的代码,但我建议您查看

http://php.net/manual/en/intro.mysql.php

这之前你继续使用mysql而不是mysqli。它没有那么不同,但mysqli似乎有一个包装在mysql上并使用“ - &gt;”实例化连接的新类。如果这是有道理的。

答案 2 :(得分:0)

试试这个:

<?php
$response = array();
$con = mysql_connect("localhost","root","");
if(!$con) {
    die('Could not connect: '.mysql_error());
}
mysql_select_db("algopm1",$con);
//if (isset($_POST['to'])) {
    $to = "Ravi";
    $result = mysql_query("SELECT *FROM `events` WHERE `to` = '$to'");
    if (!empty($result)) {
        if (mysql_num_rows($result)>0) {
            $row = mysql_fetch_array($result);
            echo $row["to"] + " " + $row["from"];
        } else {
            echo 'rows = 0';
        }
    } else {
        echo "empty for $to";
    }
//} else {
//}
?>

MYSQLI版+一些调整:

<?PHP
    $host = "localhost";
    $user = "root";
    $password = "";
    $database="algopm1";



    $link = mysqli_connect($host, $user, $password, $database);
    IF (!$link){
        echo ("Unable to connect to database!");
    }
    ELSE {
    $query = "SELECT *FROM `events` WHERE `to` = '$to'";
    $result = mysqli_query($link, $query);
     if (mysql_num_rows($result)>0) {

    while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
    echo $row["to"]. "+". $row["from"];
    }
     }
     ELSE {
         echo 'rows = 0';
     }
    }
    mysqli_close($link);

?>

我想将@njk和@Wezy归功于他们对mysql中保留字的贡献。如果表事件在这种情况下只包含一个“to”“Ravi”,则不需要WHILE循环。我怀疑事件的数量可能大于一个。

答案 3 :(得分:0)

@Wezy有一点,但让我们进行故障排除:

@JonathanRomer在评论中建议,做:

$result = mysql_query("SELECT *FROM `events` WHERE `to` = '$to'") or die(mysql_error());

它说什么?它完全失败了吗?

,就在mysql_query之前:

die("SELECT *FROM `events` WHERE `to` = '$to'");

这将打印正在执行的错误查询。接下来,启动mysql控制台或PHPMyAdmin并尝试手动执行此查询。

再说一遍,它说什么?

答案 4 :(得分:0)

实际上,我的主要目的是对此消息进行编码,并使用JSON在移动设备上接收它。这是完整的代码。对于正常检查目的,可以单独显示值,而不是使用json_encode(*)。这是我得到的解决方案,它非常适合在我正在工作的Android应用程序中接收数据。

<?php

$host = "localhost";
$user = "root";
$password = "";
$database="algopm1";
$response = array();

$mysqli = new mysqli($host, $user, $password, $database);

if (mysqli_connect_errno()){
    $response["success"] = 0;
    $response["message"] = mysqli_connect_error();
    echo json_encode($response);
}

if(isset($_POST['to'])) {
    $to = $_POST['to'];
    $query = "SELECT *FROM `events` WHERE `to` = '$to'";

    if($stmt = $mysqli->prepare($query)) {
        $stmt->execute();
        $stmt->store_result();
        $i = 0;
        if($stmt->num_rows > 0) {
            $stmt->bind_result($rowto, $rowfrom, $rowevent);
            $response["events"] = array();
            while($stmt->fetch()) {
                $events = array();
                $events["to"] = $rowto;
                $events["from"] = $rowfrom;
                $events["event"] = $rowevent;
                array_push($response["events"], $events);
            }
            $response["success"] = 1;
            echo json_encode($response);
        } else {
            $response["success"] = 0;
            $response["message"] = "No events found";
            echo json_encode($response);
        }
        $stmt->close();
    }
} else {
    $response["success"] = 0;
    $response["message"] = "Required fields are missing";
    echo json_encode($response);
}

$mysqli->close();
?>