0

Beginner here, I am trying to make a call from the PhoneGap application to a web server using XAMPP local host for a school assignment. I have a working login.php file from one of the previous projects. However, once transferred to the new project, Eclipse would not run the PHP codes and thus, the whole project, displaying the error as shown above: “Fatal error: Call to a member function fetch_array() on null”.

The differences between the project now and the ones before was where I placed the PHP files. Since I no longer have a microsoft azure subscription, I have switched to local host XAMPP. As a result, also changing my php file location from http://example.cloudapp/login.php to the same folder where I have my html files, to htcdocs/project/login.php. Here are the login.php codes:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
$conn = new mysqli("127.0.0.1", "root", "root", "bencoolen");
$userid = $_GET["userid"];
$password = $_GET['password'];
$query = "SELECT count(*) as found from profiles where userid ='" .
$userid . "' and password = '" . $password . "'";
$result = $conn->query($query);
$count = $result->fetch_array(MYSQLI_NUM);
$json_out = "[" . json_encode(array("result"=>$count[0])) . "]";
echo $json_out;
$conn->close();
}
catch(Exception $e) {
$json_out = "[".json_encode(array("result"=>0))."]";
echo $json_out;
}
?>

I have made sure that the database 'bencoolen' was created with a table called 'profiles' containing both 'userid' and 'password' fields.

enter image description here

I have also downloaded 'PHP Development Tool' on Eclipse and all my php codes syntax are highlighted. Eclipse could also run the javascript and html codes before the I have added login.php. Eclipse directed me to Line 12, with the Fatal error, which was:

$count = $result->fetch_array(MYSQLI_NUM);

Heres my unsupported theory: I think they do not recognise my userid and password fields because the PHP file is not connected to the database due to its file location

Detective merry
  • 384
  • 1
  • 21

2 Answers2

1

I think : You have to fetch that one record, it will contain the result of Count()

$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Tính Ngô Quang
  • 4,400
  • 1
  • 33
  • 33
  • 1
    Thanks, but I just tried this and they returned the same error but under a different fetch: Fatal error: Call to a member function fetch_row() on null – Detective merry Jul 08 '16 at 03:51
  • use [$count = $result->fetch_array(MYSQLI_NUM);] is wrong. it's numeric array not value in sql count [SELECT count(*) as found from profiles]. It's right when sql = [SELECT * from profiles] – Tính Ngô Quang Jul 08 '16 at 04:20
1

i am not saying my code bellow is best approach but you can follow and get result i also working same scenario and get success.

<?php
    $server = "127.0.0.1";
    $user = "root";
    $pass = "root";
    $db = "bencoolen";
    //open connection to mysql db
    $connection = mysqli_connect($server,$user,$pass,$db) or die("Error " . mysqli_error($connection));

    $userid = $_REQUEST['userid'];
    $password = $_REQUEST['password'];

    //fetch table rows from mysql db

    $sql = "select * from profiles WHERE userid= '$userid' AND password = '$password'";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

   // echo json_encode(mysqli_num_rows($result));
    if($result){
        if (mysqli_num_rows($result) >= 1 ) {
            $json=array("status"=>1,"message"=>"done");
            echo json_encode($json);
        }else{
            $json=array("status"=>0,"message"=>"email or password not match!");
            echo json_encode($json);
        }
    }else{
        $json=array("status"=>0,"message"=>"error");
        echo json_encode($json);
    }
    mysqli_close($connection); ?>

You can use "localhost" in place of 127.0.0.1 if you are running on same machine.

and you need to call your service like:

http://localhost/login.php?userid=test&password=12345

Hope it will help you.

Naitik
  • 1,455
  • 15
  • 26