I want to assign $count the value of the following sql query
$SQL = "select count(*) from sublist;"
how do I do this.
Assign variable mysql count
Moderator: General Moderators
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
Code: Select all
$count = mysql_result($SQL, 0);-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
Code: Select all
$SQL = "select count(*) as count from sublist";
$result = mysql_query($SQL);
$result = mysql_fetch_assoc($result);
$count = $result['count'];
echo $count;Code: Select all
$SQL = "select * from sublist";
$result = mysql_query($SQL);
$count = mysql_num_rows($result);
echo $count;not workng
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
<?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
Last edited by kerick on Fri Mar 12, 2004 5:49 pm, edited 1 time in total.
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
You need to select the database:
also do die('Error 1:'.mysql_error()) instead of die1 and die2
Code: Select all
$db = mysql_select_db("$db", $cid)
or die ("Couldn't select database.");