Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/103321-sholud-be-easy-to-solve/
Share on other sites

You cannot use count(8) 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'];

 

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

 

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.