User CP
Name:  
Pass:

Register
Latest News
- RO2 multi trail...
- Sneak Peak! -D...
- Affiliate News ...
- Affiliate News ...
- Affiliate News ...
- The RIAA Defeat...
- Affiliate News ...

News Archive

Top 5 Cases
Batmobile Tumbler PC

- Batmobile Tumbler PC
     Score: 81.25
- Camo Cube
     Score: 80.00
- NemoTron
     Score: 80.00
- Wall-E
     Score: 78.33
- R2-D2
     Score: 77.00


Random Rig
Case Name: Black Box


Site Affiliates
Xoxide.com
Newegg.com
Memory Giant
ThinkGeek Electronics1and1 Web Hosting
Modders-Inc
B.C.Modding
Benchmark Reviews
KreativePC
SteelSeries
Pimpin Rigz
Lamptron


Affiliate News


  Forum Index - Web Design - trying to display the number of rows in an sql table in
Author Initial Thread
sP00N
Modding Wizard

Joined: Jul 5, 2007
Posts: 776
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
- Top - Profile - Private Message - Website - Reply

faceless105
Selfmade Modder

Joined: Jun 30, 2006
Posts: 4540
Post Subject: subject
Posted on: Nov 13, 2007


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,

$conn = mysql_pconnect
mysql_select_db($db, $conn);
$sql = SELECT * FROM users";
$res = mysql_query($sql, $conn);
$total = mysql_num_rows($res);

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?
- Top - Profile - Private Message - Website - Quote - Reply

faceless105
Selfmade Modder

Joined: Jun 30, 2006
Posts: 4540
Post Subject: subject
Posted on: Nov 13, 2007


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?
- Top - Profile - Private Message - Website - Quote - Reply

sP00N
Modding Wizard

Joined: Jul 5, 2007
Posts: 776
Post Subject: subject
Posted on: Nov 14, 2007


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
- Top - Profile - Private Message - Website - Quote - Reply

squeemonkey
CMG Refugee

Joined: Jul 6, 2007
Posts: 834
Post Subject: subject
Posted on: Nov 14, 2007


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.


Coming soon to a garage near you...
- Top - Profile - Private Message - Website - Quote - Reply

faceless105
Selfmade Modder

Joined: Jun 30, 2006
Posts: 4540
Post Subject: subject
Posted on: Nov 14, 2007


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?
- Top - Profile - Private Message - Website - Quote - Reply

sP00N
Modding Wizard

Joined: Jul 5, 2007
Posts: 776
Post Subject: subject
Posted on: Nov 14, 2007


changed it and it still gives me the same error. >.<


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
- Top - Profile - Private Message - Website - Quote - Reply

faceless105
Selfmade Modder

Joined: Jun 30, 2006
Posts: 4540
Post Subject: subject
Posted on: Nov 14, 2007


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?
- Top - Profile - Private Message - Website - Quote - Reply

sP00N
Modding Wizard

Joined: Jul 5, 2007
Posts: 776
Post Subject: subject
Posted on: Nov 14, 2007


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
- Top - Profile - Private Message - Website - Quote - Reply

faceless105
Selfmade Modder

Joined: Jun 30, 2006
Posts: 4540
Post Subject: subject
Posted on: Nov 14, 2007


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?
- Top - Profile - Private Message - Website - Quote - Reply

squeemonkey
CMG Refugee

Joined: Jul 6, 2007
Posts: 834
Post Subject: subject
Posted on: Nov 14, 2007


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]


Coming soon to a garage near you...
- Top - Profile - Private Message - Website - Quote - Reply

faceless105
Selfmade Modder

Joined: Jun 30, 2006
Posts: 4540
Post Subject: subject
Posted on: Nov 14, 2007


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?
- Top - Profile - Private Message - Website - Quote - Reply

sP00N
Modding Wizard

Joined: Jul 5, 2007
Posts: 776
Post Subject:
Posted on: Nov 15, 2007


fixed it by going to here:

http://www.codingfor.....ex.php?t-120318.html

:D yay!

Heres the fruits of my labor:

http://www.powerplay......net/testphp/PPGCMS/


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
- Top - Profile - Private Message - Website - Quote - Reply







FusionMods.Net

Total Time: 5.62639