Entry
Syntax for mysql_select_db() and mysql_query()?
Feb 2nd, 2004 07:53
Michael Felt, Alexander Berkman, Dan Martin,
$connection = mysql_connect($host, $user, $pass);
mysql_select_db("my_database");
$query = "SELECT * FROM my_table";
$result = mysql_query($query, $connection);
Now, if $result != NULL you can start to read your results.
The following is just one example:
accessing the result (row) as an object.
==================>
$query = "SELECT username,ban_id,user_email from my_table";
$result = mysql_query($query);
// the number of rows in the result can be determined with
$rows = intval(mysql_num_rows($result));
<table BORDER=2>
<TR><TH COLSPAN=3 align='center'>WebSite Registered Accounts</th></tr>
<TD valign=top>
<H3>
<?php echo($rows)?> banned accounts.
</h3>
<?php
echo "<TABLE BORDER=1>\n<TR><TH>USERNAME</th><TH>E-MAIL</th></tr>";
// loop, fetching and processing the entire row as an object
while ($row = mysql_fetch_object($result)) {
print "<TR><TD>";
# use "normal -> syntax to get to variables named in SELECT statement
// print "$row->ban_id"; # not printing this in table for now
// print "</td><TD>"; # not printing this in table for now
print "$row->username";
print "</td><TD>";
print "$row->user_email";
print "</td></tr>";
}
</table>
// a more complex select statement example:
$sql = "SELECT user_email,username FROM s_users, s_banlist
USE INDEX (ban_userid)
WHERE user_active<1 AND username != 'Anonymous' AND
NOT (USER_ID = BAN_USERID) ORDER BY username ASC";