Vincent, your FAQ #4 is a great intro. I just have one suggest of an alternate coding that reduces all the nesting and moves the error messages right next to the conditions that trigger them:
$sql = "SELECT * FROM table";
// Step 1: connect
if (! ($conn_id = mysql_connect($host,$user,$pwd)))
echo 'Could not connect to database server';
// Step 2: select database
elseif ( ! mysql_select_db($database, $conn_id))
echo 'Could not select database';
// Step 3: issue query
elseif ( ! ($result = mysql_query($sql, $conn_id)))
{
// Step 3 failed, non-fatal
// (might want to run other queries after this)
echo 'SQL query failed:'.mysql_error()."<BR>".$sql;
}
// Step 4: Find out if any results
// (NOTE: this is only appropriate for SELECT queries;
// INSERT, UPDATE, and DELETE need mysql_affected_rows()
// and of course you don't fetch anything afterward.)
else if ( mysql_num_rows($result) < 1 )
{
// Step 4 failed, non-fatal, No results found
echo 'Your query did not return any results';
}
// Step 5
// Yay, results found, process them
else
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
print_r($row);
}
}