I knew it was a long shot to begin with...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GradyLorenzo
Forum Newbie
Posts: 6
Joined: Wed Mar 09, 2011 4:40 pm

I knew it was a long shot to begin with...

Post by GradyLorenzo »

I'm going to go ahead and say I've been doing PHP for only about a week now. But here goes:

Code: Select all

$i = 1;
$resultLimit = 6;
$query1 = mysql_query("SELECT *, COUNT('zipcode') FROM `maildrop` WHERE `zipcode` LIKE '".$_POST['zipcode']."'") or die(mysql_error());
while($i < $resultLimit){
$i++;
if($query1){
	$result1 = mysql_fetch_array($query1);
	$result2 = mysql_fetch_array($query1);
	$result3 = mysql_fetch_array($query1);
	$countResults = mysql_num_rows($query1);
}

            
			if($result1){
						echo ("<strong>"."There are " .$countResults. " result(s)"."</strong>");
						echo ("<p>");
						echo ($result1['name'] . "<br>");
						echo ($result1['address'] . "<br>");
						echo ($result1['zipcode'] . "<br>");
						echo ($result1['phonenumber'] . "<br>");
						echo ("<p>");
            }
			if($result2){
						echo ("<p>");
						echo ($result2['name'] . "<br>");
						echo ($result2['address'] . "<br>");
						echo ($result2['zipcode'] . "<br>");
						echo ($result2['phonenumber'] . "<br>");
						echo ("<p>");
			}
			if($result3){
						echo ("<p>");
						echo ($result3['name'] . "<br>");
						echo ($result3['address'] . "<br>");
						echo ($result3['zipcode'] . "<br>");
						echo ($result3['phonenumber'] . "<br>");
						echo ("<p>");
			}
			else{
				 echo ("<b>" . $_POST['zipcode'] . "</b>" . " - This record does not exist");
				 echo ("<br>");
            }
}
Basically the idea is to show multiple results (if more than one exists) in a nicely formatted style. But this code shows one healthy result, then 5 "This record does not exist". Sorry if I sound noobish, I'm only 19.

So is there a better way of displaying multiple results?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I knew it was a long shot to begin with...

Post by AbraCadaver »

You need to find a good tutorial on PHP/MySQL, but here is the long and short of it:

Code: Select all

$countResults = mysql_num_rows($query1);

if($countResults > 0) {
    while($result1 = mysql_fetch_array($query1)) {
        echo ("<strong>"."There are " .$countResults. " result(s)"."</strong>");
        echo ("<p>");
        echo ($result1['name'] . "<br>");
        echo ($result1['address'] . "<br>");
        echo ($result1['zipcode'] . "<br>");
        echo ($result1['phonenumber'] . "<br>");
        echo ("<p>");
    }
} else {
    echo ("<b>" . $_POST['zipcode'] . "</b>" . " - This record does not exist");
    echo ("<br>");

}
If you only want a certain number of records then use the LIMIT clause in your query.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
GradyLorenzo
Forum Newbie
Posts: 6
Joined: Wed Mar 09, 2011 4:40 pm

Re: I knew it was a long shot to begin with...

Post by GradyLorenzo »

It works, and it takes care of the "record does not exist." But I have two records with a 28379 zipcode, but it only shows one of them.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I knew it was a long shot to begin with...

Post by AbraCadaver »

GradyLorenzo wrote:It works, and it takes care of the "record does not exist." But I have two records with a 28379 zipcode, but it only shows one of them.
Sorry, I didn't look at your query. When you use an aggregate function like COUNT() or SUM() the way you have it will only return one row. Try this:

Code: Select all

$query1 = mysql_query("SELECT * FROM `maildrop` WHERE `zipcode` = '" . mysql_real_escape_string($_POST['zipcode']) . "'") or die(mysql_error());
Also, be sure to use mysql_real_escape_string() on user submitted data.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
GradyLorenzo
Forum Newbie
Posts: 6
Joined: Wed Mar 09, 2011 4:40 pm

Re: I knew it was a long shot to begin with...

Post by GradyLorenzo »

You, sir, have helped me keep my job
Post Reply