我不太喜欢PHP或mySQL,所以希望您能对我有所帮助。我有一个php脚本,该脚本带有一个函数,该函数返回一个json以及数据库表中的所有条目:
public function select($table, $wheres = null)
{
$connect = $this->connect();
if ($wheres == null)
{
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'`');
} else {
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'` WHERE '.$this->wheres($wheres));
}
$i = 0;
$ret = array();
while ($row = mysqli_fetch_assoc($query)) {
foreach ($row as $key => $value) {
$ret[$i][$key] = $value;
}
$i++;
}
return ($ret);
}
编辑:
我有调用此功能的应用程序。一切都按预期工作,但是随后我在其中一个表中添加了2个字段(1个文本,1个varchar),现在当我调用此函数时,它什么也不返回;字面上是一个空字符串。
我还注意到,如果我只删除那些新字段,那么它会起作用,这有点烦人,而且我需要这些字段,但我无法弄清楚问题根源在哪里。
同样,应用程序代码也不是问题:调用此函数时,它仅传递2个参数(方法名称和表名称),这不会出错。
顺便说一下,这是表结构,它可以帮助您吗?
json
,此返回json
<?php
function getData($tbl_name=null,$id=null)
{
$con = mysqli_connect("localhost","root","","mydb"); //my db is my database name
if(!$con){ die( "could't connect with database" ); }
$sql = mysqli_query($con,'SELECT * FROM $tbl_name WHERE id = {"$id"}');
while($row = mysqli_fetch_assoc($sql))
{
#echo"<pre>";
#print_r($row); //this will return data form data base in array from
return json_encode($row);
}
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句