3

i have a really strange problem.

This is a part of a .php page:

$query="SELECT idlezione, nomemateria, oinizio, ofine FROM materia, lezione, ora, giorno WHERE materia.idmateria=lezione.idmateria AND ora.idora=lezione.idora AND giorno.idgiorno=lezione.giorno AND nomegiorno=\"$nomeg\" AND iddocente=\"$docente\" AND idclasse=\"$classe\" ORDER BY oinizio;";
    echo $query;
    $res=mysql_query($query,$dbconn);
    echo mysql_num_rows($res);

Now, when i copy the result of echo $query; to phpmyadmin it returns me 2 rows, but in my .php echo mysql_num_rows($res); returns "0".

If i use a simple query like SELECT * FROM materia; it returns many rows..

UPDATE: Everything was on altervista.org, now i tried moving my db on local and running the query (using XAMPP) and everyhing works fine.. Why?

XandruDavid
  • 67
  • 1
  • 8
  • Php can run complicated queries.. give detail about your table shema – Ananta Prasad Jun 15 '15 at 15:05
  • 1
    How can my table schema be a problem if the query runs perfectly in phpmyadmin? – XandruDavid Jun 15 '15 at 15:10
  • Just as a note: `mysql_` functions have been deprecated for a long time and are removed as of PHP 7 (due later this year). This is because they are unsafe. I highly recommend switching to `mysqli_` or better `PDO` or even better still, an ORM such as Doctrine. – DanielM Jun 15 '15 at 15:17
  • I know, thanks, but this is not a solution at my problem... – XandruDavid Jun 15 '15 at 15:18
  • That's why it's "Just a note" and not an answer. ;) Still looking at the problem. – DanielM Jun 15 '15 at 15:19
  • There doesn't seem to be anything wrong with the query itself which leads me to wonder if the `$dbconn` is connected to the same database as your phpMyAdmin (also don't use phpMyAdmin, that's a security risk too :p) – DanielM Jun 15 '15 at 15:23
  • any other query in that same file works just fine, so im pretty sure it has nothing to do with $dbconn, just this last one isnt returning any data – XandruDavid Jun 15 '15 at 15:31
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 15 '15 at 15:54
  • thanks but that's not the point... – XandruDavid Jun 15 '15 at 16:15
  • Add a test after mysql_query: if ($res === FALSE) echo mysql_error($dbconn); – AndrewQ Jun 15 '15 at 17:54
  • 2
    Try to print mysql_error() after executing the query to be sure there's nothing wrong on it. Anyway, like everybody else, I insist that you should use PDO or mysqli_ because going with deprecated commands is plainly wrong – PerroVerd Jun 16 '15 at 08:29
  • I updated the post, is not a problem with mysql functions... – XandruDavid Jun 16 '15 at 12:59
  • You might try to execute `SET SQL_BIG_SELECTS=1` on your connection before running your statements – king_nak Jun 16 '15 at 13:09
  • Maybe its because of the comma on your query: DER BY oinizio**;**. Try removing it – CarlosCarucce Jan 20 '16 at 16:47
  • As far as I know, mysql uses single quotes only too... try changing for example this `nomegiorno=\"$nomeg\"` to `nomegiorno='$nomeg'` – CarlosCarucce Jan 20 '16 at 16:49

1 Answers1

-1

I suggest u use PDO and prepared statements.

Here http://docs.php.net/pdo.prepared-statements an introduction to pdo and prepared statements (including bound parameters).