Page 1 of 1
Assign variable mysql count
Posted: Fri Mar 12, 2004 5:06 pm
by kerick
I want to assign $count the value of the following sql query
$SQL = "select count(*) from sublist;"
how do I do this.
Posted: Fri Mar 12, 2004 5:11 pm
by andre_c
Posted: Fri Mar 12, 2004 5:22 pm
by Illusionist
Code: Select all
$SQL = "select count(*) as count from sublist";
$result = mysql_query($SQL);
$result = mysql_fetch_assoc($result);
$count = $result['count'];
echo $count;
But if you just trying to get the number of rows, try this:
Code: Select all
$SQL = "select * from sublist";
$result = mysql_query($SQL);
$count = mysql_num_rows($result);
echo $count;
not workng
Posted: Fri Mar 12, 2004 5:43 pm
by kerick
I've stripped down the php and now have this
<?php
$usr ="root";
$pwd="test";
$db = "master";
$host ="localhost";
#connect to database
$cid = mysql_connect ($host, $usr,$pwd);
if (!$cid) {echo ("Error:") . mysql_error() . "\n"); }
?>
<?php
$SQL ="select * from sublist";
$result = mysql_query($SQL) or die ("die1");
$count = mysql_num_rows ($result) or die ("die2");
echo $count;
echo $SQL;
?>
I get die1
if I remove the or die I get an echo of the sql
Posted: Fri Mar 12, 2004 5:47 pm
by andre_c
You need to select the database:
Code: Select all
$db = mysql_select_db("$db", $cid)
or die ("Couldn't select database.");
also do die('Error 1:'.mysql_error()) instead of die1 and die2