0

I'm having a total buzz kill here. I'm trying to get a result from a query with mysql_fetch_array() but its bringing the from value. Here's a quick example:

This is the query:

$query = SELECT COUNT(*) AS total FROM rese WHERE usua_cod = '{$user_cod}';

This is the query output on the "database tool":

enter image description here

This is the PHP code:

        $sql_check = mysql_query($query) or die( mysql_error());

        if($sql_check)
        {
            $result_check = mysql_fetch_array($sql_check);
            if($result_check[0] > 0)
            {
                //Erro 
                    $Result = array('Resultado'=>"NO",
                                    "msg"=>"Você já possui uma reserva neste horário",
                                    'debug'=>$result_check[0],
                                    'query'=>"$query",
                                    'res1'=>$result_check);

                    $arrayOfChildren[] = $Result;
                    $myJSON = json_encode($arrayOfChildren);
                    echo($myJSON);
            }
            else
            {
              // DOES THE INSERT CODE
            }
        }

On the Xcode console I get this:

2014-11-26 21:10:00.784 ezmall[1250:423465] dict : (
        {
        Resultado = NO;
        debug = 1;
        msg = "Voc\U00ea j\U00e1 possui uma reserva neste hor\U00e1rio";
        query = "SELECT COUNT(*) AS total FROM rese WHERE usua_cod = '{$user_cod}'";
        res1 =         {
            0 = 1;
            total = 1;
        };
    }
)

So I thinks there's something kind of messed up because the "0" thats on "res1" should be the result for the "total" and not an index. So, any thoughts?

Cheers.

3
  • var_dump($sql_check); prints? And do you see that big red box on the manual, it is there for a reason ;) Commented Nov 26, 2014 at 23:23
  • WARNING: This is terrifyingly insecure because those parameters are not properly escaped. You should NEVER be putting $_POST data directly into the query: it creates a gigantic SQL injection bug. mysql_query is an obsolete interface and should not be used, it's being removed from PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way explains best practices. Commented Nov 26, 2014 at 23:53
  • you can see link Commented Nov 27, 2014 at 0:03

1 Answer 1

0

mysql_fetch_array() creates a result array with both numeric and named result column keys, so each result value will be in there twice. So the 0 you are seeing is the index for 'result column 0'.

You want to use mysql_fetch_row() or mysql_fetch_assoc() instead, depending on whether you want to have numeric column ids or column names as keys

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

Comments

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.