1

I want to Count a number of elements in a MySQL Table with the PHP element count, but when i try to give out the result it print 'Resource id #5' which is of course the id for succeeded MYSQL srcipts. If I type it in the SQL console it says I have got Syntax Error (#1064). Thats my code:

<?php
$dbhost = >>hostname<<;
$dbuser = >>user<<;
$dbpass = >>password<<;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
 die('Could not connect: ' . mysql_error());
}
mysql_select_db(>>database to be selected<<)
or die ("Database couldn´t be found");

echo mysql_query('SELECT COUNT(*) FROM table'); ?>

Which mysql_fetch_ do i have to use?

Thanks for any effords and a happy new year

Tim

3

2 Answers 2

2

You have to fetch the result from the query.

$result = mysql_query('SELECT COUNT(*) AS count FROM table');
$row = mysql_fetch_assoc($result);
echo $row['count'];

You should also convert from the mysql extension to PDO or mysqli, but the basic structure is the same -- after performing a query you have to fetch the results as a separate step.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked. I will probably write the website finished first and rewrite it for PDO or mysqli afterwards.
0

If you just looking for:

Which mysql_fetch_ do i have to use?

Than you can try these:

mysql_fetch_assoc();
mysql_fetch_array();
mysql_fetch_object();

Side note: Also remember what Mr. Barmar said about the **PDO or mysqli_* **

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.