user1957878 user1957878 - 1 year ago 78
MySQL Question

why i can't display my records in php mysql?

I have records in my database but I can't display them. Can someone check my codes, please. I'm just an amateur web developer. Thanks for any help.

$groups=mysql_query("SELECT * FROM groups ORDER BY id ASC");

$members=mysql_query("SELECT * FROM members WHERE group='$g_id'");
echo "<tr><td>$m_name<br/>($g_name)</td><td>$m_email</td></tr>";
echo "<tr><td colspan=2>Nothing to display</td></tr>";
echo "<tr><td colspan=2>Error</td></tr>";

With this code I get the
result which is
. If I remove
WHERE group='$g_id'
from the query, all of my records are displayed randomly, but I'd like to show my records (members) by group.

Answer Source

You need to escape reserved words in MySQL like group with backticks

SELECT * FROM members WHERE `group` = '$g_id'

You can also spare the inner loop when you join your data like this

select as gid, g.g_name, as mid, m.m_name, m.m_email
from groups g
inner join members m on =
order by asc

This is easier and will increase performance since you don't need to execute a lot of queries but just one.

Also please don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about Prepared Statements instead, and use PDO or MySQLi. See this article for a quick overview how to do it and why it is so important.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download