I have a problem with mysql_fetch_array. I'm trying to do this:
if(count(mysql_fetch_array(mysql_query($peticion)))) {
array_push($errores, $texto['conta_existe']);
}
The previous code should detect if mysql_fetch_array found a row in my database and, if it found at least one, an array should be pushed. Problem is that if I haven't got anything in my db, fetch_array returns the number "1".
I tried to find what's happening with this peace of code:
$arrayc = mysql_fetch_array(mysql_query($peticion), MYSQL_NUM);
echo 'PRINT_R: ';
echo print_r($arrayc);
echo '<br>COUNT: ';
echo count($arrayc);
And returns this:
PRINT_R: Array ( [0] => FLEREX [conta] => FLEREX ) 1
COUNT: 2
I don't understand why there's that number one there, after the array. The previous quote was returned with only one row, to show you the array; but if there's not any row in the db, this is what I get:
PRINT_R: 1
COUNT: 1
I don't know where comes from that one, but is always there.
Thanks for reading and sorry for my bad english.
Try mysql_num_rows instead of count and delete the mysql_fetch_array
mysql_fetch_array
doesn't return an array of all the results, it just returns the next row of results, or false
if there are no more results. So count()
returns the number of columns multiplied by 2 (because the array contains both numeric and associative entries).
Use mysql_num_rows
to get the number of rows in the results.
if (mysql_num_rows(mysql_query($peticion))) {