php sql where command to check for containg - php

I get a string vaiable using POST in my php. I want to append this string so that i can use in the sql query to check for containing this string. I want something like this %string%
What i am doing now this gives me error:
$hotelName = '%'+hotelName_old+'%';

just to be sure: did you change your statement to ... WHERE fieldname LIKE '%value%'??

it should be like this
$hotelName = "%'".$hotelName_old."'%";

try this.
just directly use your variable.
$sql = "select * from `tbl_name` where `field_name` like '%{$find}%'"
$result = mysql_query($sql);

Related

I need the output of this $LoadId=implode(',',array_filter($_POST["load"])); to look like this ('7209','7210')

SO i get data from a form using this
$LoadId=implode(',',array_filter($_POST["load"]));
I then would like to submit this to a MSSQL query with an "in" statement
where myLoadId in $LoadId
but the $LoadID looks like 7209,7210 and I need it to look like
('7209','7210')
Seems your LoadId column contains interger value so why you need single quotes ' around it? Simply use-
$LoadId=implode(',',array_filter($_POST["load"]));
$query = "SELECT * FROM your_table WHERE myLoadId IN ($LoadId)";
echo $query;
If you still need quotes around it then you can do it this way-
$LoadId = "'".implode("','", array_filter($_POST["load"]))."'";
$query = "SELECT * FROM your_table WHERE myLoadId IN ($LoadId)";
echo $query;
WORKING DEMO: https://3v4l.org/2XEjJ
Put simple quotes around the implode() and change it's glue from , to ',' :
$LoadId = "'".implode("','", array_filter($_POST["load"]))."'";

small php fetch query with "WHERE" issue

$u_ress = mysql_query("SELECT * FROM `blackjack` WHERE `brukernavn`='$spiller->brukernavn' AND `by`='$by'");
$bj = mysql_fetch_object($qry);
This code wont work. It wil only show my $by, but its not what i want. I want it to get from blackjack where brukernavn is ( as it said) AND from by aswell.
How can i this?
SELECT * FROM blackjack WHERE brukernavn='{$spiller->brukernavn}' AND by='$by'
Notice when doing advanced variables within the string you must use brackets.
Try below code,
echo "Query:=".$u_ress = ("SELECT * FROM blackjack WHERE `brukernavn` ='{$spiller->brukernavn}' AND `by` ='$by'");
$query = mysql_query($u_ress);
//$bj = mysql_fetch_object($query);
mysql_query is not recommended.

Getting value from my sql data base using get method

I am getting values from mysql data base using get method i am passing survey_id and question_id from the url
like below
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1,question_id=1
but it is giving error
my php code is given below for fetching
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id"' ");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
echo json_encode($rows);
This line is having error,this:
$question_id"' "
should be
$question_id . "'"
You should be separating get variables with & not , in the url.
That's because you should use & instead of , between url parameters
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
$survey_id = mysql_real_escape_string($_GET['survey_id']);
$question_id = mysql_real_escape_string($_GET['question_id']);
GET params are normally separated with & not ,. So your link should look more like this:
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
Also, please note that GET variables are not automatically translated to PHP variables. You need to pull them from $_GET array:
$survey_id = $_GET['survey_id']
You does not pass value comma separated in url.You have use & in url like
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id."' ");
A dot is missing!
URL and QUERY BOTH ARE WRONG
http://myserver.com/emrapp/surveyAnswersScreenOne.php?survey_id=1&question_id=1
AND
$question_id . "' "
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id"'");
you forgot the point for concatenating the string after $question_id -> this should fix your issue:
$query = mysql_query("SELECT * from survey_Answers where survey_Id='".$survey_id."' AND question_Id='".$question_id."'");
anyway,also consider sanitizing your url-inputs -> http://xkcd.com/327/

using strpos in mysql?

I'm not sure if this is the correct function in using mysql but I was wondering if someone can help me with this problem I'm having
Ex:
I have mysql with these values
id name
1 house_home
2 movie_film
3 restaurant_food
So if I'm trying to find movie, it should get the value movie_film.
Is strpos function the right function for this case? Or is there another way?
$string = 'movie';
$query = $db->prepare("SELECT * FROM values WHERE name = ?";
$query->execute(array($string));
while($row = $query->fetch(PDO::FETCH_ASSOC)){
echo $row['name']; //this should output movie_film
}
Thanks for help in advance!
Using strpos you will get the position of that string and then trim the length you want
Use LOCATE('substring', 'mainstring') function
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_locate
You can use the LIKE 'operator' in SQL.
SELECT * FROM values WHERE name LIKE '%?%'
Please try LIKE operator for it
SELECT * from values where name LIKE '%?%';

building a sql query string with php parameters

I'm trying to do this, but it returns null?
$query_1=$field_name[0]."='{".$field_value[0]."}'";
and then
getType = mysql_query("SELECT * FROM wines WHERE $query_1") or die(mysql_error());
while if i do like this:
$getType = mysql_query("SELECT * FROM wines WHERE $field_name[0]='{$field_value[0]}'") or die(mysql_error());
it works fine.
is this even possible, or am I missing something too obvious?
thank you in advance!
You are building it the wrong way. You should never use curly brackets (or any other string) in a SQL query. Concatenate your query instead.
Like this:
$query_1=$field_name[0]."='".$field_value[0]."'";
and oh, you missed a $ before your query, thats why its null.
This works for me:
$field_name[0] = "test";
$field_value[0] = "someting";
$query_1=$field_name[0]."='".$field_value[0]."'";
echo ("SELECT * FROM wines WHERE $query_1") or die(mysql_error());
Hope it helps

Categories