Print a php code

Hello,

why the following code is not printed out ?

<?php 
	$numberLinks="SELECT COUNT (id) FROM linksBegin";
	$printNumber=mysql_query($numberLinks);
	echo "$printNumber";
	?>

no error message appears in the page, but also nothing else appears , why ?

Turning on display_errors while you are testing will make it much easier to find and fix the errors.

You would of course remove that line after you finish testing.

Since the call returns false if it can’t assign a resource you don’t really need to check specifically that it is a resource (although doing so can help to reduce the possibility of errors). Simply not having the field evaluate to false is sufficient to indicate that it contais a resource given that the preceding statement will assign either a resource or false.

Check out the manual. :wink:

[fphp]ini_set[/fphp] & [fphp]is_resource[/fphp]


<?php

error_reporting(-1);
ini_set('display_errors', true);

$res = mysql_query('SELECT COUNT(id) FROM linksBegin;');

$num = 0;
if(is_resource($res)){
  list($num) = mysql_fetch_array($res, MYSQL_NUM);
}
echo $num;

?>

fetch the rows

That means that the query is now working since it returns the resource that provides the link to the database (which is what all sql query calls return if they successfully connect to the database).

To be able to see the value(s) returned by the query you need to actually fetch the rows returned and extract the fields that those rows contain.

it gives me the following result :
Resource id #8

I don’t know why, the number should about 2030, and I don’t know why the word RESOURCE ??

thanks for your help

Try getting rid of the blank between COUNT and (id) which is causing the query to fail.

what you mean when you say ( fetch the rows ) ? can you provide me a sample code please ?

Thanks for you all, I appreciate your help.
AnthonySterling your code is working properly, but I like to understand it please, why you use ( ini_set ) and ( is_resource ) in your code ? I mean what is their roles exactly ?

This is correct. When you echo the executed query it will return you the resource id.

If you want to print the number of row affected try this…

if($printNumber){
echo mysql_num_rows($printNumber);
}