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'].
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.