FabioRosa Posted January 18, 2018 Share Posted January 18, 2018 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 More sharing options...
Barand Posted January 18, 2018 Share Posted January 18, 2018 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.