We help IT Professionals succeed at work.

What am I doing wrong with this query?

Bruce Gust
Bruce Gust asked
on
Here's my code:

$q2 = "select distinct(ENODEB) as enb , carrier from awsoffload where MARKET = '" . $mkt . "' and CARRIER <> '1' ";
//echo $q2  . "\n";
$select_query_3=$con->query($q2) ;
	if(!$select_query_3)
	{
		echo mysqli_error($con);
	}


$row2 = mysqli_fetch_assoc($select_query_3);

Open in new window


When I echo out the query itself and run it in phpMyAdmin, I get data. So there is information being retrieved. Still, I get this error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /var/www/html/sasite/weekly_reports/procedures/aws_offload_proc2.php on line 107

Line 107 is this:  while ($row2 = mysqli_fetch_assoc($r2));

What is flawed in my code that's resulting in this error?

Thanks!
Comment
Watch Question

Brian TaoOwner
Top Expert 2014

Commented:
Why are you pasting part of your code without line 107 where there's error? If you post the relevant part it will be easier for us to help. (The error says $r2 is null, but there's no $r2 in the partial code you pasted)
Bruce GustPHP Developer

Author

Commented:
There's over 200 lines of code on this particular script I'm working with. It's not mine and the thing that's a little "different" is the fact that it has the query towards the top of the page, but then doesn't call the row until a hundred lines later. That being the case, I figured it was considerate to not post the whole thing.

But if that helps, here's the whole script:

<?php //ob_start();//Start buffer output 
date_default_timezone_set('America/Chicago');
ini_set('max_execution_time', 1200);
require 'common.php';

//echo 'DATABASE PROCC ' . date('h:i:s') . "\n" ;

$con->query('delete from awsoffload2');
  
$q1 = "select distinct(MARKET) from awsoffload where carrier = '1' order by MARKET asc ";
//echo $q1  . "\n";
$select_query=$con->query($q1) ;
	if(!$select_query)
	{
		echo mysqli_error($con);
	}

$row1 = mysqli_fetch_assoc($select_query);

$lte1 = 0 ;
$aws1 = 0 ;

