I'm not good at english so i appologise it for first.
I have an array value like this:
$a=['php','java','.Net']
please look at this link provided.
http://sqlfiddle.com/#!9/2c07a/4
How to make selection with the table.
Please help me guys...I thank you in advance...
Use FIND_IN_SET
PHP code:
$a=['php','java','.Net']
$query_part = '';
foreach($a as $v)
{
$query_part .= "FIND_IN_SET('$v',keyskills) OR ";
}
$query_part = trim($query_part, ' OR ');
$sql = "select jobid from jobfair where $query_part";
//select jobid from jobfair where FIND_IN_SET('php',keyskills) OR FIND_IN_SET('java',keyskills) OR FIND_IN_SET('.Net',keyskills);
If you want to return rows that match all values then use AND clause.
Reference: https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set
Related
I have a database like which has multiple columns and when querying it with a WHERE clause it won't get any results.
Here is the code I am using :
$columns = $_GET['var'];
$where = $_GET['where'];
$checkValue = $_GET['checkValue'];
$userInput = $_GET['userInput'];
$query = "SELECT ";
foreach($columns as $val)
$query .= "$val, ";
$query .= "FROM Email";
if($where === "yes")
$query .= " WHERE $checkValue = '$userInput'";
$columns is multiple checkboxes for the user to select which columns they wish to see. It works perfectly except when adding the where clause. When I've been testing it I made sure that the it was exactly the same as in the database. Also the $checkValue is a dropdown list which values are exactly the same as in the database. Also just to note later on I edit the query so the last comma is removed.
To print it out I use :
while($c = mysqli_fetch_assoc($results)){
foreach($columns as $val){
$header = ucwords($val);
echo "<b>$header</b><br>";
echo $c[$val]."<br>";
}
echo "-------------------------------<br>";
}
This is the query that is outputted when not using the where clause and works:
SELECT date, mediatype FROM Email
And here is the query that doesnt work:
SELECT date, mediatype FROM Email WHERE mediatype = 'Blog'
Any advice?
EDIT:
Here is the table with:
There is more columns but these are ones I want to focus on.
Your generate SQL request seems to have a syntax error. Just change the way you generate it.
Instead of
foreach($columns as $val)
$query .= "$val, ";
Try
$query .= implode(', ' $columns);
That will skip the last comma.
The blog column had an extra empty line that 'Blog%' wasn't working on. I went in the database and deleted the extra line and used the query again and it worked.
Thanks everyone for the help :)
I want to break comma seperated values which are retrieved from database and then put each individual in a string and print it in a for loop only. I am getting the values from database but i am not be able to break values. Below is the code what i have done right till now.Please help to solve my issue. Thanks in advance.
$query="select * from create_segment";
$result = mysql_query($query,$link) or die(mysql_error());
$number_of_segment=mysql_num_rows($result);
while($row=mysql_fetch_assoc($result))
{
$segments[]=$row;
}
foreach($segments as $success) {
$thePostIdArray = explode(', ', $success['subjects']);
for($i=0; $i < count($thePostIdArray); $i++)
{
echo "string...".$strings=$thePostIdArray[$i];
}
}
It output:
string...1,2,3,4,5,6,7,8,9,10
string...1,2,3,4,5,6,7,8
but i want something like this:
for(......)
{
echo "values...".$i;
}
which should output
values...1
values...2
values...3 and so on.
my database structure is like:
id subjects
1 1,2,3,4,5
2 1,2,7
Please try to use ',' instead of ', ' in your
explode(', ', $success['subjects']);
statement so it would look like:
explode(',', $success['subjects']);
Use:
explode(',', $success['subjects'])
You had a space after the comma, but there are no spaces in the database values.
If you have the opportunity to change the database structure you should convert that table into something like this:
Id Subject
1 1
1 2
1 3
2 1
etc.
It will make your queries much easier, and it the right way to go. It will also make it easier for you in the future do join this table with other tables to get the desired results. I know this doesn't answer your questions, but some good advice will never hurt. One of the other answers will answer your question directly.
$query="select * from create_segment";
$result = mysql_query($query,$link) or die(mysql_error());
$number_of_segment=mysql_num_rows($result);
while($row = mysql_fetch_assoc($result))
{
foreach (explode(',',$row['subjects']) as $value){
echo echo "string... {$row['id']} " . $value . '<br />';
}
}
My code checks if there is $GET value, if not then assign ALL values of array.
Seems like simple thing,not sure why its not working.
if(isset($_GET["smonth"])) {$smonth= $_GET["smonth"];
}
else {$smonth =12;} working , but not what I want
else {$smonth =array (1,2,3,4,5,6,7,8,9,10,11) ;}
After that I would like to use it in SQL :
and d.month_of_year = '".$smonth."%'
That would be something like
and month_of_year = (all values of array) or 1 value)
My Question:
What would be best solution to check, if active month is available? If not, assign All months to query.Thank You
The built-in PHP functions of in_array and implode should solve your issue:
in_array('1', $_GET["smonth"]); // checks if January is in $_GET["smonth"]
implode("," , $_GET["smonth"]); // Pull all of the values out of $_GET["smonth"] as a A STRING
Try in your statement and d.month_of_year IN (" . implode(',', $smonth) . ")
= operator checks for single value. If you want to check multiple values, use in.
and d.month_of_year in (".$smonth.")
You also have a % there, which works with LIKE queries.
<?php
if(isset($_GET['month'])){
$month = date('m'); //This would give you the index of the current month.
$array = array('01','02','02');
$query = "select * from table where month = ";
if(in_array($month,$array)){
$query = "select * from table where month = '".$month."'";
//Then query here
}
else
{
$query = "select * from table";
$where = "";
foreach($month as $m){
$where .= ' month = "'.$m.'" and ';
}
//There would be a ending and pls just try remove it
$query .= $where;
// then query here
}
}
?>
I'm having a bit of trouble getting my retrieved values from an SQL query into the correct format.
I've managed to join multiple rows into the one value, however I am not sure how to make it separate each of the values with a comma. Essentially I need all the ID's of a product to be retrieved as, for example, if the database had values of '5,6,9,1' '1,3,4' and '2,1' I want it to throw a comma in between each like -> '5,6,9,1,1,3,4,2,1' instead is doing something more like -> '5,6,911,3,42,1' which is what it is doing at the moment.
The code I'm using is below. Any help would be greatly appreciated.
$hist = "SELECT ORDITEMS FROM cust_orderc WHERE ORDDATE >
to_date('".$olddate."','dd/mm/yyyy')";
$histitem = OCIParse($db, $hist);
OCIExecute($histitem);
while($row = oci_fetch_array($histitem)){
$pastitem .= $row['ORDITEMS'];
}
echo "$pastitem";
You can do same in oracle using LISTAGG
$hist = "SELECT LISTAGG(ORDITEMS) as ORDITEMS FROM cust_orderc WHERE ORDDATE > to_date('".$olddate."','dd/mm/yyyy')";
Edit OR PHP way
$pastitem = '';
while($row = oci_fetch_array($histitem)){
$pastitem .= $row['ORDITEMS'] . ',';
}
$pastitem = trim($pastitem, ",");
echo $pastitem;
I have this code above which i use to implode some variable.
The issue is that i need to create the same thing for $hostess_name[] as i did for $hostess_id_selected[].
I don't know what am i doing wrong.
I need to implode it the same way as i did with $hostess_id_selected1
foreach($hostess_id as $val) {
$hostess_id_selected[] = $val;
$sqlnomehostess="SELECT nome_hostess FROM hostess where id='$val'";
$resultnomehostess=mysql_query($sqlnomehostess)or die(mysql_error());
$hostess_name= array();
while ($row=mysql_fetch_array($resultnomehostess,MYSQL_ASSOC)) {
$hostess_name[] = $row['nome_hostess'];
}
}
$hostess_id_selected1 = implode("-",$hostess_id_selected);
you have $hostess_name= array(); inside the loop. move it above
EDIT:
some tips:
foreach($hostess_id as $val) {
$hostess_id_selected[] = $val;
// this is pointless, i mean - you are recreating $hostess_id
btw, just a little tip for improvement - instead of running many SQL queries you can use a single query:
$sql = "SELECT GROUP_CONTACT(`nome_hostess` SEPARATOR '-') AS name_list
FROM `hostess`
WHERE id IN (".implode(',',$hostess_id).")";
if the items in $hostess_id are not necessarily numeric:
$sql_ids = array();
foreach($hostess_id as $id)
$sql_ids[] = mysql_real_escape_string($id);
$sql = "SELECT GROUP_CONTACT(`nome_hostess` SEPARATOR '-') AS name_list
FROM `hostess`
WHERE id IN (".implode(',',$sql_ids).")";
and after that:
the sql query returns 1 row with a column called "name_list", which contains the names joined with "-".
if you want to maintain the order of the id and name - you should do:
$sql = "SELECT
GROUP_CONTACT(`nome_hostess` SEPARATOR '-') AS name_list,
GROUP_CONTACT(`id` SEPARATOR '-') AS id_list
FROM `hostess`
WHERE id IN (".implode(',',$hostess_id).")";
implode is a basic function of php. It always work perfect.
Just check your array $hostess_id_selected, what it returning.
I think it will help you.