1

I have a mysql join query for retrieving the users a user subscribed to.

SELECT * FROM (subscriptions) JOIN users ON users.userid = subscriptions.following WHERE userid = $userid ORDER BY subscribed desc

In order to ensure that the query is correct, is there a php function that I can use to see what I produced? Maybe get a table structure of the query like below. Or is there another way to get a visual table design of my join?

+----+-------------+-------------------+
| ID | some column | some column       |
+----+-------------+-------------------+
| 1  | 102         | 213               |
| 2  | 64          | 23                |
| 3  | 4           | 344               |
+----+-------------+-------------------+

EDIT: It seems that I can test the query in phpmyadmin which produces a table of my join query. This is a great way to get a visual of your query!

1
  • 4
    Could you just test your query on the mysql> command line? mysql -u username -p. You'll be prompted for your database password. Then use dbname; Then type your query and see what scrolls across your screen. Commented Jul 19, 2011 at 5:09

4 Answers 4

1

Not necessarily a single function, but you can create yourself one using:

  • mysql_fetchassoc() to get the resultset as key(col name)/value(col value) pairs
  • array_keys() to get all the column names from resultset and create your table header
Sign up to request clarification or add additional context in comments.

Comments

1

Not with PHP. The best you could do is to loop through it and create a table. It might be better to just use something like HeidiSQL to visually look at your queries.

Comments

1

It's not clear, what you mean. To examine a JOIN in MySQL do a EXPLAIN SELECT * FROM (subscriptions) JOIN users ON users.userid = subscriptions.following WHERE userid = $userid ORDER BY subscribed desc and output this result in PHP

Comments

1

It's not clear what you are asking for.

If you want a generic way to test that the results are as you expect - there's no way to this without detailed knowledge of the data.

You shouldn't be testing if your query is correct at run time. And there is no absolute value of 'correct' to measure against.

If you just want to see the meta-data regarding the columns in the returned dataset, use mysql_fetch_field()

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.