(simple?) Problem out putting MySQL query results

Discussion in 'PHP' started by jamus, Aug 4, 2006.

  1. #1
    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?
     
    jamus, Aug 4, 2006 IP
  2. ahkip

    ahkip Prominent Member

    Messages:
    9,205
    Likes Received:
    647
    Best Answers:
    0
    Trophy Points:
    310
    #2
    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)) {
     
    ahkip, Aug 4, 2006 IP
    dct likes this.
  3. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you dude! Caouldn't see the wood for the trees!
    Your a star!
     
    jamus, Aug 4, 2006 IP
  4. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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 :(
     
    jamus, Aug 9, 2006 IP
  5. ahkip

    ahkip Prominent Member

    Messages:
    9,205
    Likes Received:
    647
    Best Answers:
    0
    Trophy Points:
    310
    #5
    TRY

    $count=mysql_num_rows($result) ;
    if($count==0){echo "none1";}
     
    ahkip, Aug 9, 2006 IP
  6. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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\">&nbsp;</td>";
    	echo "</tr>";
    }
    
    echo "</table>";
    
    	}
    $count=mysql_num_rows($result) ;
    if($count==0){echo "none1";}
    
    
    PHP:
     
    jamus, Aug 10, 2006 IP
  7. jamus

    jamus Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks dude. being thick again. i worked it out.

    Thanks again for your help!
     
    jamus, Aug 10, 2006 IP