Hi, I have an example query that was supplied with a post (zip) code database. It is supposed to calculated which suppliers are closest to the entered postcode. After validating the postcode format the function below is called function getDistanceUK($fromPcode) { $_from_postcode = splitpostcode($fromPcode); // parse postcode to match database search criteria $_from_postcode = substr(md5($_from_postcode), 0, 8); // encrypts received value using PHP md5 $_to_postcode = splitpostcode($targetPcode); // parse postcode to match database search criteria $_to_postcode = substr(md5($_to_postcode), 0, 8); // encrypts received value using PHP md5 $db_conn = db_connect(); // connect to database $query = "SELECT sup_name, sup_postcode_md5, (SQRT(POW((b.coord1 - a.coord1), 2) + POW((b.coord2 - a.coord2), 2))/1000) * 0.621 AS distance FROM uk_code a, uk_code b, suppliers WHERE a.code = '".$_from_postcode."' AND b.code = suppliers.sup_postcode_md5 HAVING (distance < 5) ORDER BY distance asc "; if (!$query) { echo '</table>'; exit('<p>Error retrieving data from database!<br />'. 'Error: ' . mysql_error() . '</p>'); } /// MY EFFORTS TO PRINTS THE RESULTS while ($list = mysql_fetch_array($query)) { echo "<tr valign='top'>\n"; $sup_postcode_md5 = $list['sup_postcode_md5']; echo "<td>$sup_postcode_md5</td>\n"; echo "</tr>\n"; } } PHP: However my efforts to display the results show the error "mysql_fetch_array(): supplied argument is not a valid MySQL result resource " Any one see where im going wrong?
dude, you create the query, but didn't run it in mysql. add this after the "$query = SELECT sup_name,"..... $result=mysql_query($query) or die(mysql_error()); remove line 16-20 then replace while ($list = mysql_fetch_array($query)) { with # while ($list = mysql_fetch_array($result)) {
You'll probably get this straight away too! How can i add an "if returns no records 'echo 'this message'"? if (!$result){ echo 'none1'; } seems to print everytime
Thanks, but "mysql_fetch_array(): supplied argument is not a valid MySQL result resource" Am i putting it in the right place? $db_conn = db_connect(); // connect to database $query = "SELECT sup_name, sup_postcode, sup_address, sup_postcode_md5, (SQRT(POW((b.coord1 - a.coord1), 2) + POW((b.coord2 - a.coord2), 2))/1000) * 0.621 AS distance FROM uk_code a, uk_code b, suppliers WHERE a.code = '".$_from_postcode."' AND b.code = suppliers.sup_postcode_md5 HAVING (distance < '".$area."') ORDER BY distance asc "; $result=mysql_query($query) or die(mysql_error()); echo "<table width=\"100%\" align=\"left\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" ><tr>"; while ($list = mysql_fetch_array($result)) { $sup_postcode_md5 = $list['sup_postcode_md5']; $sup_name = $list['sup_name']; $sup_postcode = $list['sup_postcode']; $distance = $list['distance']; $distance = round($distance); $sup_address = $list['sup_address']; echo "<td colspan=\"3\" align=\"left\"><p class=\"suppliersName\">$sup_name</p></td>"; echo "<td align=\"right\"><p class=\"suppliersDistance\">$distance miles away</p></td></tr>"; echo "<tr><td colspan=\"3\" align=\"left\" width=\"130px\"><span class=\"suppliersAddress\">$sup_address $sup_postcode</span></td>"; echo "<td align=\"left\"> </td>"; echo "</tr>"; } echo "</table>"; } $count=mysql_num_rows($result) ; if($count==0){echo "none1";} PHP: