using strpos in mysql? - php

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 '%?%';

Related

php checking array values

Hey all i am trying to figure out a way to see what all values are within the array:
$sql = "select * from product where type = ? limit 1";
$query = self::$__CONN__->prepare($sql);
$query->execute(array($type)) or die(print_r($query->errorinfo()));
I am new at PHP and im not sure what all that is doing to populate the query string. I am guessing that the first query is connecting to the server and sending out the query but how do i see what its returning back in the array above?
Any help would be great!
Just dump the $type variable
var_dump($type);
Or if you are interested in the array wrapper itself
var_dump(array($type));

php sql where command to check for containg

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);

PHP: mysql query "WHERE first 3 (or 4,5,6 depending on length of string) characters = '$string'"

I am doing a ajax suggestion function
The user gives an input of a word (like ICE), then the ajax script passes this to a .php script and that script looks it up in the database and should suggest the word ICECREAM.
This is where I am stuck. I need a query which goes like this:
SELECT * FROM table WHERE first str_len($string) characters = $string
Could you help me out please?
You want LIKE for this.
SELECT
...
WHERE
field LIKE '$string%'
SELECT * FROM table WHERE first LIKE 'string%'
$length = strlen($input);
$statement = $pdoObject->prepare("SELECT * FROM table WHERE LEFT(first, $length) = :input");
$statement->execute(array(':input' => $input));
This is safe because $length is guaranteed to be an integer so no need to make it a parameter.

How do i parse the the value from a string in the function call?

i have function which is something like this
function multiple_delete($entity1,$entity2,$entity2) {
$query = "SELECT * FROM tablename WHERE id = '4' ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $entity1;
echo $entity2;
echo $entity3;
}
and my function call is
multiple_delete('$row[\'pic_title\']', '$row[\'pic_brief\']', '$row[\'pic_detail\']');
keeping in mind the three value which i am passing through the parameter is the entity name of particular table.
now this function will print it as the string i.e ($row['pic_title'], $row['pic_brief']', $row['pic_detail']) and hence not parse it as the value which i want it to do. if it parse it as the value then i will be able to get the records from the database. i have tried with the combination of single quotes, doubles, with concatenation operator etc. is there any way i tell the script that do not parse it as the string instead treat it as it have been declared to fetch the value from database. does php have any function for this ? or i am going wrong with the logic.
if i skip the parameters and declare in the functions something like this.
echo $row['pic_title'];
echo $row['pic_brief'];
echo $row['pic_detail'];
it works perfectly fine . why is that when i try to achieve the same thing with the help of parameter it refuses to fetch the value from the database, and instead it returns the same declared string from the function call.
Please do not tell me that i dont need that parameter, i need it because i want it to perform the dynamic data manipulation, with regard to different tables and different table entities. and the above function is just the demonstration of my problem not the exact function. if you want to have a look at the exact function you can check here.
What is wrong with my function?
thank you
Just pass the names of the columns:
multiple_delete('pic_title', 'pic_brief', 'pic_detail');
Then you can use them to access the corresponding values in the row array by using the names them as keys:
function multiple_delete($entity1, $entity2, $entity3) {
$query = "SELECT * FROM tablename WHERE id = '4' ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row[$entity1];
echo $row[$entity2];
echo $row[$entity3];
}

mysql IN parameter

When the user check more than one (checkbox) option which are then combine into a string of "apple,orange,pear"
SELECT id, pos, FROM $db WHERE dtime>='$now' AND jsub IN ('$arr[1]') ;
When I pass the string to $arr[1], it won't work correctly, how do I split into array and get mysql IN function to process correctly?
use:
$str = "SELECT id, pos, FROM $db
WHERE dtime>='$now' AND jsub IN ('".explode(',',$arr."')";
and don't forget to sanitize the parameters before ...
Use FIND_IN_SET.
SELECT id, pos, FROM $db WHERE dtime>='$now' AND FIND_IN_SET(jsub, '$arr[1]')
the question is WAY unclear, but I suspect you want something like
foreach ($arr as $key => $item) $arr[$key] = mysql_real_escape_string($item);
$in = "'".implode("','",$arr);
$sql = "SELECT id, pos, FROM $db WHERE dtime>='$now' AND jsub IN ($in)";
But man, I hate guessing.
Why don't you get yourself a static query, without any PHP code?
To see, if it ever works?
If not - ask on SO for the proper query. SQL query, not PHP code.
If yes - write a PHP code that produces the exact query.
Compare to the example one.
If failed - ask on SO for the PHP code, providing an example of the resulting query and an array.
Is it too hard rules to follow?

Categories