michael
michael

Reputation: 3

How do you query two variables in order to return one result?

I have a database with columns assigned username, datePro, and quantity. $datePro is a DATE that a certain $quantity of material is produced from $username. I'm trying to query results to echo the $quantity of material that is produced during a certain month from $username.

I've experimented and searched for help already posted but cannot seem to figure this out on my own. Any guidance is appreciated. here's what I have...

$resultDate = mysql_query ("SELECT datePro FROM power WHERE username = '$username' ") or die(mysql_error()) ;

$resultQuant = mysql_query("SELECT quantity FROM power WHERE username = '$username' ") or die(mysql_error()) ;

$datePro = mysql_fetch_row($resultDate);
$quantity = mysql_fetch_row($resultQuant);

if ($datePro = '%Y-01-%d') echo $quantity;

Upvotes: 0

Views: 311

Answers (3)

horatio
horatio

Reputation: 1436

If you can have multiple rows for the same datePro and username, then you can use SUM to total all entries:

SELECT sum(quantity) where username='$username' and datePro=date($dateToLookup)

(The date comparison will depend on how you store the date in the table.)

if there can be only one, then

SELECT quantity where username='$username' and datePro=date($dateToLookup)

Both will return one row, with one field, which you can output or store.

Upvotes: 0

Chris Baker
Chris Baker

Reputation: 50612

Are you after this:

$queryObj = mysql_query ("SELECT datePro, quantity  FROM power WHERE username = '$username' ") or die(mysql_error()) ;

$row = mysql_fetch_assoc($queryObj );


if ($row['datePro'] == '%Y-01-%d') echo $row['quantity'];

Upvotes: 1

KJYe.Name
KJYe.Name

Reputation: 17179

$resultAll = mysql_query ("SELECT datePro, quantity FROM power WHERE username = '$username' ") or die(mysql_error()) ;

$row = mysql_fetch_row($result);

echo $row[0]; // datePro
echo $row[1]; // quantity

you can select both from power at the sametime

Upvotes: 1

Related Questions