do {

$mkt = $row1['MARKET'];

//echo $mkt . '\n';
$q9 = "select market_desc, region from awsoffload where MARKET = '". $mkt ."'"   ;
//echo $q9  . "\n";
$select_query_1=$con->query($q9) ;
	if(!$select_query_1)
	{
		echo mysqli_error($con);
	}

$row9 = mysqli_fetch_assoc($select_query_1);
$region = $row9['region'];
$market_name = $row9['market_desc'];

$q7 = "insert into awsoffload2 (region, market, market_name) values('$region', '$mkt', '$market_name')"  ;
$select_query_2=$con->query($q7) ;
	if(!$select_query_2)
	{
		echo mysqli_error($con);
	}



$q2 = "select distinct(ENODEB) as enb , carrier from awsoffload where MARKET = '" . $mkt . "' and CARRIER <> '1' ";
//echo $q2  . "\n";
$select_query_3=$con->query($q2) ;
	if(!$select_query_3)
	{
		echo mysqli_error($con);
	}


$row2 = mysqli_fetch_assoc($select_query_3);

do {
	
$aws_enb = $row2['enb'];
$carrier = $row2['carrier'];
//echo $enb . '\n';

//echo $aws_enb . "\n";

if ($carrier == '2' || $carrier == '3') {
$q3 = "select sum(DL_VOL) as aws_dl from awsoffload where ENODEB = '". $aws_enb. "'" ;

$select_query_4=$con->query($q3) ;
	if(!$select_query_4)
	{
		echo mysqli_error($con);
	}


$row3 = mysqli_fetch_assoc($select_query_4);

$aws1 = $aws1 + $row3['aws_dl'];

$lte_enb = $aws_enb - 300000 ;
}
//if ($carrier == '2' || $carrier == '3') {$lte_enb = $aws_enb - 300000 ;}
//if ($carrier == '4') {$lte_enb = 0 ;} //$aws_enb - 600000 ;}


//echo $carrier . "\n";
//echo $lte_enb . "\n";

$q4 = "select sum(DL_VOL) as lte_dl from awsoffload where ENODEB = '". $lte_enb."'"  ;

$select_query_5=$con->query($q4) ;
	if(!$select_query_5)
	{
		echo mysqli_error($con);
	}

$row4 = mysqli_fetch_assoc($select_query_5);

$lte1 = $lte1 + $row4['lte_dl'];

//echo $lte1 . "\n";


} while ($row2 = mysqli_fetch_assoc($r2));

$q5 = "select count(enodeb) as aws_cnt from awsoffload where MARKET = '" . $mkt . "' and left(enodeb,1) <> 1 and eutrancell = 1";

//echo $q2  . "\n";
$r5 =$con->query($q5) ;
$row5 = mysqli_fetch_assoc($r5);
$aws_cnt = $row5['aws_cnt'];

$q10 = "select count(enodeb) as lte_cnt from awsoffload where MARKET = '" . $mkt . "' and left(enodeb,1) = 1 and eutrancell = 1";

//echo $q2  . "\n";
$r10 =$con->query($q10) ;
$row10 = mysqli_fetch_assoc($r10);
$lte_cnt = $row10['lte_cnt'];

$q6 = "select sum(dl_vol) as tot_vol from awsoffload where MARKET = '" . $mkt . "' ";

//echo $q2  . "\n";
$r6 =$con->query($q6) ;
$row6 = mysqli_fetch_assoc($r6);
$tot_vol = $row6['tot_vol'];

$tot_vol_adj = ($tot_vol/1024) ; // divide by 1024 to convert Megabyte to GigaByte

$lte_adj  = ($lte1 / 1024) ; // divide by 1024 to convert Megabyte to GigaByte
$aws_adj  = ($aws1 / 1024) ; // divide by 1024 to convert Megabyte to GigaByte

$cell_aws_offload = ($aws_adj / ($lte_adj + $aws_adj))* 100 ;
$market_aws_offload = ($aws_adj / $tot_vol_adj)* 100 ;

$q11 = "select (sum(B4_cnt)/sum(lte_cnt))*100 as aws_ue_percent from ue_cnt where MARKET_ID = '" . $mkt . "'";

//echo $q2  . "\n";
$r11 =$con->query($q11) ;
$row11 = mysqli_fetch_assoc($r11);
$aws_ue_percent = $row11['aws_ue_percent'];

$aws_percent = ($aws_cnt/($lte_cnt + $aws_cnt))*100 ;
$aws_ue_aws_sites = ($aws_percent * $aws_ue_percent)/100 ;



$q8 = "update awsoffload2 set aws_dl_vol = '$aws_adj', lte_dl_vol = '$lte_adj' , tot_vol = '$tot_vol_adj', aws_cnt = '$aws_cnt' , lte_cnt = '$lte_cnt',  cell_aws_offload = '$cell_aws_offload', market_aws_offload = '$market_aws_offload', aws_site_percent ='$aws_percent', realized_aws_ue ='$aws_ue_percent', aws_ue_aws_sites ='$aws_ue_aws_sites' where MARKET = '$mkt'"  ;
//echo $q8  . "\n";
echo 'COMPLETED MARKET ' . $mkt . "\n";
$update_query =$con->query($q8) ;
if(!$update_query)
	{
		echo mysqli_error($con);
	}

$lte1 = 0 ;
$aws1 = 0 ;
$lte_adj  = 0 ;
$aws_adj  = 0 ;
$cell_aws_offload = 0 ;
$market_aws_offload = 0 ;


} while ($row1 = mysqli_fetch_assoc($select_query));

//$q15 = "update regional_update set aws_offload = '0', aws_offload_per_site = '0', aws_site = '0', aws_ue = '0'";

$q15 = "update regional_update set aws_offload = '0', aws_offload_per_site = '0', aws_ue = '0'";
//echo $q14 . "\n";
mysqli_query($mysqli, $q15);  
  
$q12 = "select distinct(region) from awsoffload2"; 
//echo $q2  . "\n";
$r12 =$con->query($q12) ;
$row12 = mysqli_fetch_assoc($r12);


