Jump to content

Array to String Conversion not working


FabioRosa

Recommended Posts

 

Hello everyone,

 

A simple function is giving me a headache. Maybe someone can clarify what the problem is:

<?php

function count_all_stores_by_local_city($loc,$cit){
        global $connection;
        $query = "SELECT COUNT(*) FROM tbl_stores WHERE id_local = {$loc} AND id_city = {$cit}";
        $count = mysqli_query($connection,$query);
        confirm_query($count);
        return $count;

}

?>

...

 

<?php
        if(isset($_POST['submit'])){
            $local = $_POST["local"];
            $city = $_POST["city"];
            $result_set = find_all_stores_by_local_city($local, $cit);
            $count = mysqli_fetch_assoc(count_all_stores_by_local_city($local, $city));
    ?>
    <h2>Stores Found: <?php echo $count; ?> </h2>
 

I understand the $count is an object, a print_r gives me the result, but not in the desired format. What to do? It seems something so simple but I think I'm not seeing the point.

 

Thanks in advance!

 

Link to comment
Share on other sites

Are $loc and $cit numeric or string values?

 

If strings, they need to be in single quotes within the query.

 

You also need to check if the query failed or not. ($count will be 'false' if it failed. If so, check the error message value.

 

Avoid embedding function calls within function calls - it prevents you from error checking.

 

After calling the function, $count is an array. The value you want will be in the first element. Use a column alias in the query when using sql functions eg

SELECT COUNT(*) as total FROM ...
then it's easy to reference the required array element as $count['total']. Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.