1

I have 3 php files in my Android app - dbConnect.php, which connects with the database, volleyLogin.php, which logs in the user, and UserDetails.php, which gets some info about the logged in user.

They work perfectly when running the files in my browser but when run from my Android App UserDetails.php isn't working. It keeps returning '0 results' and I am told the variable username is undefined even though I use the include statement which is supposed to insert the content of one PHP file into another PHP file.

My dbConnect.php looks like this :

<?php

define ('HOST', 'localhost');
define ('USER', 'root');
define ('PASS', 'password');
define ('DB', 'dbname');

$con = mysqli_connect("localhost", "root", "password", "dbname") or die('Unable to Connect');

?>

My volleyLogin.php is like this :

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){
$username = $_POST['username'];

include('dbConnect.php');
$sql = "SELECT * FROM user WHERE username = '$username'";
$result = mysqli_query($con,$sql);
$check = mysqli_fetch_array($result);
if(isset($check)){

        echo 'success';
} else {
        echo 'Could not log in';
}
}

?>

And UserDetails.php looks like this :

<?php
include('dbConnect.php');
include('volleyLogin.php');

$resultSet = $con->query("SELECT category.cat_name FROM category INNER JOIN user ON category.user_id =
user.user_id WHERE user.username = '$username' ")

or die($con->error);

if ($resultSet->num_rows > 0) {

        while($rows = $resultSet->fetch_assoc()) {

$catname = $rows['cat_name'];

        echo "<p> Name: $catname </p>";
}}
        else {
   echo "0 results";
}
//echo "userdetails.php works";

$con->close();

?>

The problem line is :

$resultSet = $con->query("SELECT category.cat_name FROM category INNER JOIN user ON category.user_id =
user.user_id WHERE user.username = '$username' ")
CHarris
  • 2,693
  • 8
  • 45
  • 71

1 Answers1

1

Your logic is wrong: mysqli_*() functions basically either return an object/result handle, or a boolean true (success)/false (failure). They ALWAYS return something, so having:

$check = mysqli_fetch_array($result);
if(isset($check)){

is wrong. $check will ALWAYS be set. You need to explicitly check for boolean false to detect failure:

if ($check === false) { 
   die(mysqli_error($con));
}

And note that in none of your sample code do you define $username, so your query is doing WHERE user.username = '', and probably matching 0 rows

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I'll look into your answer but is `$username` not defined in `volleyLogin.php` when I say `$username = $_POST['username'];` ? – CHarris Sep 20 '16 at 21:41