Jump to content


Simple script needs some LOVIN'


4 replies to this topic

#1 krazyjosh5

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 03 July 2007 - 07:43 PM

Hi. First post.

Im getting weird output with this one. Basically Im having it scavenge my database zip code field for any fields longer than 5 (the length of a zip code). A few of my zip fields have full addreses in them due to a crappy script that went through it to 'fix' them.

Now, I get this as output:

Messed up entry:


Messed up entry:


Messed up entry:


Messed up entry:
Resource id #5

Messed up entry:
Resource id #6

Messed up entry:

Heres my code:
 <?php
include 'include.php';

$query = mysql_query("SELECT zip FROM shops");

while($array=mysql_fetch_array($query)){
	
	// How big is it? Heh.
	$length = strlen($array["zip"]);

	// If its NOT length 5, then warn
	if(!($length == 5)){

		// Query the entry
		$query2 = mysql_query("SELECT zip FROM shops WHERE zip = '$array[zip]'");

		// Echo out entry
		echo "Messed up entry: <br>";
		echo $query2;
		echo "<br><br>";

	}
	else{
		// do nothing
	}
}

?>

For the record, my zip field is text. It (should) contains zip codes. This script is to find the non-conformists. Also, Im using the newest version of EasyPHP. :bs:

Edited by rc69, 03 July 2007 - 11:56 PM.


#2 Ultimatum

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 03 July 2007 - 09:45 PM

Well you are checking or $length not equal is to 5 inside a while, the while loops all records 1 by 1 so $length is always 1. Try to echo $length.

And if you don't need an else, you can just leave it out.

#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 July 2007 - 11:56 PM

To start, you should refrain from swearing here. Not everybody that reads these forums is an adult.

Now, i have no idea what ultimatum said, but here's what i have to say. There are a couple basic problems, most of which revolves around the logic you are trying to use.

You should first start by reasearching the mysql_query function. When attempting to debug php, it helps to know the return type of functions so you can properly handle them (i.e. arrays, objects, resources). That is why i always use print_r() :)

In short, your output is just fine. It's letting us know you have something working.


Next up, the problems with your logic.
include 'include.php';

$query = mysql_query("SELECT zip FROM shops");

while($array=mysql_fetch_array($query)){
	
	// How big is it? Heh.
	$length = strlen($array["zip"]);

	// If its NOT length 5, then warn
	if($length != 5){ //1
//2
		// Echo out entry
		echo "Messed up entry: <br>";
		echo '<pre>'.print_r($array,1).'</pre>';
		echo "<br>";
	}
//3
}
In regards to the "footnotes" i added into the comments above.

1. Sorry if i'm going on about logic, i'm just a fan of it. You are negating an equality here. PHP has an operator that natively does that. It would be more efficient in several ways to just use that.

2. You've already selected the entry in the first select statement. If the if-statement returns true, then $array already has the data you need and there is no need to select it again.

3. Adding an else onto the end of an if-statement is not a requirement. If you don't plan on doing something with it, it's just wasting space (and possibly time... not sure on that one though).

#4 icio

    Young Padawan

  • Members
  • Pip
  • 10 posts
  • Location:Perth, Scotland

Posted 06 July 2007 - 01:30 AM

I think what you're best to do is the following:

<?
$query = mysql_query("SELECT * FROM `shops` WHERE CHAR_LENGTH(`zip`)!=5;");
while ($shop = mysql_fetch_assoc($query)) {
	// => Zip not five characters in `$shop`
	// Do what you want from here on
	print_r($shop);
}

Edited by icio, 06 July 2007 - 01:31 AM.


#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 06 July 2007 - 03:43 PM

This is where we run into a problem. In my experience i've never heard of sql functions performing well. That may work all fine and well, and for a begining programmer with a small, low-traffic site, that could be fine. But when you get into the higher levels of programming, it is often times more efficient to use php's functions rather than the ones that mysql provide.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users