Jump to content

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

Link to comment
https://forums.phpfreaks.com/topic/12643-syntax-error/
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]
Link to comment
https://forums.phpfreaks.com/topic/12643-syntax-error/#findComment-48504
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]
Link to comment
https://forums.phpfreaks.com/topic/12643-syntax-error/#findComment-48545
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.
Link to comment
https://forums.phpfreaks.com/topic/12643-syntax-error/#findComment-48574
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.