0

以下代码始终显示

    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 {
    //}
    ?>
4

5 回答 5

6

to是 MySQL 中的保留字,如果要使用它,必须用反引号括起来:

.... WHERE `to` = ...
于 2013-01-27T18:25:26.267 回答
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 循环。我怀疑事件的数量可能大于一个。

于 2013-01-27T18:54:03.820 回答
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尝试手动执行此查询。

再说一遍,它说什么?

于 2013-01-28T15:20:12.730 回答
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();
?>
于 2013-01-30T19:10:35.973 回答
0

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

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

这在你继续使用mysql而不是mysqli之前。没有什么不同,但 mysqli 似乎对 mysql 有一个包装器,并使用“->”为连接实例化新类。如果那有意义的话。

于 2013-01-27T18:29:51.853 回答