0

如何从 mysql 查询中选择一行并将其保存到变量中。这是我目前使用的完整代码:

    <?php
    header('Content-Type: text/html; charset=utf-8');
    include 'config.php';

    // Connect to server and select databse.
    mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect");
    mysql_select_db("$dbname")or die("cannot select DB");


    $IND=$_POST['id'];


    $IND = stripslashes($IND);
    $IND = mysql_real_escape_string($IND);

    $sql = "select * from table where IND='$IND'";
    $result=mysql_query($sql);

       $row = mysql_fetch_array($result);
      $number = $row['number'];


    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    if ($count == 1) {
       $message = mysql_fetch_assoc($result);
        $message['status'] = 'ok';
        $message = array($message);


    } else {
         $message = mysql_fetch_assoc($result);
        $message['status'] = 'error';
        $message = array($message);
    }


    header('Content-Type: application/json');
        print '{"key":'. json_encode($message) .'}';
?>

但是当我使用$row = mysql_fetch_array($result); $number = $row['number'];JSON 编码的 $message 时显示未定义。

那么如何从 mysql 查询中选择一条记录并使我的所有其余代码也可以工作呢?

4

1 回答 1

0

将变量声明$messagearray,然后声明条件assign中的值if。尝试如下:

$message=array();
$count=mysql_num_rows($result);

    if ($count == 1) {
       $message = mysql_fetch_assoc($result);
        $message['status'] = 'ok';
        $message = array($message);


    } else {
         $message = mysql_fetch_assoc($result);
        $message['status'] = 'error';
        $message = array($message);
    }
于 2012-07-03T07:26:15.537 回答