Jump to content

Recommended Posts

I've tried running this same query in phpmyadmin and it turns out great but I'm not as fortunate when I run it in php.

So, I figured maybe it's in my php code because the query works fine.

 

This is the error that it returns.

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\reports.php on line 56

 

//////////////////////////////////////////

//$date1 and $date2 contains the right values, i checked

$dquery="select distinct `vessel` as `vessel` from `bill` where `dateissue` >= ' ".$date1." ' and `dateissue` <= '".$date2." ' ";

$dres=mysql_query($dquery) or die(mysql_error());

while($drow=mysql_fetch_array($dres)){

$vessel=$drow['vessel'];

       

        echo $vessel;

}

Link to comment
https://forums.phpfreaks.com/topic/217903-what-am-i-doing-wrong-in-this-script/
Share on other sites

The code inside your while(){} loop is reusing and overwriting the $dres variable so when the while loop condition is reevaluated after the first pass through the loop, $dres is no longer the result resource from the first query.

yea I actually did not show that I put this inside a <tr><td>

sorry

 

well it's working now without me changing anything,

although it's giving me a message saying that the server

has gone away

 

if any one can help me with the server has gone away

problem it'd be much appreciated.

 

by the way I have more than 511, 000 files in my database and counting

I know this is one of the reasons

also the reason why it takes around 12 minutes for the page to load

 

yes 12

 

Is this something that I could solve by making some changes and modifications

in my http config?

There's a whole section in the mysql documentation addressing the "server has gone away" error.

 

If you have a 12 minute page generation/load time, you need to find out what is causing that and fix it. You would need to post the complete code responsible for producing that problem if you want someone on the forum to help with it.

 

I have more than 511, 000 files in my database

 

^^^ Nothing you have posted so far concerns files in your database, so again, posting your code would be the quickest way of getting help with what your code (and queries) are doing.

 

Just having that many pieces of data in your database does not account for a long page load time unless you are retrieving all that data and looping through it using some slow parsed, tokenized, interpreted php code or you are executing queries inside of loops or you don't have necessary indexes setup in the database tables...

 

 

 

I am using ajax so I have two php files displayed in this page

 

this is my dfsr.php

<form method="get">
<table width="196" border="0">
<tr>
<td width="71" valign="top">From: 
<!-- i would first select the distinct date from the database then pass this value to dfsr_content.php -->
<select name="date1" onchange="htmlData('dfsr_content.php', 'date1='+this.value)" id="search-text" />
<option value="#" selected="selected">start date</option>
<?php
$query="select distinct dateissue as dateissue from bill order by dateissue desc";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$dateissue = $row['dateissue'];
if($dateissue!=""){
echo "<option value=".$dateissue.">".$dateissue."</option>";
}}	
?>
</select>
<td width="41">To: 
<div id="txtResult">
<select name="date2" id="search-text">
<option>end date</option>
</select> </div>
</tr>
<tr>
<td>
</tr>
<tr>
<td>
</tr>
<tr>
<td><input type="submit" id="search-submit" />
</tr>
</table>
</form>

 

while this is my dfsr_content.php

 

<?php
require_once("connection.php");
mysql_select_db($database);
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);	

//date2
$date1 = $_GET['date1'];
if ($_GET['date1'] == $date1) {
   ?>
<head>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script src="ajax_req.js" type="text/javascript"></script>
</head>
<body>
<form method="get">
<select name="date2" id="search-text" onChange="htmlData('#', 'date2='+this.value)">
<?php
//this is the second drop down that populates from the database based on the date selected from the previous page
//the rest of the queries below takes the files where the date is within the set range ($date1 and $date2)
$query="select distinct dateissue as dateissue from bill where dateissue >= '".$date1."' order by dateissue desc  ";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$date2 = $row['dateissue'];
if($date2!=""){
echo "<option value=".$date2." selected=selected>".$date2."</option>";	
}}
?>
<input type="hidden" value="<?php echo $date2;?>" name="<?php echo $date2;?>">
<input type="hidden" value="<?php echo $date1;?>" name="<?php echo $date1;?>">
</select>

