1

This is for an Android application and I am using web-services written in php. I have 3 php scripts - dbconnect.php(this has all the db variables), login.php(this validates the user credentials and open a session) and showCases.php(which will return a list of tickets from the userID variable obtained from the login.php script)

However, for some reason I have: "Access denied for user 'ODBC'@'localhost' (using password: NO)" when I am requesting information showCases.php. I am not quite sure if the session is being used correctly.

Here is the login.php script:

 //Displaying the error if sql query is incorrect
 if(!$result){
    die(mysql_error());
 }else{
    $row = mysql_fetch_assoc($result);
    $first_name = $row['first_name'];
    $id = $row['id'];
 }

 //If found, number of rows must be 1
 if($num_rows==0){    
   $success = 0;
 }else{
   //creating session
   session_start();
   session_register("$username");
   session_register("$password");
   $success = 1;  

 }
   $arr = array(
    array('username' => $username, 'id'=>$id,'success'=>$success, 'first_name'=>$first_name));
    #array('success'=> $success));

 echo json_encode((array('Username'=>$arr)));

 mysql_close();
 ?>

The script would return the username array to the android application for it to process the validation. Upon validation, the android application would request tickets from this php script.

session_start();

#require('dbconnect.php');
require_once('login.php');

$response=array();
$response['infos'] = array();   

//execute the SQL query and return records
$result = mysql_query("SELECT cases.id, cases.account_id....  casesvisibility.user_id = '$id'......";

//Displaying the error if sql query is incorrect
if(!$result){
   die(mysql_error());
}

$num_rows = mysql_num_rows($result);
$arr = array();

if($num_rows!=0){
while ($row = mysql_fetch_assoc($result)) {
$arr['cases_id']=$row['cases.id'];
$infos[]=$arr;
}
}else{
#$arr['existingCases']=$row['0'];
$arr['cases_id']=0;
$infos[]=$arr;
}

#Echoing a JSONArray
print(json_encode(array('Cases'=>$infos)));
//close the connection
mysql_close();
?>

I am not too sure this is well-written code for the functionality I want it to achieve. When I call this script from android application, I first get the JSON_array from the login script and it tells me:

{"Username":[{"username":"","id":null,"success":0,"first_name":null}]}id is 
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in E:\wamp\www\mobile.dcl.mu\webserver\showCases.php on line 18

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in E:\wamp\www\mobile.dcl.mu\webserver\showCases.php on line 18

Access denied for user 'ODBC'@'localhost' (using password: NO)

I understand that I'm trying to use a variable from another php script.

Can you help me some this issue please?

Thank you

3 Answers 3

1

Where you create $num_rows? Probably you want to put this code in top login.php:

 //Displaying the error if sql query is incorrect
 if(!$result){
    die(mysql_error());
 }else{
    $num_rows = mysql_num_rows($result);
    if ($num_rows) {
            $row = mysql_fetch_assoc($result);
            $first_name = $row['first_name'];
            $id = $row['id'];
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

I think you are mixing up session and db connection (if I understand your code snippets correctly).

Keep in mind, that for the server, each request is a completely new process. You can share variables via session, but you can not share process bound resources like the db connection.

as your server is telling you, on the second request there is no password and maybe even the wrong (default?) db user given.

So, what you need to do is simply this:

Provide the db connection information in every possible execution path. i.e. if your android app calls http://your.server/showCases.php directly, include dbconnect.php in showCases.php, too.

I think you had it once: in your second script you have this line:

#require('dbconnect.php');

try uncommenting this again, it may bring you a step closer to your goals.

P.S. the pure mysql_* functions are deprecated, read the php.net docu (or other questions here) for the security concerns regarding these functions. Upgrade your scripts to use PDO or mysqli

2 Comments

I tried using the require('dbconnect.php') but it gave me the same result. I'm not quite sure what can I do.
do you need login.php in showCases.php? you have a mysql_close() call at the bottom of your source code snippet from login.php, this will kill your db connection before any code is executed in showCases.php. try the request without requirering login.php
0

Most likely issue with your database connection call, you are either not providing correct username and or password. i suspect missing password . check your database connection

$db_host = "localhost"; // database host
$db_name = "test";   // database name
$db_login = "root";  // database user
$db_pass= "*****"; // database password

$db = mysql_connect($db_host, $db_login, $db_pass);
mysql_select_db($db_name,$db);

this is just sample code to indicate what i am referring to. Also, make sure you include this db connection file on each php file. There are various ways to make db connection available to different php files( where sql calls gets make)

1 Comment

I have this on top of the login.php script include('dbconnect.php'); This script contains all the correct information about the database and the mysql connect. Do I need to include it as well on top of the showCases.php even though I have included login.php?

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.