Jump to content

[SOLVED] php mysql query gone wrong


ballhogjoni

Recommended Posts

hey everyone

I have this script that is supposed to pull the number of rows based on a date range. The problem I am having is that it only pulls one row, when I know that there are multiple rows.

 

My code:

<?php
$date2 = date("Y-m-d",mktime(0, 0, 0, $m1, $d1, $y1));	
$date1 = date("Y-m-d",mktime(0, 0, 0, $m2, $d2, $y2));
include "action.php";//this script includes the functions below
echo "<br>Sales: ".sale(strtotime($date2),strtotime($date1));
echo "<br>Leads: ".lead(strtotime($date2),strtotime($date1));
?>

 

<?php
function sale($d2,$d1) {
$result = mysql_query("SELECT id FROM offers WHERE mediaCompany = '".strtolower($_SESSION['myusername'])."' AND typeOfLead = 'sale' AND orderDate BETWEEN '".$d2."' AND '".$d1."'") or die(mysql_error());
$count = mysql_num_rows($result);
return $count;
}
function lead($d2,$d1) {
$result = mysql_query("SELECT id FROM offers WHERE mediaCompany = '".strtolower($_SESSION['myusername'])."' AND typeOfLead = 'lead' AND date BETWEEN '".$d2."' AND '".$d1."'") or die(mysql_error());
$count = mysql_num_rows($result);
return $count;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/109621-solved-php-mysql-query-gone-wrong/
Share on other sites

<?php
function sale($d2,$d1) {
$result = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM offers WHERE mediaCompany = '".strtolower($_SESSION['myusername'])."' AND typeOfLead = 'sale' AND orderDate BETWEEN '".$d2."' AND '".$d1."'") or die(mysql_error());
$count = mysql_result($result, 0);
return $count;
}
function lead($d2,$d1) {
$result = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM offers WHERE mediaCompany = '".strtolower($_SESSION['myusername'])."' AND typeOfLead = 'lead' AND date BETWEEN '".$d2."' AND '".$d1."'") or die(mysql_error());
$count = mysql_result($result, 0);
return $count;
}
?>

 

 

Try that, I didn't test it so i might have added or forgotten a ( or ), post back with the feedback

I figured it out.

 

I was taking to many steps. I had this in my code date("Y-m-d",mktime(0, 0, 0, $m2, $d2, $y2));. The date function didn't need to be there becasue I am saving the date as the unix timestamp using time(). So basically the query was reading the wrong date and therefore not giving me the result i wanted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.