简体   繁体   English

为什么当我查找某些列而不查找其他列时,php为什么返回json?

[英]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.

相关问题 PHP 字符串比较在某些视图中看起来相同,但在其他视图中看起来不同 - PHP string comparison for strings which look the same in some views, but not in others 为什么 CORS 在某些地方有效,而在其他地方无效? - Why does CORS work in some places, but not in others? php openid:为什么该库接受某些用户名,而其他用户名却导致错误? - php openid: Why does the library accept some usernames and others cause errors? 为什么当我尝试了一段时间在 phpMyadmin 数据库的表中创建列时,它不起作用? - why when I have been trying for some time to create columns in a table in a phpMyadmin database, does it not work? 对于使用sudo的某些命令,PHP exec正确返回,而其他命令则不正确 - PHP exec returns correctly for some commands using sudo, not others json解码某些请求但使用Algolia php库却无法解决其他请求 - json decode error on some requests but not others with Algolia php library Laravel 中的 JSON 列不接收其他列的值 - JSON column in Laravel does not receive values of others columns PHP Searchfunction不适用于某些产品,但适用于其他产品 - PHP Searchfunction doesn't for some products but DOES work for others 为什么我的PHP回声在某些地方返回0(但在其他地方却起作用)? - Why is my PHP echo returning a 0 in some places (yet working in others)? 为什么有些数组值会被覆盖而有些则不会? PHP - Why are some array values being overwritten and others are not? PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM