-4

I'm trying to VIEW (print or echo) on the page the actual query that is being sent by the following:

$req1 = mysql_query('select m1.id, m1.title, m1.date_time, count(m2.id) as reps, users.username as userid, users.username from pm as m1, pm as m2,users where ((m1.user1="'.$_SESSION['username'].'" and m1.user1read="no" and users.id=m1.user2) or (m1.user2="'.$_SESSION['username'].'" and m1.user2read="no" and users.id=m1.user1)) and m1.id2="1" and m2.id=m1.id group by m1.id order by m1.id desc'); 

I've tried looking all over the net for a way to do this but "echo $req1" just returns a resource ID. Something is wrong, probably with the username variable, and it's returning no records. Could someone please explain how to get the actual QUERY, as it's running with username variables, etc. to print? I can't figure out how to do it.

Thank you in advance for your genuis!!! :)

  • 1
    The easiest way? Instead of passing the SQL straight to `mysql_query()`, assign it to a variable first. You can then pass that variable to `mysql_query()`, as well as echo it out. – andrewsi Aug 23 '13 at 15:33
  • Isn't that what the $req1 is? I'm confused. What would that look like? – user2672667 Aug 23 '13 at 15:33
  • @andrewsi gave you the answer you need. This is also good practice from a code readability standpoint. I think the question should be a closed however as I don't know that I've seen a better case of not meeting the "minimal understanding" criteria. – Mike Brant Aug 23 '13 at 15:35
  • Here you go: http://stackoverflow.com/questions/4794927/why-does-this-return-resource-id-2 – bobbiloo Aug 23 '13 at 15:36
  • Thanks to Smokey for the demonstration below. For us new kids who are just learning, some of the terms are still confusing and an example is very helpful. :) – user2672667 Aug 23 '13 at 15:40
  • @user2672667 - it can get a little confusing. your `$req1` contains the results of the `mysql_query()` function call - it's either going to be a set of all the records, if the query succeeds, or `false`, if there was a problem. What me and SmokeyPHP both suggested was to create a string with your SQL in it; you can pass that into `mysql_query()` as well as print it out. – andrewsi Aug 23 '13 at 15:46
  • @user2672667 - does that help any, or have I made it more confusing? – andrewsi Aug 23 '13 at 15:46

2 Answers2

3
$sql = 'select m1.id, m1.title, m1.date_time, count(m2.id) as reps, users.username as userid, users.username from pm as m1, pm as m2,users where ((m1.user1="'.$_SESSION['username'].'" and m1.user1read="no" and users.id=m1.user2) or (m1.user2="'.$_SESSION['username'].'" and m1.user2read="no" and users.id=m1.user1)) and m1.id2="1" and m2.id=m1.id group by m1.id order by m1.id desc';
var_dump($sql);
//then to run it
$req1 = mysql_query($sql);

Though the mysql_* functions are deprecated - you should be using mysqli or PDO

MDEV
  • 10,730
  • 2
  • 33
  • 49
0

You can look in the logs of Mysql. On a Linux system, those are in the /var/log directory. You also need to start your Mysql server with specific options, depending on the version. You will find more infos here: http://dev.mysql.com/doc/refman/5.1/en/query-log.html

Be careful: Logging queries has an important impact on performance, so I would not recommand to do that on a production server.

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124