0

Ok i have build an app that is Database oriented ( Mysql ), the thing is that it connect's to the Database from JSON php file and i want it to have multiple users with different UN and Pass.

I have non experience with Json files and i found some examples but can't combine them. Here are the codes:

<?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);
?>

This is one way but it is not multi user.

<?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."}';}
?>

This is with multi user but not containing array.

<?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);
    }
}
?>

and this is perfect but can't mix it with the second one.

Any idea how to do so?

1
  • Don't build JSON manually and don't use deprecated mysql_* functions. I am not sure what you mean by "multi-user". DO you mean you want different mysql user/pw combos? If so, why? Commented Oct 14, 2014 at 14:00

1 Answer 1

1

i have multiuser functionality in app , and i am using this and it works for me :

i hope it will work for you ,

$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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.