count function in php/mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tom_1
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2008 1:21 pm

count function in php/mysql

Post 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;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: count function in php/mysql

Post 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
tom_1
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2008 1:21 pm

Re: count function in php/mysql

Post by tom_1 »

i got 2querys running at the same time thats y i think
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: count function in php/mysql

Post 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.
tom_1
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2008 1:21 pm

Re: count function in php/mysql

Post 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);
 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: count function in php/mysql

Post 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());
Post Reply