[PHP] Finding array in MySQL (I'm not asking the right question)
PHP Gurus, What I'm trying to do must be both common and simple, but I'm not asking the question right because I can't find the answers in Google or the online PHP manual using the search words like array, find, PHP, MySQL, etc... I have an array, and I want to select the fields in a table that match the contents of the array. The array consists of a set of user IDs. I want to find their emails from the table in a MySQL database which stores all their contact information. Sounds simple, right? Well, "SELECT email FROM table WHERE id = " . $array . ")"; isn't cutting it. I'm a total beginner, but I can understand why this doesn't work without being able to fix it. The data inside the array needs to be extracted into the set of ids it contains so that MySQL can read it. So I could run a while() loop and query the database for each id in the array, but that's obviously totally inefficient. There must be a command which handles this kind of thing. It's not explode(), it's not in_array()... I know what I'm after, but I just can't come up with the search term or phrasing which will allow a search engine to help me find it. Can someone please point me in the right direction? -- Cheers! Dave G [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding array in MySQL (I'm not asking the right question)
You can do something like $str = "'".implode("', '", $arr)."'"; $query = "SELECT * FROM table WHERE id IN ($str)"; Hope this helps, -- Cordialement, --- Sophie Mattoug Développement web dynamique [EMAIL PROTECTED] --- Dave G wrote: PHP Gurus, What I'm trying to do must be both common and simple, but I'm not asking the question right because I can't find the answers in Google or the online PHP manual using the search words like array, find, PHP, MySQL, etc... I have an array, and I want to select the fields in a table that match the contents of the array. The array consists of a set of user IDs. I want to find their emails from the table in a MySQL database which stores all their contact information. Sounds simple, right? Well, "SELECT email FROM table WHERE id = " . $array . ")"; isn't cutting it. I'm a total beginner, but I can understand why this doesn't work without being able to fix it. The data inside the array needs to be extracted into the set of ids it contains so that MySQL can read it. So I could run a while() loop and query the database for each id in the array, but that's obviously totally inefficient. There must be a command which handles this kind of thing. It's not explode(), it's not in_array()... I know what I'm after, but I just can't come up with the search term or phrasing which will allow a search engine to help me find it. Can someone please point me in the right direction? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Finding array in MySQL (I'm not asking the right question)
[snip] Well, "SELECT email FROM table WHERE id = " . $array . ")"; [/snip] $arrayUser[1] = 'bob'; $arrayUser[2] = 'cindy'; $arrayUser[1] = 'mark'; $countUser = count($arrayUser); for($i = 0; $i < $countUser; $i++){ $sql = "SELECT email FROM table WHERE id = '" . $arrayUser[$i] . "' "; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding array in MySQL (I'm not asking the right question)
"SELECT email FROM table WHERE id IN ('" . implode("','",$array) . "')"; And don't forget to check if $array really contains only numeric values. Dave G wrote: PHP Gurus, What I'm trying to do must be both common and simple, but I'm not asking the question right because I can't find the answers in Google or the online PHP manual using the search words like array, find, PHP, MySQL, etc... I have an array, and I want to select the fields in a table that match the contents of the array. The array consists of a set of user IDs. I want to find their emails from the table in a MySQL database which stores all their contact information. Sounds simple, right? Well, "SELECT email FROM table WHERE id = " . $array . ")"; isn't cutting it. I'm a total beginner, but I can understand why this doesn't work without being able to fix it. The data inside the array needs to be extracted into the set of ids it contains so that MySQL can read it. So I could run a while() loop and query the database for each id in the array, but that's obviously totally inefficient. There must be a command which handles this kind of thing. It's not explode(), it's not in_array()... I know what I'm after, but I just can't come up with the search term or phrasing which will allow a search engine to help me find it. Can someone please point me in the right direction? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding array in MySQL (I'm not asking the right question)
> $arrayUser[1] = 'bob'; > $arrayUser[2] = 'cindy'; > $arrayUser[1] = 'mark'; > $countUser = count($arrayUser); > for($i = 0; $i < $countUser; $i++){ > $sql = "SELECT email FROM table WHERE id = '" . $arrayUser[$i] ."' "; > } Is this method faster than just using IN and implode()? ie, "WHERE id IN ( '" . implode() . "' )" Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Finding array in MySQL (I'm not asking the right question)
[snip] > $arrayUser[1] = 'bob'; > $arrayUser[2] = 'cindy'; > $arrayUser[1] = 'mark'; > $countUser = count($arrayUser); > for($i = 0; $i < $countUser; $i++){ > $sql = "SELECT email FROM table WHERE id = '" . $arrayUser[$i] ."' "; > } Is this method faster than just using IN and implode()? ie, "WHERE id IN ( '" . implode() . "' )" [/snip] No, because it requires the query be run more than once, but you may not get the desired results. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Finding array in MySQL (I'm not asking the right question)
> Is this method faster than just using IN and implode()? Thanks to all who replied. I believe I have found what I'm looking for in using the implode() command. My assumption is that it was faster and more efficient to try and do as much processing on the PHP side and make as few queries to the database as possible. Is that a correct assumption, or am I wrong there? -- Cheers! Dave G [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Finding array in MySQL (I'm not asking the right question)
Dave G wrote: Is this method faster than just using IN and implode()? Thanks to all who replied. I believe I have found what I'm looking for in using the implode() command. My assumption is that it was faster and more efficient to try and do as much processing on the PHP side and make as few queries to the database as possible. Is that a correct assumption, or am I wrong there? Totally wrong ! It's always better to have maximum work done by MySQL Hope this helps, -- Cordialement, --- Sophie Mattoug Développement web dynamique [EMAIL PROTECTED] --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Finding array in MySQL (I'm not asking the right question)
> Totally wrong ! It's always better to have maximum work done by MySQL So does that mean that this: > $countUser = count($arrayUser); > for($i = 0; $i < $countUser; $i++){ > $sql = "SELECT email FROM table WHERE id = '" . $arrayUser[$i] . > "' "; > } ... is faster than this: > "SELECT email FROM table WHERE id IN ('" . > implode("','",$array) . "')"; ... ? -- Cheers! Dave G [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php