do {
$reg = 	$row12['region'];
//echo $reg .'\n';

$q16 = "select sum(dl_vol) as tot_vol from awsoffload where region = '" . $reg . "'"; 
//echo $q13  . "\n";
$r16 =$con->query($q16) ;
$row16 = mysqli_fetch_assoc($r16);
$tot_vol = ($row16['tot_vol']/1024); // divide by 1024 to convert Megabyte to GigaByte


$q13 = "select sum(aws_dl_vol) as aws_offload, (sum(aws_dl_vol)/(sum(lte_dl_vol)+sum(aws_dl_vol)))*100 as aws_offload_per_site, sum(aws_cnt)/(sum(aws_cnt)+sum(lte_cnt))*100 as aws_site, avg(realized_aws_ue) as aws_ue from awsoffload2 where region = '" . $reg . "'"; 
//echo $q13  . "\n";
$r13 =$con->query($q13) ;
$row13 = mysqli_fetch_assoc($r13);

$aws_offload = ($row13['aws_offload']/$tot_vol)*100;
$aws_off_per_site = $row13['aws_offload_per_site'];
$aws_site = $row13['aws_site'] ;

$q20 = "select sum(b4_cnt)/sum(lte_cnt)*100 as aws_ue from ue_reg where region = '" . $reg . "'"; 
//echo $q13  . "\n";
$r20 =$con->query($q20) ;
$row20 = mysqli_fetch_assoc($r20);
$aws_ue = $row20['aws_ue'] ;


//$q14 = "update regional_update set aws_offload = '$aws_offload', aws_offload_per_site = '$aws_off_per_site', aws_site = '$aws_site', aws_ue = '$aws_ue' where reg_name = '" . $reg . "'";

$q14 = "update regional_update set aws_offload = '$aws_offload', aws_offload_per_site = '$aws_off_per_site', aws_ue = '$aws_ue' where reg_name = '" . $reg . "'";

//echo $q14 . "\n";
$rock_on =$con->query($q14) ;

} while ($row12 = mysqli_fetch_assoc($r12));

$q17 = "select sum(dl_vol) as totvol from awsoffload"; 
//echo $q13  . "\n";
$r17 =$con->query($q17) ;
$row17 = mysqli_fetch_assoc($r17);
$area_tot_vol = ($row17['totvol']/1024); // divide by 1024 to convert Megabyte to GigaByte

$q18 = "select sum(aws_dl_vol) as area_aws_offload, (sum(aws_dl_vol)/(sum(aws_dl_vol)+sum(lte_dl_vol)))*100 as area_aws_offload_per_site, sum(aws_cnt)/(sum(aws_cnt)+sum(lte_cnt))*100 as area_aws_site  from awsoffload2"; 
//echo $q13  . "\n";
$r18 =$con->query($q18) ;
$row18 = mysqli_fetch_assoc($r18);

$area_aws_offload = ($row18['area_aws_offload']/$area_tot_vol)*100;
$area_aws_off_per_site = $row18['area_aws_offload_per_site'];
$area_aws_site = $row18['area_aws_site'] ;
//$area_aws_ue = $row18['area_aws_ue'] ;


$q19 = "update regional_update set aws_offload = '$area_aws_offload', aws_offload_per_site = '$area_aws_off_per_site'  where reg_name = 'AREA'";

//$q19 = "update regional_update set aws_offload = '$area_aws_offload', aws_offload_per_site = '$area_aws_off_per_site', aws_site = '$area_aws_site' where reg_name = 'AREA'";

$bring_it =$con->query($q19) ;

$q21 = "select (sum(B4_CNT)/sum(LTE_CNT))*100 as area_aws_ue from ue_reg"; 
//echo $q13  . "\n";
$r21 =$con->query($q21) ;
$row21 = mysqli_fetch_assoc($r21);
$area_aws_ue = $row21['area_aws_ue'] ;

$q22 = "update regional_update set aws_ue = '$area_aws_ue' where reg_name = 'AREA'";
$dig_it =$con->query($q22) ;


$q23 = "select * from aws_sites"; 
//echo $q13  . "\n";
$r23 =$con->query($q23) ;
$row23 = mysqli_fetch_assoc($r23);

do {
$reg = $row23['Region'] ;
$aws_sites = $row23['aws_sites']*100 ;

$vivian_6="update regional_update set aws_site = '$aws_sites' where reg_name = '".$reg."'";
$sweetness =$con->query($vivian_6) ;

} while ($row23 = mysqli_fetch_assoc($r23));



mysqli_close($mysqli);
?>

Open in new window


As an aside, this is a working code. The difference is that it's currently functioning on a server that respects mysql as opposed to mysqli. So I'm thinking it might be in the context of my attempting to update the code that I've done something squirrel-ly.
Owner
Top Expert 2014
Commented:
With a first look, please change the line
while ($row2 = mysqli_fetch_assoc($r2));

Open in new window

to
while ($row2 = mysqli_fetch_assoc($select_query_3));

Open in new window


As there's no $r2 defined anywhere in your code.
Bruce GustPHP Developer

Author

Commented:
Brian, that did it! Sometimes just another set of eyes to help you see obvious mistakes.

Thanks!
Brian TaoOwner
Top Expert 2014

Commented:
Glad that helps.  Thanks for the points.