从外部MySQL数据库获取JSON错误?

时间:2011-06-10 07:22:37

标签: php json

我正在使用本教程,我不知道这是什么错误

    <?php
header('Content-type: application/json');  // this is the magic that sets responseJSON

// Connecting, selecting database
$link = mysql_connect($dbhost, $dbuser, $dbpass)
    or die('Could not connect: ' . mysql_error());
mysql_select_db($dbname) or die('Could not select database');

switch($_POST['op']) {
    case 'getAllRecords': {
        $table = $_POST['table'];
        $query = sprintf("SELECT * FROM %s", mysql_real_escape_string($table));
        // Performing SQL query
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());
        $all_recs = array();
        while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $all_recs[] = $line;
        }
        break;
    }
}

echo json_encode($all_recs);

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

错误是: - null
警告:mysql_free_result():提供的参数不是 /home/ajay/public_html/mapleleafrealities.com/test.php 中有效的MySQL结果资源 26

This is Example

My Error

如果您有从外部数据库获取json的任何简单示例,请给我链接或代码

1 个答案:

答案 0 :(得分:1)

op POST参数不是“getAllRecords”,因此您尝试编码不存在的内容,并释放从未采用的结果。试着把它们放进去。