[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 739
  • Last Modified:

PHP with MySQL problem with search results array

This is a search results page which should display the results in rows, but I'm getting an error at line 138 "$row = $result->fetch_assoc();" -  Fatal error: Call to a member function fetch_assoc() on a non-object.  I'm pretty clueless as to how it can be fixed since I'm a PHP novice.

<?php require_once('../../Connections/connCoreWH_b.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Results</title>

<style type="text/css">
<!--



.hilite {	
	background-color: #E8F2F8;	
	}

.hilite2 {
	background-color: #FFFFFF;
    }

body { 
  background-color: #CCCC99;
  }

th.well {   
   border:  2px solid #336699;   
   padding: 5px;
   background-color:#CCCCCC;
   font-weight:bold; 
   color: #336699;  
  }
  
td.welldata {
   
   border:  2px solid #336699;   
   padding: 5px;   

}
-->
</style>

</head>

<body>

<h1>Search Results</h1>

<?php
// create short variable names
$county = $_REQUEST['County'];
$operator = $_REQUEST['Operators'];
$township = $_REQUEST['Township'];
$range = $_REQUEST['Range'];
$section = $_REQUEST['Section'];
$api = $_REQUEST['API'];
$permit = $_REQUEST['Permit'];


if (!get_magic_quotes_gpc()){
$county = addslashes($county);
$operator = addslashes($operator);
$township = addslashes($township);
$range = addslashes($range);
$section = addslashes($section);
$api = addslashes($api);
$permit = addslashes($permit);
}

//@ $db = new mysqli('localhost', 'root', '', 'agsdb');
mysql_select_db($database_connCoreWH, $connCoreWH);
//if (mysqli_connect_errno()) {
//echo 'Error: Could not connect to database.  Please try again later.';
//exit;
//}

$query = "SELECT WELL_NAME, WELL_NUM, AGS_ID, OPERATOR_NAME, RES_FIELDNAME, API, PERMIT_NUM, CountyName,  TWN, RNG, SECTION, well_locationid

FROM corewh_wells

WHERE 
CORE_SAMPLE = 1 
AND (CountyName = '".$county."' OR '".$county."' = '-1') 
AND (OPERATOR_NAME = '".$operator."' OR '".$operator."' = '-1')
AND (TWN = '".$township."' OR '".$township."' = '-1')
AND (RNG = '".$range."' OR '".$range."' = '-1')
AND (SECTION = '".$section."' OR '".$section."' = '-1')
AND (API = '".$api."' OR '".$api."' = '-1')
AND (PERMIT_NUM = '".$permit."' OR '".$permit."' = '-1')
ORDER BY AGS_ID ASC";
//$result = $db->query($query);
$result = mysql_query($query);

//$num_results = $result->num_rows;
$num_results = mysql_num_rows($result);

echo "<p style='font-size: 20px;'><strong>".$num_results."</strong> wells found where:</p>";
if ($county == -1) {
echo "<span>County = <br />";
} else {
echo "<span>County = <strong>".$county."</strong><br />";
}
if ($township == -1) {
echo "Township = <br />";
} else {
echo "Township = <strong>".$township."</strong><br />";
}
if ($range == -1) {
echo "Range = <br />";
} else {
echo "Range = <strong>".$range."</strong><br />";
}
if ($section == -1) {
echo "Section = <br />";
} else {
echo "Section = <strong>".$section."</strong><br />";
}
if ($operator == -1) {
echo "Operator = <br />";
} else {
echo "Operator = <strong>".$operator."</strong><br />";
}
if ($api == -1) {
echo "API = <br />";
} else {
echo "API = ".$api."<br />";
}
if ($permit == -1) {
echo "Permit = </span><br />";
} else {
echo "Permit = <strong>".$permit."</strong></span><br />";
}

echo "<table style='border-collapse: collapse;'><thead><th width='25' class='well'>#</th><th width='100' class='well'>AGS ID</th><th width='125' class='well'>API</th><th width='50' class='well'>Permit</th><th width='200' class='well'>Operator</th><th width='200' class='well'>Lease Name</th><th width='75' class='well'>Number</th><th width='150' class='well'>County</th><th width='50'class='well'>Township</th><th width='50' class='well'>Range</th><th width='50' class='well'>Section</th></thead>";
echo "<tbody>";

for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();

if($i % 2) {

echo "<tr class='hilite'>";
echo "<td class='welldata'>".($i+1)."</td>";
echo "<td class='welldata'>";
echo "<a href='core_details_b.php?recordID=";
echo stripslashes($row['well_locationid']);
echo "'>";
echo stripslashes($row['AGS_ID']);
echo "</a>";
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['API']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['PERMIT_NUM']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['OPERATOR_NAME']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['WELL_NAME']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['WELL_NUM']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['CountyName']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['TWN']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['RNG']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['SECTION']);
echo "</td>";
echo "</tr>";

} else {

echo "<tr class='hilite2'>";
echo "<td class='welldata'>".($i+1)."</td>";
echo "<td class='welldata'>";
echo "<a href='core_details_b.php?recordID=";
echo stripslashes($row['well_locationid']);
echo "'>";
echo stripslashes($row['AGS_ID']);
echo "</a>";
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['API']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['PERMIT_NUM']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['OPERATOR_NAME']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['WELL_NAME']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['WELL_NUM']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['CountyName']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['TWN']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['RNG']);
echo "</td>";
echo "<td class='welldata'>";
echo stripslashes($row['SECTION']);
echo "</td>";
echo "</tr>";
}
}
$result->free();
echo "</tbody></table>";
$db->close();

?>

</body>
</html>

Open in new window

0
Nate_LR
Asked:
Nate_LR
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
You're mixing mysql methods and that does not work.  There are the older 'mysql' functions and the newer 'mysqli' functions.  Since you seem to be switching to the older functions, line 138 should probably use mysql_fetch_array http://us2.php.net/manual/en/function.mysql-fetch-array.php .

$row = mysql_fetch_array($result, MYSQL_ASSOC);

Open in new window

0
 
Nate_LRAuthor Commented:
Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now