Solved

How to split a string to use in query

Posted on 2009-05-14
6
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 51

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
Independent Software Vendors: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

733 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