</form>
<?php
}
?>
<table width="748">
<tr><td width="58"><strong>
<td width="85">Date: <$date> day
</tr>
<tr>
<strong>
<td align="left">Vessel
<td align="center">Route
<td width="83" align="center">ICR#
<td width="129" align="right">Prepaid
<td width="120" align="right">A/C Origin
<td width="135" align="right">A/C Destination
<td width="106" align="right">Total
</strong>
</tr>
<tr>
<td colspan="7"><h2 class="title"></h2>
</tr>
<?php
//vessel
$dquery="select distinct(`vessel`) as `vessel` from `bill` where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."'  ";
$dres=mysql_query($dquery) or die(mysql_error());
while($drow=mysql_fetch_array($dres)){
$vessel=$drow['vessel'];

$rquery="select loading, unloading from `bill` where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `vessel`='".$vessel."'  ";
$rres=mysql_query($rquery) or die(mysql_error());
$rrow=mysql_fetch_array($rres);
$loading=$rrow['loading'];
$unloading=$rrow['unloading'];
?>
<tr>
<td align="left"><?php echo $vessel;?>
<td align="center"><?php echo $loading." - ".$unloading;?>
<td align="center"><?php echo "<u>                     </u>";?>
<td align="right">
<?php
$prepaid=0;
$pquery="select sum(`gfreight`) as `ptotal` from `bill` where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `payment` = 'P' and `vessel` = '".$vessel."'  ";
$presult=mysql_query($pquery);
$prow=mysql_fetch_array($presult);
	$ptotal=$prow['ptotal'];
	$ptotal=number_format($prow['ptotal'],2);
	echo "P ".$ptotal;
?>
<td align="right">
<?php
$squery="select sum(`gfreight`) as `stotal` from `bill` where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `payment` = 'S' and `vessel` = '".$vessel."'  ";
$sresult=mysql_query($squery);
$srow=mysql_fetch_array($sresult);
	$stotal=$srow['stotal'];
	$stotal=number_format($srow['stotal'],2);
	echo "P ".$stotal;
?>
<td align="right">
<?php
$dquery="select sum(`gfreight`) as `dtotal` from `bill` where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `payment` = 'D' and `vessel` ='".$vessel."'  ";
$dresult=mysql_query($dquery);
$drow=mysql_fetch_array($dresult);
	$dtotal=$drow['dtotal'];
	$dtotal=number_format($drow['dtotal'],2);
	echo "P ".$dtotal;
?>
<td align="right">
<?php
$tquery="select sum(`gfreight`) as `total` from `bill` where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `vessel` = '".$vessel."'  ";
$tresult=mysql_query($tquery);
$trow=mysql_fetch_array($tresult);
	$total=$trow['total'];
	$total=number_format($trow['total'],2);
	echo "P ".$total;
?>
</tr>
<?php
}
?>
<tr>
<td colspan="2"> 
<td>Sub-Total
<?php
//sub-total prepaid
$spquery="select sum(gfreight) as sptotal from bill where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `payment` = 'P'  ";
$spres=mysql_query($spquery);
$sprow=mysql_fetch_array($spres);
$sprepaid=number_format($sprow['sptotal'],2);

//sub-total origin(S)
$soquery="select sum(gfreight) as sototal from bill where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `payment` = 'S'  ";
$sores=mysql_query($soquery);
$sorow=mysql_fetch_array($sores);
$sorigin=number_format($sorow['sototal'],2);

//sub-total destination
$sdquery="select sum(gfreight) as sdtotal from bill where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' and `payment` = 'D' limit 0,30";
$sdres=mysql_query($sdquery);
$sdrow=mysql_fetch_array($sdres);
$sdestination=number_format($sdrow['sdtotal'],2);

//total
$tquery="select sum(gfreight) as total from bill where `dateissue` >= '".$date1."' and `dateissue` <= '".$date2."' limit 0,30";
$tres=mysql_query($tquery);
$trow=mysql_fetch_array($tres);
$total=number_format($trow['total'],2);
?>
<td align="right">P <?php echo $sprepaid;?>
<td align="right">P <?php echo $sorigin;?>
<td align="right">P <?php echo $sdestination;?>
<td align="right">P <?php echo $total;?>
</tr>
<tr>
<td colspan="2"> 
<td colspan="3">Daily Total Receivables . . . . . . . . . . . . . . .
<td align="right">P <?php $dailytolrec = $sorigin + $sdestination; echo $dailytolrec;?>
</tr>
<tr>
<td colspan="4"> 
<td>Overall Total
<td colspan="2" align="right">P <?php 
$i=0;
while($i!=25){
	echo " ";
	$i++;
	}
echo "<u>".$total."</u>";?>
</tr>
<tr>
<td>
<td>
<td>
</tr>
</table>
</body>

 

Thank you very much for bearing with me

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.