CyberJunkie
CyberJunkie

Reputation: 22684

Mysql return table structure from query

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!

Upvotes: 1

Views: 583

Answers (4)

symcbean
symcbean

Reputation: 48387

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()

Upvotes: 1

Tudor Constantin
Tudor Constantin

Reputation: 26871

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

Upvotes: 1

rabudde
rabudde

Reputation: 7722

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

Upvotes: 1

JohnP
JohnP

Reputation: 50029

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.

Upvotes: 1

Related Questions