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 fr开发者_开发技巧om $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;
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'];
$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
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.
精彩评论