Solved

How to split a string to use in query

Posted on 2009-05-14
6
191 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 49

Accepted Solution

by:
Ryan Chong earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Author Comment

by:syedasimmeesaq
Comment Utility
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
Comment Utility
never mind..got it.
Thanks guys
0
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
Comment Utility
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now