?
Solved

How to split a string to use in query

Posted on 2009-05-14
6
Medium Priority
?
211 Views
Last Modified: 2013-12-12
I have a dynamic string $str

the string is based on a text box where users enter the zip code and separate it by comma
so in textbox they enter
92331, 92334, 92335
and the str will then be
$str = "92331, 92334, 92335";
now I want to take that string and use it in query like this
mysql_query(select * from mytable where zip=923341 or zip = 92334 or zip = 92335)

how can I do that. I cant hardcode it as it has to depend on what users enter in the textbox.

Thanks
0
Comment
Question by:syedasimmeesaq
  • 4
6 Comments
 
LVL 54

Accepted Solution

by:
Ryan Chong earned 1000 total points
ID: 24391658
you can simply try use "In" clause instead... like:

mysql_query("select * from mytable where zip in (".$str.") ) ";
0
 
LVL 7

Assisted Solution

by:ycTIN
ycTIN earned 1000 total points
ID: 24391723
if you are trust the user input is always correctly , you can direct execute sql using IN
otherwise you may check the input like below

<?php
$str = "92331, 92334, 92335";
 
//validate and reformat input
$ids = explode(',',$str);
foreach ($ids as &$id) {
	if (!is_numeric($id)) {
		die('in valid input');
	}
	$id = trim($id);
}
 
//execute
if (empty($ids)) {
	die("no input");
} else {
	$ids_str = implode(',',$ids);
	$sql = "SELECT * FROM `mytable` WHERE `zip` IN ($ids_str)";
	
	echo $sql;
}
?>

Open in new window

0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24391949
HI I tested the sql and it gives me results from the first zip only in the string.
so if the string is like this
$str = "92331, 92334";
it will give results for 92331
but if the string is like
$str = "92334, 92331";
it will give results only for 92334

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24391969
could it be because I should rather be formatting the query like this

$sql_query = "select * from $table where `zip` IN ('92331','92334')";

where as with the current format, ut is putting the query like this
$sql_query = "select * from $table where `zip` IN (92331,92334)";
see the quote ' is missing.
how would I do that
thanks


0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 24391982
never mind..got it.
Thanks guys
0
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
ID: 31581767
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question