gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:39:11 25 4
gpt4 key购买 nike

好的,我已经构建了一个面向数据库的应用程序(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com