PHP dev - W-t-f mates?

Status
Not open for further replies.

krazyjosh5

theres GOLD in dem tubes!
May 28, 2007
1,010
19
0
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:
Code:
Fucked up entry: 


Fucked up entry: 


Fucked up entry: 


Fucked up entry: 
Resource id #5

Fucked up entry: 
Resource id #6

Fucked up entry:
Heres my code
PHP:
<?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)){

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

        // Echo out entry
        echo "Fucked 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.

For bonus points, can anyone recommend to me (hopefully Jon doesnt mind this) a forum where I can get more immediate PHP help? Im new to PHP coding and dont know the 'happenin spots' for php on the web. WickedFire is great and all but definitely not THE place for PHP ;) Feel free to PM me those.
 


in $query2, you have $array[zip] it should be $array['zip'] or $array["zip"] but you'll have to use concatenation.

Try
Code:
[COLOR=#000000][COLOR=#0000bb]$query2 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]mysql_query[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"SELECT zip FROM shops WHERE zip = '".$array['zip']."'"[/COLOR][COLOR=#007700])[/COLOR][/COLOR];

Don't forget your semicolon ; at the end each time you set a variable.

Check out pixel2life.com forums and codingforums.com as well.
 
Bandit, now I dont get anything. I just get a bunch of lines of "Fucked up entry:"

Im using EasyPHP if that applies.
 
You're getting that "Resource id #5" stuff because you're trying to echo out the result of mysql_query (which is a resource).

Try this
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)){

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

        // Echo out entry
        echo "Fucked up entry: <br>";
        echo "<pre>"; // For formatting of the next line
        print_r(mysql_fetch_assoc($query2));
        echo "</pre><br><br>";

    }
    else{
        // do nothing
        echo "<p>Entry Okay</p>"; // Better than displaying nothing
    }
}

?>
 
This could all be combined into a query...

Code:
$sql = "SELECT `zip`.`zip` FROM `shops` WHERE LENGTH(`zip`.`zip`) <> 5;
$rs = mysql_query($sql);

while($row = mysql_fetch_assoc($rs)){ print_r($row); }

That should work.
 
Maxor,
one problem with the code you provided (the script) is:

Code:
Warning:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Program Files\EasyPHP 2.0b1\www\golf\test2.php on line 20
I got a similar error with that crafty SQL statement, too.
 
Maxtor, I couldnt get the script or the sql statement to work. The error message above was for the script.
 
you're getting that error cause maxor's sql statement is incorrect. he's pulling the zip from the zip table using zip.zip and the zip table isn't being included in the select, only the shops table is. So take your original sql statement and then take maxor's code and swap his sql statement with yours. And you should have what you're lookin for.

$sql = "SELECT * FROM shops WHERE LENGTH(zip) = 5;
$sql_out = mysql_query($sql);
while($row = mysql_fetch_assoc($sql_out)){
echo $row['zip']."<br>";
}

that should do the trick. I didn't check the sql as I don't have the table. Hope that helps.
 
Status
Not open for further replies.