Jump to content

Is this possible?


beaux1

Recommended Posts

If you aren't going to be using all of the data that your select query returns, then it is very inefficient to query the database and have it return potentially a lot of data for only a call to mysql_num_rows.  If you only want a row count, use the count function in mysql...

 

$result = mysql_query("SELECT COUNT(items) FROM item_table") or die(mysql_error());
$num_of_rows = mysql_result($result, 0);

Link to comment
Share on other sites

Actually if it's a table of like 99999 items, and you're doing lots of queries, mysql_num_rows could be bad.

 

I would do something like (assumed every row has a column named id):

$q = mysql_query("SELECT COUNT('id') AS count FROM items");

$r = mysql_fetch_assoc($q);

$number_of_item_rows = $r['count'];

 

Gah someone beat me to it while I was posting :P

Link to comment
Share on other sites

It's a mathematical count, so I have to use SELECT SUM(item_count) FROM items. Items is a row in the information table. I'm really a newb at all of this, so bear with my poor attempt to do this. Items is a row in information.

 

<?php

include 'config.php';

include 'opendb.php';

$sql = "SELECT SUM(item_count) FROM information

echo $sum['item_count'];

include 'closedb.php';

?>

 

But this isn't going to work, so yeah, what should I do?

Link to comment
Share on other sites

$sql = "SELECT SUM(item_count) FROM information
echo $sum['item_count'];

 

should be

 

$sql = "SELECT SUM(item_count) AS item_count FROM information";
$result = mysql_query($sql);
$sum = mysql_fetch_assoc($result);
echo $sum['item_count'];

 

or

 

$sql = "SELECT SUM(item_count) FROM information";
$result = mysql_query($sql);
echo mysql_result($result, 0);

Link to comment
Share on other sites

Ok, problem. I'm actually counting a row called helmet. So I just renamed it, added the or die(mysql_error()); (as I learnt off you in another topic, hitman =P)

So my code is:

<?php
include 'config.php';
include 'opendb.php';
$sql = "SELECT SUM(helmet_count) FROM information";
$result = mysql_query($sql) or die(mysql_error()); 
echo mysql_result($result, 0);
include 'closedb.php';
?>

Unknown column 'helmet_count' in 'field list'

 

The column helmet is there, is there something I'm not picking up here?

Link to comment
Share on other sites

See that works:

<?php
include 'config.php';
include 'opendb.php';
$sql = "SELECT count(helmet) AS helmet_count FROM information";
$result = mysql_query($sql) or die(mysql_error()); 
echo mysql_result($result, 0);
include 'closedb.php';
?>

 

Except, it's not what I want. That is counting the number of rows, I want to count the integers in every line under the helmet row.

 

So like:

|helmet|

2

3

1

 

PHP Script will count them and output would be = 6

Link to comment
Share on other sites

Wewt, works, I just replaced SELECT count(helmet with SELECT sum(helmet.

 

<?php
include 'config.php';
include 'opendb.php';
$sql = "SELECT sum(helmet) AS helmet_count FROM information";
$result = mysql_query($sql) or die(mysql_error()); 
echo mysql_result($result, 0);
include 'closedb.php';
?>

 

Now, anyone know how I would go about calculating multiple rows?

Link to comment
Share on other sites

Same way, just separate each column by a comma...

 

$query = "SELECT SUM(helmet) AS helmet_count, SUM(shoe) AS shoe_count, SUM(sock) AS sock_count FROM information";
$result = mysql_query($query) or die(mysql_error());

$helmet_count = mysql_result($result, 0, "helmet_count");
$shoe_count = mysql_result($result, 0, "shoe_count");
$sock_count = mysql_result($result, 0, "sock_count");

Link to comment
Share on other sites

<?php
include 'config.php';
include 'opendb.php';
$query = "SELECT SUM(helmet) AS helmet_count, SUM(belt) AS belt_count, SUM(earplugs) AS earplugs_count FROM information";
$result = mysql_query($query) or die(mysql_error());

$helmet_count = mysql_result($result, 0, "helmet_count");
$belt_count = mysql_result($result, 0, "belt_count");
$earplugs_count = mysql_result($result, 0, "earplugs_count");
echo mysql_result($result, 0);
include 'closedb.php';
?>

 

It's only echo-ing helmet_count, though.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.