尝试使用php从mysql中选择记录,结果是json,没有标识符


Trying to select records from mysql using php, the results are in json without the identifiers

如前所述,我得到了一个没有标识符的JSON字符串。我使用的代码是:

<html>
    <head>
        <script language="javascript" type="text/javascript" src="/content/scripts/jquery/v2.1.3/jquery-2.1.3.js"></script>
    </head>
    <body>
        <h2> Client example </h2>
        <h3>Output: </h3>
<?php
    $host = "localhost";
    $user = "{username}";
    $pass = "{password}";
    $databaseName = "{database}";
    $tableName = "{table}";
    $con = mysql_connect($host,$user,$pass);
    $dbs = mysql_select_db($databaseName, $con);
    $result = mysql_query("SELECT * FROM $tableName");
    $array = mysql_fetch_row($result);
    echo json_encode($array);
?>
</body>

我的结果是:

["2d791c8b-d8cf-11e4-a712-002590f3d888","********","****"]

我是PHP和MySQL的新手,所以要温和一点。

mysql_fetch_row返回枚举数组

[0] => "2d791c8b-d8cf-11e4-a712-002590f3d888"

mysql_fetch_array返回关联数组

["key"] => "2d791c8b-d8cf-11e4-a712-002590f3d888"

mysql_*函数现在已经失效,前面是MySQLi/PDO。

要具体回答您的问题,请尝试使用以下方法:

<?php
$host = "localhost";
$user = "{username}";
$pass = "{password}";
$databaseName = "{database}";
$tableName = "{table}";
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
$result = mysql_query("SELECT * FROM $tableName");
$array = mysql_fetch_array($result);
echo json_encode($array);
?>

但rockerBOO是对的,至少看看mysqli