php - JSON 数据库连接无法识别

标签 php mysql json

好的,我已经构建了一个面向数据库的应用程序(Mysql),问题是它从 JSON php 文件连接到数据库,我希望它有多个具有不同 UN 和 Pass 的用户。

我没有使用 Json 文件的经验,我找到了一些示例,但无法将它们组合起来。代码如下:

<?php
$link = mysql_pconnect("host", "db_user", "db_password") or die("Could not connect");
mysql_select_db("db_name") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM news");
while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}
echo json_encode($arr);
?>

这是一种方法,但它不是多用户的。

<?php 
header('Content-type: application/json');
if($_POST) {
    if($_POST['username'] == '****' && $_POST['password'] == '*****') {
        echo '{"success":1}';
    } else {
        echo '{"success":0,"error_message":"Username and/or password is invalid."}';
    }
}else {    echo '{"success":0,"error_message":"Username and/or password is invalid."}';}
?>

这是多用户但不包含数组。

<?php
$host = "************"; //Your database host server
$db = "****"; //Your database name
$user = "****"; //Your database user
$pass = "****"; //Your password

$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");
}
else
{
    //Attempt to select the database
    $dbconnect = mysql_select_db($db, $connection);
    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");
    }
    else
    {
        $query = "SELECT * FROM *****";
        $resultset = mysql_query($query, $connection);
        $records = array();
        //Loop through all our records and add them to our array
        while($r = mysql_fetch_assoc($resultset))
        {
            $records[] = $r;
        }
        //Output the data as JSON
        echo json_encode($records);
    }
}
?>

这是完美的,但不能与第二个混合。

知道该怎么做吗?

最佳答案

我的应用程序具有多用户功能,我正在使用它,它对我有用:

我希望它对你有用,

$sql ="select * from login WHERE user_name='$s1' and password='$s2'";
$data = mysql_query($sql);
$count = mysql_num_rows($data);

if($count==1)
{
    $response['success']=1;
    $response['userdetail'] = mysql_fetch_assoc($data);

}
else
{
    $response['success']=0;
}


echo json_encode($response);

关于php - JSON 数据库连接无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26356561/

相关文章:

json - 编码后删除转义字符

php - 如何在 Symfony 表单中设置下拉列表子元素的样式

mysql - 尝试分别统计某种类型的入口数量

php - MySQL默认值不正确

mysql - 如何在 INSERT 之前检查 mysql 中的值

ios - 出现-[__ NSCFDictionary objectAtIndex:]错误:无法识别的选择器已发送到实例

javascript - 如何使用 JQuery 将 javascript 对象(从 json webmethod 转换)映射到数组,其中 js 对象仅具有 1 个属性和多个值

PHP - 发现 Debian 软件包是否存在

php - 2个mysql数据库连接

php - 在 PHP 中转义 iframe srcdoc 值的安全方法?