0

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.

6
  • 1
    echo print_r($arrayc);.... print_r($arrayc) outputs the value of $arrayc, and returns a booleam true; that you're then echoing that returned value, and which is displayed as 1 Commented Apr 18, 2014 at 16:42
  • 1
    remove echo here echo print_r($arrayc) Commented Apr 18, 2014 at 16:43
  • 3
    Just a heads-up, start using PDO or mysqli functions. They're safer and mysql_* functions are (going to be?) deprecated. Commented Apr 18, 2014 at 16:43
  • mysql_* ARE deprecated as of PHP 5.5 Commented Apr 18, 2014 at 16:45
  • @Bono the extension is deprecated as of PHP 5.5.0. Commented Apr 18, 2014 at 16:45

2 Answers 2

2

Try mysql_num_rows instead of count and delete the mysql_fetch_array

Sign up to request clarification or add additional context in comments.

Comments

0

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))) {

1 Comment

We posted our answers at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.