Jump to content


Photo

Syntax error...


  • Please log in to reply
3 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 22 June 2006 - 03:39 PM

I can't figure out how to query against an array...

(MySQL/PHP)

this gives no result (I know the pn's are good)

$list=array("900.026","900.859","900.809","900.669","900.667");

        $result = mysql_query("SELECT * FROM crosslist WHERE TPN in ('$list')")or die(mysql_error());
        while ($row=mysql_fetch_array($result))
            {
            echo $row['TPN']." ".STR_PAD($row['TPN'],8,"0",STR_PAD_LEFT)."<br/>";;
            }

Table structure if you want it...

# Table "crosslist" DDL

CREATE TABLE `crosslist` (
`CUST` varchar(8) NOT NULL default '',
`CPN` varchar(16) NOT NULL default '',
`CREV` varchar(9) NOT NULL default '',
`TPN` varchar(15) NOT NULL default '',
KEY `second` (`TPN`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='InnoDB free: 219136 kB; InnoDB free: 206848 kB; InnoDB free:'



#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 22 June 2006 - 04:11 PM

<?php
$list=array("900.026","900.859","900.809","900.669","900.667");

$list = '"'.implode('","',$list).'"';
$result = mysql_query("SELECT * FROM crosslist WHERE TPN in ($list)") or die(mysql_error());
while ($row=mysql_fetch_array($result))
{
    echo $row['TPN']." ".STR_PAD($row['TPN'],8,"0",STR_PAD_LEFT)."<br/>";;
}
?>


#3 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 22 June 2006 - 05:55 PM

So, without the implode, I was seeing the array, not the contents of the array?


[!--quoteo(post=386874:date=Jun 22 2006, 12:11 PM:name=shoz)--][div class=\'quotetop\']QUOTE(shoz @ Jun 22 2006, 12:11 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<?php
$list=array("900.026","900.859","900.809","900.669","900.667");

$list = '"'.implode('","',$list).'"';
$result = mysql_query("SELECT * FROM crosslist WHERE TPN in ($list)") or die(mysql_error());
while ($row=mysql_fetch_array($result))
{
    echo $row['TPN']." ".STR_PAD($row['TPN'],8,"0",STR_PAD_LEFT)."<br/>";;
}
?>
[/quote]


#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 22 June 2006 - 08:00 PM

So, without the implode, I was seeing the array, not the contents of the array?

The array is evaluated inside of the string to become the string "Array" because PHP converts it to that.
[a href=\"http://www.php.net/manual/en/language.types.string.php#language.types.string.casting\" target=\"_blank\"]String Conversion[/a]

Implode() is used to put the contents of the array in a string that you can use in the query.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users