[英]Why does php returns a json when I look for some columns and not for others?
Here is my code, it works like it is right now: 这是我的代码,它就像现在一样工作:
$con = @mysql_connect($server_name, $mysql_username, $mysql_password) or die("cannot connect");
mysql_select_db($db_name) or die("cannot select DB");
$sql="SELECT `idComp`,`nombre`,`foto` from RECURSOHUMANO;";
$result = mysql_query($sql);
$json= array();
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_assoc($result)){
$json['Data'][]=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>
The table that i'm working on has other columns and would like to add some of them to the SELECT query, but when I add another one, say apPat, it stops working. 我正在处理的表具有其他列,并希望将其中一些添加到SELECT查询中,但是当我添加另一列(例如apPat)时,它将停止工作。
Just to be clear Ill write how I would normally do it: 为了清楚起见,我会写我通常会做的事情:
$sql="SELECT `idComp`,`nombre`,`apPat`,`foto` from RECURSOHUMANO;";
apPat is exactly like 'nombre', so I dont see why it doesn't work. apPat就像'nombre',所以我不明白为什么它不起作用。
Also when I add other columns it works, it just breaks with a couple of them, but I need them. 另外,当我添加其他列时,它可以工作,但其中的几个中断了,但是我需要它们。
UPDATE: Just to clarify. 更新:只是为了澄清。 The MySQL query is correct, if I run it by itself it gives me what I want, even when I add the other columns.
MySQL查询是正确的,如果我自己运行它,即使我添加其他列,它也会给我想要的东西。 The problem, I think is in the php.
我认为问题出在php中。
When using single or double quotes in your field
list, you are telling mysql
to make a column that has that exact string inside of it. 当在
field
列表中使用单引号或双引号时,您是在告诉mysql
创建一个在其中包含确切字符串的列。
So your results more like likely look like this, correct? 所以您的结果更像是这样,对吗?
mysql> select `pid`, `source`, `alias` from url_alias limit 5; +-----+--------------------+--------------------------+ | pid | source | alias | +-----+--------------------+--------------------------+ | 1 | /taxonomy/term/16 | /blah/project-management | | 2 | /taxonomy/term/14 | /blah/development | | 3 | /taxonomy/term/137 | /blah/business | | 4 | /taxonomy/term/13 | /blah/design | | 5 | /taxonomy/term/15 | /blah/quality-assurance | +-----+--------------------+--------------------------+ 5 rows in set (0.00 sec)
Notice now the single quotes around 'i am a string'
and how the results are shown: 现在注意
'i am a string'
周围的单引号以及如何显示结果:
mysql> select `pid`, 'i am a string', `alias` from url_alias limit 5; +-----+---------------+--------------------------+ | pid | i am a string | alias | +-----+---------------+--------------------------+ | 1 | i am a string | /blah/project-management | | 2 | i am a string | /blah/development | | 3 | i am a string | /blah/business | | 4 | i am a string | /blah/design | | 5 | i am a string | /blah/quality-assurance | +-----+---------------+--------------------------+ 5 rows in set (0.00 sec)
I hope this helps! 我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.