Mysql
 sql >> Database >  >> RDS >> Mysql

Verifica se mysql_query ha restituito qualcosa o meno

La risposta di Jeremy Ruten sopra è buona e viene eseguita rapidamente; d'altra parte, ti dà solo il numero di righe e nient'altro (se vuoi i dati del risultato, devi interrogare nuovamente il database). Cosa uso:

// only ask for the columns that interest you (SELECT * can slow down the query)
$query = "SELECT some_column, some_other_column, yet_another_column FROM `table`";
$results = mysql_query($query, $connection);
$numResults = mysql_num_rows($results);
if ($numResults > 0) {
   // there are some results, retrieve them normally (e.g. with mysql_fetch_assoc())
} else {
   // no data from query, react accordingly
}