Jump to content

Archived

This topic is now archived and is closed to further replies.

AV1611

Syntax error...

Recommended Posts

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

(MySQL/PHP)

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

[code]
$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/>";;
            }
[/code]

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:'

Share this post


Link to post
Share on other sites
[code]
<?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/>";;
}
?>
[/code]

Share this post


Link to post
Share on other sites
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) [snapback]386874[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]
<?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/>";;
}
?>
[/code]
[/quote]

Share this post


Link to post
Share on other sites
[quote]
So, without the implode, I was seeing the array, not the contents of the array?
[/quote]
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.