1

I have checked my Sql statement in the phpMyAdmin and it returns multiple results but when i run it as a php script it doesn't return anything. Can anyone please tell me what mistake i'm making?

<?php 
require "conn.php";
/*$sqlQry = $_POST["sqlQry"];*/

$sqlQry = "SELECT bad.id, Name, Address, Latitude, Longitude, AvAge FROM baraddresses bad inner join barlivedata bld on bad.id = bld.id inner join bar_data bdt on bdt.id = bad.id";

$result = mysqli_query($conn ,$sqlQry);

$json = array();
while ($row = mysqli_fetch_array($result , MYSQL_NUM)){
$json[] = array('id' => $row[0], 
        'Name' => $row[1],
        'Address' => $row[2],
        'Latitude'=> $row[3],
        'Longitude' => $row[4],         
        'AvAge' => $row[5]          
            );
}

$jsonstring = json_encode($json);
    echo $jsonstring; 

?>

Conn.php

<?php

/* This file allows you to connect to a database */


$db_name = "bdata";
$mysql_username = "root";
$mysql_password = "" ;
$server_name = "localhost";
$conn = mysqli_connect($server_name , $mysql_username , $mysql_password , $db_name  );

/*
if($conn){
echo "true";
} 
*/

?>
10
  • 1
    change to MYSQLI_NUM Commented May 8, 2017 at 8:49
  • does this return anything when u run on php myadmin? Commented May 8, 2017 at 8:56
  • Yes it does in phpmyadmin. Commented May 8, 2017 at 8:59
  • @Gurchani can you please add this if (mysqli_num_rows($result) > 0) { then your while loop}else{echo "No results} Commented May 8, 2017 at 9:04
  • No even echoing "No result" Commented May 8, 2017 at 9:09

2 Answers 2

2

Change you code like below and check once:-

conn.php (check file name and correct yourself):-

<?php
    //comment these two lines when everything started working fine
    error_reporting(E_ALL);
    ini_set('display_errors',1);

    $db_name = "bdata";
    $mysql_username = "root";
    $mysql_password = "" ;
    $server_name = "localhost";
    $conn = mysqli_connect($server_name , $mysql_username , $mysql_password , $db_name  );

    if(!$conn){
      echo "connection error:-".mysqli_connect_error();
    }
?>

And other page:-

<?php
    //comment these two lines when everything started working fine
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    require "conn.php";

    $sqlQry = "SELECT bad.id, bad.Name, bad.Address, bad.Latitude, bad.Longitude, bad.AvAge FROM baraddresses bad JOIN barlivedata bld on bad.id = bld.id JOIN bar_data bdt on bdt.id = bad.id";

    $result = mysqli_query($conn ,$sqlQry) or die(mysqli_error($conn));

    $json = array();
    if(mysqli_num_rows($result)>0){
       /* Either use this */
        while ($row = mysqli_fetch_assoc($result)){
            $json[] = array('id'=>$row['id'],'Name'=>$row['Name'],'Address'=>$row['Address'],'Latitude'=>$row['Latitude'],'Longitude'=>$row['Longitude'],'AvAge'=>$row['AvAge']);
        }
        /* Or use this

        $json = mysqli_fetch_all($result,MYSQLI_ASSOC);

        mysqli_free_result($result); */

        mysqli_close($conn);

        echo "<pre/>";print_r($json);

        $jsonstring = json_encode($json);

        echo $jsonstring; 
    }else{
      die('No record exist');
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

why not to use simply mysqli_fetch_all, as long as the OP is filling a new array with the same database columns names?
@hassan added that
@Gurchani glad to help you :):)
2

Change MYSQL_NUM to MYSQLI_NUM;-

while ($row = mysqli_fetch_array($result , MYSQLI_NUM)){


}

8 Comments

you have enough reputation to put it as a comment
@Gurchani do echo $sqlQry;die(); and copy and paste that query directly to phpmyadmin and check anything returned or not?
I have pasted it directed in phpmy admin and it does return results there but when i run it in this code it doesnt return anything.
@Gurchani can you please add conn.php code too in your question
@ArunKumaresh what makes u think its him?
|

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.