好的我已经构建了一个面向数据库的应用程序(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);
}
}
?>
这是完美的,但不能与第二个混合使用。
知道怎么做吗?
答案 0 :(得分:1)
我在应用程序中有多用户功能,我正在使用它,它适用于我:
我希望它对你有用,
$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);