Proper Syntax?
Posted: Wed Oct 23, 2002 11:01 pm
Hi PHP's,
Does anyone know the proper syntax to query mysql for a substring in a string already residing in a column in a database.
I hope this isn't too confusing.
I tried to keep it brief. I thought the easiest way to explain was to use examples....
This works great to select multiple records from multiple years when only one year has been set to "date_field" in the database.
To query the database...
This matches up a whole string(date_field) to an exactly matching array element($year_filter[any])...
But if you store an array initially in the database...
You implode $date_field[] to change it to a string so you can store in the database....
Now $date_field is a string in the database (date_field="2002, 2003, 2004, 2005, 2006")
What is the proper syntax to retrieve records that contain the substring "2002" in the "date_field string" in the database?
Thanks so much for any help or advice...
Virgil
Does anyone know the proper syntax to query mysql for a substring in a string already residing in a column in a database.
I hope this isn't too confusing.
This works great to select multiple records from multiple years when only one year has been set to "date_field" in the database.
Code: Select all
$date_field="2002"
INSERT INTO etc....To query the database...
Code: Select all
<select size="6" name="year_filterї]" multiple>
<option>ALL</option>
<option >2002</option>
<option >2003</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>Code: Select all
if($year_filterї0] == "ALL"){
$year_filter = array("2002", "2003", "2004", "2005", "2006");
}
if($year_filterї0] != "ALL"){
$count_year = count($year_filter);
if($count_year <= 1 ){
$year_filter=$year_filterї0];
}
}
$count_year = count($year_filter);
if($count_year > 1 ){
$year_filter = implode( "','", $year_filter);
}Code: Select all
$query = "SELECT * FROM table WHERE date_field IN ('$year_filter')";But if you store an array initially in the database...
Code: Select all
$date_field=array("2002", "2003", "2004", "2005", "2006");Code: Select all
$date_field=implode(",", $date_field);What is the proper syntax to retrieve records that contain the substring "2002" in the "date_field string" in the database?
Code: Select all
$query = "SELECT * FROM table WHERE date_field ????????????('$year_filter')";Virgil