trilbyfish Posted April 28, 2008 Share Posted April 28, 2008 hi i have the sql query SELECT COUNT( * ) FROM `bookingtable` WHERE `eight` = '-' and the output is 2. how do i echo the output using php? I know it is probably right in front of me, but i cant find the solution. The php code i have at the moment is <?php require_once ('../project/connect.php'); // Connect to the db. $query = "SELECT COUNT(*) FROM `bookingtable` WHERE `eight` = '-'"; $result = @mysql_query ($query); // Run the query. $row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable. echo ($row['count(*)']); ?> The output of this is a blank page. If anyone can tell me what i need to echo, or if there are any problems above that, i would be grateful. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/103321-sholud-be-easy-to-solve/ Share on other sites More sharing options...
revraz Posted April 28, 2008 Share Posted April 28, 2008 Check this out and scroll down to the "As" part http://www.techonthenet.com/sql/count.php Quote Link to comment https://forums.phpfreaks.com/topic/103321-sholud-be-easy-to-solve/#findComment-529156 Share on other sites More sharing options...
moselkady Posted April 28, 2008 Share Posted April 28, 2008 You cannot use count( as an array index. You can try: echo $row[0]; Another way is to assign a name to COUNT(*) in your query and use that name as array index: $query = "SELECT COUNT(*) AS mycount FROM `bookingtable` WHERE `eight` = '-'"; ... ... echo $row['mycount']; Quote Link to comment https://forums.phpfreaks.com/topic/103321-sholud-be-easy-to-solve/#findComment-529157 Share on other sites More sharing options...
sasa Posted April 29, 2008 Share Posted April 29, 2008 echo ($row['COUNT(*)']); Quote Link to comment https://forums.phpfreaks.com/topic/103321-sholud-be-easy-to-solve/#findComment-529326 Share on other sites More sharing options...
trilbyfish Posted April 29, 2008 Author Share Posted April 29, 2008 Ok thanks for the help. The only thing that seems to work for me is echo $row[0]; Is there a way to do this for multiple columns? ive tried changing the number in the $row[0] but that just echoes nothing. Quote Link to comment https://forums.phpfreaks.com/topic/103321-sholud-be-easy-to-solve/#findComment-529684 Share on other sites More sharing options...
moselkady Posted May 1, 2008 Share Posted May 1, 2008 If you select more than one field, the array $row will have their values in the order you put in SELECT statement <?php ...... $query = "SELECT field1, field2, field3 FROM `bookingtable` WHERE `eight` = '-'"; $result = @mysql_query ($query); // Run the query. $row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable. echo $row[0]; // field1 echo $row[1]; // field2 echo $row[2]; // field3 ?> Another thing you code do is user print_r($row) to print all the values in the array and check its structure Quote Link to comment https://forums.phpfreaks.com/topic/103321-sholud-be-easy-to-solve/#findComment-530815 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.