2017-05-08 175 views
1

我檢查了我的phpMyAdmin中的Sql語句,它返回多個結果,但是當我將它作爲php腳本運行時,它不返回任何東西。任何人都可以告訴我我犯了什麼錯誤嗎?mysqli_fetch_array沒有返回任何東西

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

​​
+1

變化MYSQLI_NUM –

+0

這是否返回任何東西當U上的PHP myadmin運行? –

+0

是的,它在phpmyadmin中。 – Gurchani

回答

2

更改你的代碼像下面檢查一次: -

conn.php(檢查文件名,並糾正自己): -

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

和其他頁面: -

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

爲什麼不使用簡單的mysqli_fetch_all,只要OP使用相同的數據庫列名填充新數組? – hassan

+0

@hassan補充說明 –

+0

明白了,你太棒了。非常感謝 – Gurchani

1

變化MYSQL_NUMMYSQLI_NUM; -

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


} 
+0

我試過這個,它不會改變任何內容 – Gurchani

+0

您有足夠的聲望將它作爲評論 –

+0

@Gurchani做'echo $ sqlQry; die();'並將該查詢直接複製並粘貼到phpmyadmin並檢查任何返回d還是不是? –

相關問題