2

I found similar questions but can't solve my problem yet. Here is the relevant code:

$query = "SELECT * FROM conceptos WHERE descripcion = '$descripcion'";
if ($result = mysql_query($query,$connection)){
    if (mysql_num_rows($result) > 0){
        //Do something
    } else {
        die($query);
        exit;
    }
} else {
    die(mysql_errno() . ' - ' . mysql_error());
    exit;
}

I don't have problems with the connection or the permissions, because this code snippet is inside a loop and the other queries enter the "Do something" section. But when I take the echoed query and execute it in phpMyAdmin, it returns 1 value as expected. Why? What reasons can lead to this behavior? Thanks in advance for any advice.

9
  • 1
    Change die($query); to die(mysql_error()) and see what it outputs. Commented Oct 10, 2013 at 19:17
  • 3
    Also note that the mysql_ functions are deprecated, if you can, you should consider switching over to either mysqli or PDO. Commented Oct 10, 2013 at 19:20
  • @AmalMurali mysql_errno() outputs 0 and mysql_error() outputs nothing. The query has no errors because it enters in the if part not the else part of the code. The problem is that the query returns no value and the same query (from the die) copy and pasted in myphpadmin gets one result! Commented Oct 10, 2013 at 19:33
  • If description is some variable text, then a) encodings might be messed up b) too much character escaping. Kind of example for (b): you could have &amp; or &lt; in you description, which become & and < after output in html. Commented Oct 10, 2013 at 19:33
  • @SergeSeredenko I think that the problem can be by the encodings... the $description variable has a single quote but I escaped it with mysql_real_escape_string() before constructing the $query string variable. Commented Oct 10, 2013 at 19:41

5 Answers 5

4

I had this problem and found that it was because I junked up my database by copy/pasting directly to the database from MS Word. Pasting had inserted special slanted apostrophes that PHPMYADMIN could apparently parse but my php code could not. Once I replaced those with a standard single quote, all was well.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this "SELECT * FROM conceptos". If it's worked, you have bad query in "WHERE ..."

1 Comment

this same query executes about 1,000 times before (with the where part) the problem... the query itself has no errors, the problem is that it has no results but it must have. If the same query with the where part includes is executed in phpmyadmin it works as expected.
1

Are you sure your query is searching for the right description? The double quotes should expand all internal variables, but you do have single quotes as well in case there is a copying to stackoverflow issue.

This will ensure that the description is expanded in case.

$query = "SELECT * FROM conceptos WHERE descripcion = '" . $descripcion . "'";

Secondly, have you validated the variable contents you are using, as suggested by @crotos?

The mysql_ are also deprecated, so you should use PDO, or at the least, mysqli_.

1 Comment

I know about mysql_ deprected functions, but this is an old project.
0

You can try to setup the general query log of your mysql server and see what queries are really executed. See http://dev.mysql.com/doc/refman/5.1/en/query-log.html

Also, check your encodings. Maybe your mysql connection is in ISO8859-1 and your table fields are in UTF-8 (or the opposite). Do you have any accents or special characters in your data?

1 Comment

ZeWaren maybe I must to reformulate the question, maybe the question is about how to make a query in PHP that works like in phpmyadmin, i.e what codification is used in phpmyadmin and in a php script. Thanks for the link.
0

i also faced this problem and got it solved using:

mysqli_query($con,$query);

instead of

mysql_query($query);

coz its depreciated

source:

File Downloading error from database php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.