Post Subject: trying to display the number of rows in an sql table in
Posted on: Nov 13, 2007
I'm trying to display the number of rows in an sql table in php, does this look correct. The table is called users, and the db connects by an external file by using mysql_pconnect
mysql_select_db("users", $db);
$member_total_stat1 = mysql_query("SHOW * FROM users, $db");
$member_total_stat = mysql_num_rows($member_total_stat1);
it either gives me an error for the second or 3rd line mentioned.
steamid: dead7iest_weap0n
AMD X4 720 @ 3.5GHZ, 4GB DDR2, PowerColor 5770 1GB, 40" LCD, 200gb+ 2TB HDD's
Now A+, Net+, Linux+ and MCTS Windows 7 and Server '08 Certified :D
thats close, youd do something more along the lines of...
mysql_pconnect
mysql_select_db($db);
$sql = mysql_query("SELECT * FROM users");
$res = mysql_query($sql);
$total = mysql_num_rows($res);
Now I do mine a little different but I've never used pconnect, the only difference would be if I treated pconnect like connect would be,
where I do specify the $conn, I beleive those functions will use the last connection you made by default. The mysql_select_db connects to the db first and you can specify the connection. you only need to specify the table in the sql statement.
The mysql_num_rows can also only work once you've performed the query with mysql_query. that as well doesn't require the connection to be in the function, but i usually do for good measure.
let me know if that helps or you have any other questions.
If con is the opposite of pro, is Congress the opposite of progress?
also try never to use SELECT * unless it's necessary, it wasts bandwidth and time since your transfering so much unnecessary info. this could be easily accomplished by SELECT userID form users. or SELECT COUNT(userID) as total from users and then grabbing it with $rows = mysql_fetch_rows($res); then... $total = $rows[0]; the $res would be your $res = mysql_query($sql);
that may make things a little more confusing since aggregrates can sometimes seem like a lot (still do to me at times, lol), but thats the most effecient way to do that. though you won't notice any real slow ups until you start doing that with pretty large tables.
If con is the opposite of pro, is Congress the opposite of progress?
Its giving me this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/powerpla/public_html/testphp/PPGCMS/base/sidebar.php on line 8
this is the new code:
mysql_select_db($db);
$sql = mysql_query("SELECT COUNT(user_id) * FROM users");
$res = mysql_query($sql);
$total = mysql_num_rows($res); <-line 8
steamid: dead7iest_weap0n
AMD X4 720 @ 3.5GHZ, 4GB DDR2, PowerColor 5770 1GB, 40" LCD, 200gb+ 2TB HDD's
Now A+, Net+, Linux+ and MCTS Windows 7 and Server '08 Certified :D
faceless, learn foxpro so you can optimize our inventory tables... I cant stand to look at them any more but I'm not technically supposed to program at all so I dont fix it.
spoon, change $sql = mysql_query("SELECT COUNT(user_id) * FROM users"); to this, $sql = "SELECT COUNT(user_id) * FROM users";. the sql is jsut a string :)
ad squeemonkey, are you talkin about the html tables or my database tables? if it's the html tables, i think thats a spot where the fact that i'm a developer and not a designer shows the most, They work and thats where i left them, lol
If con is the opposite of pro, is Congress the opposite of progress?
that error generally comes up when your sql didn't work, I just noticed one other error we missed, the *. this eliminates the need for the *.
$sql = "SELECT COUNT(user_id) as total FROM users";
$res = mysql_query($sql,$conn);
$row = mysql_fetch_rows($res);
$totalUsers = $row[0];
using the aggregrate COUNT will make it so theres only one return value. thats why I use mysql_fetch_row instead of array since e're not looping through records. at the same time that makes mysql_num_rows inaccurate since all that does is count the rows recurned which in this instance would be 1. if you instead just use * instea dof the count, then mysql_num_rows would work, but thats a slight waset of bandwidth.
If con is the opposite of pro, is Congress the opposite of progress?
arg it seems that it hates that 3rd line of code no matter what I change it too:
Fatal error: Call to undefined function: mysql_fetch_rows() in /home/powerpla/public_html/testphp/PPGCMS/base/sidebar.php on line 4
heres the code:
$sql = "SELECT COUNT(user_id) as total FROM users";
$res = mysql_query($sql,$dbh);
$row = mysql_fetch_rows($res);
$totalUsers = $row[0];
$dbh is pointing to the connection settings code.
steamid: dead7iest_weap0n
AMD X4 720 @ 3.5GHZ, 4GB DDR2, PowerColor 5770 1GB, 40" LCD, 200gb+ 2TB HDD's
Now A+, Net+, Linux+ and MCTS Windows 7 and Server '08 Certified :D
i'm guessing your new error is in this line...
$res = mysql_query($sql,$dbh);
is $dbh refference mysql_select_db or mysql_connect? it's susposed to refference the mysql_connect. if it's causing a real error, then you can remove that refference and make it into this...
$res = mysql_query($sql);
If con is the opposite of pro, is Congress the opposite of progress?
I was saying if you can optimize the freaking db files that get built in fox then you can have a job here... we dont pay much and the benefits aren't all that great but I'll be damn if we dont have fun [crickets chirp]
haha I can understand the fun, I had to pull a 12 hour work day yesterday. Right now I'm working a pretty good job for a pretty huge company. it's great experience and will look even better on a resume for future jobs :)
If con is the opposite of pro, is Congress the opposite of progress?