Solved

How to split a string to use in query

Posted on 2009-05-14
6
194 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 50

Accepted Solution

by:
Ryan Chong earned 250 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 250 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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