0

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.

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.

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. The problem, I think is in the php.

  • 1
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Mar 28 '17 at 23:19
  • 1
    Don't use the error suppression operator (@) as it hides error messages that may be helpful in debugging your code. You should also always write your code so it does not generate any PHP errors including notices. – John Conde Mar 28 '17 at 23:19
  • You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to get a detailed error message from the database. – John Conde Mar 28 '17 at 23:20
  • 1
    You _do_ realize that you are using different characters around your table names? That is a syntax error, an obvious one. `'` is not `\``... – arkascha Mar 28 '17 at 23:21
  • 1
    @arkascha Yes I realized that, it was just a typo I made while making the question. Also the query works works without the ` charaters. The problem comes when adding the other columns of the table. – Brandon Villalbazo Nussbaumer Mar 28 '17 at 23:28
  • @JohnConde I'm currently changing all the mysql functions for the mysqli. Even so it continues with the problem. – Brandon Villalbazo Nussbaumer Mar 28 '17 at 23:32
  • 1
    You *still* haven't checked for error in your code. MySQL is probably telling you what's wrong. You're just not listening. – John Conde Mar 28 '17 at 23:39
  • @JohnConde When running the MySQL by itself in workbench or phpmyadmin, it gives me the things I ask for without any error messages. The problem comes from the PHP. – Brandon Villalbazo Nussbaumer Mar 28 '17 at 23:55
  • 2
    *Then use PHP to check for the errors!* – John Conde Mar 29 '17 at 00:00
  • This is impressive. You are told from every side that you need to use the error checking options the mysql extension offers you to be able to profit from that information. But instead of trying to find out (or ask) how to do that you simply insist that there are no errors shown (of course not if you don't visualize them!). May we know _why_ you refuse to take any sound advice after asking for excatly that here? – arkascha Mar 29 '17 at 06:07

1 Answers1

0

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.

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:

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!

sebi
  • 79
  • 6
  • The single quote was just a typo in the question so this answer is not correct – John Conde Mar 28 '17 at 23:32
  • OK apologies. What is the actual error you are receiving? Have you tried running `DESC RECURSOHUMANO` to verify the column exists? – sebi Mar 28 '17 at 23:36
  • @sebi Im using phpmyadmin and workbench to manage the database so I can see that the column exists through there. I'm not receiving any error, it just doesnt gives me the encoded json at the end of the code. If I just use the columns `idComp`, `nombre`, and `foto`, it shows it when I run the php, but when I add the other column it doesnt show me anything, not even an error. – Brandon Villalbazo Nussbaumer Mar 28 '17 at 23:42
  • Ok then the next step is to print out the error you receive when you add the offending column to get a better idea of what the problem is. It will save you lots of time! – sebi Mar 28 '17 at 23:43
  • @sebi There are no error messages, it just displays a blank page – Brandon Villalbazo Nussbaumer Mar 28 '17 at 23:56
  • Check the error logs. See if your web server is suppressing the errors. As mentioned in other posts, the `@` char will suppress warnings. There's also a command you can run `mysql_error` like the example shows http://php.net/manual/en/function.mysql-error.php – sebi Mar 28 '17 at 23:59