Page 1 of 1

count function in php/mysql

Posted: Mon Apr 14, 2008 3:37 pm
by tom_1
Hello im trying to get this count function working when i had it in php its jus printin the statement and aint showing any results but works in mysql admin why is this??

$query="SELECT COUNT(*) FROM oeter WHERE monday='n' ";
echo $query;

Re: count function in php/mysql

Posted: Mon Apr 14, 2008 3:42 pm
by John Cartwright
You need to pass the query through mysql_query(), then fetch the results using mysql_fetch_assoc()..

the manual has plenty of examples

Re: count function in php/mysql

Posted: Mon Apr 14, 2008 3:53 pm
by tom_1
i got 2querys running at the same time thats y i think

Re: count function in php/mysql

Posted: Mon Apr 14, 2008 3:56 pm
by John Cartwright
Please don't use AOL speak since we have many people with english as a second language, and may not understand abbreviations.

As to your problem, I don't understand what you mean.

Post all relevant code.

Re: count function in php/mysql

Posted: Mon Apr 14, 2008 4:01 pm
by tom_1
Sorry about that his my code

Code: Select all

<!-----Display data in textboxes PHP----->
<?
 
// Standard SQL
$query ="SELECT COUNT(*) FROM peter WHERE day='no'"; 
 
 
$query = "SELECT Code FROM Records WHERE lock= '".$_SESSION['name']."'";
 
// Connect to DB server
$connection = mysql_connect("", "", "");
if (!$connection)
    die("Cannot connect to DB");
 
// Select database
mysql_select_db("", $connection)
    or die("Cannot find DB");
 
$result = mysql_query($query, $connection);
 
// Loop through data and display
while($row = mysql_fetch_array($result))
{
code = $row['Code'];
 
} 
// Close connection ! (please !)
mysql_close($connection);
 
?>

Re: count function in php/mysql

Posted: Mon Apr 14, 2008 4:41 pm
by John Cartwright
So what is the problem? I see you have 2 different $sql variables, however the first one is overwritten by the second.
You need to run the first SQL before rewritting it to a different query.. or use different variable names

Code: Select all

$sql1 = 'SELECT COUNT(*) ...';
$result1 = mysql_query($sql1) or die(mysql_error());
 
$sql2 = 'SELECT * ....';
$result1 = mysql_query($sql2) or die(mysql_error());