gpt4 book ai didi

php - mysqli_fetch_array 没有返回任何东西

转载 作者:行者123 更新时间:2023-11-29 05:08:27 27 4
gpt4 key购买 nike

我已经在 phpMyAdmin 中检查了我的 Sql 语句,它返回了多个结果,但是当我将它作为 php 脚本运行时,它没有返回任何内容。谁能告诉我我犯了什么错误?

<?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";
}
*/

?>

最佳答案

像下面这样更改您的代码并检查一次:-

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

关于php - mysqli_fetch_array 没有返回任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43843350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com