i want to know how to get all fields value search by array.
i have array that s
my table is
id content
1 zahr
2 hai
.
.
.
and so on
$a = {2,3,4,5,43,32};
i have to take the contents by this id(from array "a").
i know, i can use "for" loop for getting each element from mysql.
but i would like to use any filters or any predefined function
thanks and advance
SELECT * FROM table WHERE id IN (2,3,4,5,43,32)
the statement could be created by using implode like:
$myvar = '('.implode(',',$a).')';
and then
SELECT * FROM table WHERE id IN $myvar
use IN
Related
I want to make array like this for the Answer field:
$my_array = array("Melon-Daiquiri.jpg1_2.jpg","banner1.png","images.jpg");
After that I want to use Shuffle function to show this record randomly.
Here is my var_dump($array) Output. (coming from database)
Simply use array_map like as its an array of objects you can simply achieve it like as
$result = array_map(function($v) {
return $v->Answer;
},$your_array);
If your are getting your data from database you can use RAND() mysql method to shuffle your data. example
SELECT * FROM users ORDER BY rand() LIMIT 20
My session variables are
$_SESSION["listofIds"]=163,164;
$_SESSION["listofVals"]=4,3;
I want to select session values like
$_SESSION["listofIds"][0]=163;
$_SESSION["listofIds"][1]=164;
$_SESSION["listofVals"][0]=4;
$_SESSION["listofVals"][1]=3;
I tried
echo $_SESSION["listofIds"][0];
then it prints '1' i.e. first character. How can I handle this?
Also how can I get 1st element of $_SESSION["listofIds"] and 1st element of $_SESSION["listofVals"] after placing in while loop for mysql query.
Your session variables seem to be strings. Use
$IdsArr = explode(",", $_SESSION["listofIds"]);
echo $IdsArr[0];
With explode splitting the string by "," to an array.
$listofIds = array();
$listofIds = explode(",",$_SESSION["listofIds"]);
now you can access the variables using listofIds[0] or listofIds[1].
First of all you should start by defining from scratch an array while placing values in the $_SESSION array. This is a better way to write code.
Anyway as all the other answers said:
$listofIds = array();
$listofIds = explode(",",$_SESSION["listofIds"]);
will convert your string into an array and you will be able to access each value in different ways. Most commons:
using a foreach loop
getting the index of the item you want to use.
If you want to use the first item in a query you should get its value using the index (array starts from 0) so your value will be $listofids[0]. The same logic will apply to the other $_SESSION value.
Another good practice for programming: when you put an array value in a query string assign it to a variable before:
$id_to_search = $listofids[0];
and then use it into the query:
$sql = "SELECT * FROM your_table WHERE id='$id'";
This will save you a lot of headache with singe and double quotes.
I have following array in PHP:
$arr = [1, 3, '4A', '5A', '5C', '2B', '2C', '2E'];
Somehow I need to convert this array to a string which look like this:
$filter = "(id like '1%') OR
(id like '3%') OR
(id like '4A%') OR
(id like '5A%' OR id like '5C%') OR
(id like '2B%' OR id like '2C%' OR id like '2E%')";
The first character of the array value indicates a unique categorie and is wrapped in parentheses.
In the end I will use this to query the database;
$sql = "SELECT * FROM list WHERE {$filter}";
Can someone help me converting this array to the correct string ? What is the best method to achieve this ?
To give you an idea of what I'm trying:
The 'id' column in my database row looks like; 1B2875. Where the first char (1) indicates a categorie and the second char (B) the subcategorie. The given array is a result of a client-side filter request.
You can use regular expressions in SQL:
SELECT * FROM list WHERE id REGEXP '^(1|3|4A|…)[0-9]+$'
The value inside the parentheses can be generated with the PHP function implode('|', $filter_strings)
Note that you should validate and escape the filter strings first to prevent the user input from manipulating the query. The function preg_quote is useful here.
I got an array with ids like :
$array = array('1234','3214','8764','8467'2364','1113')
and what I do to retrieve the items data is to use the IN statement, like :
...product_id IN ('.$array .')...
The problem is that the data I get is given with an order which I don't know (e.g 8467,2364,3214,1234...).
Is there any way to get the data from mysql following the order provided in the array ?
Thanks in advance
Use order clause like,
$oderFields = implode(",",$array);
SELECT....ORDER BY field(product_id, $oderFields);
I think you can just use implode function to your array so you will have as string which you can use in your query
$array = array('1234','3214','8764','8467','2364','1113');
$string = implode(",", $array);
echo $string;
this will output
1234,3214,8764,8467,2364,1113
Now you can use this string in your query
product_id IN ('.$string.')
Which will be
product_id IN (1234,3214,8764,8467,2364,1113)
I have a "recruiter" table in my database which has different attributes and one of them is "Professions". "Professions" is a serialized array which I get from a multiple select form. And this works fine.
When I unserialize this attribute nothing is printed - no error, no text.
This is a code I was testing serialization with:
$sql = 'SELECT Company_name, Status, Size, Professions, Seniority_levels, Sector, Website, Location FROM Recruiter';
$query = mysql_query($sql, $con);
while($result = mysql_fetch_array($query, MYSQL_BOTH)){
$recruiters[] = array($result[0], $result[1], $result[2], $result[3], $result[4], $result[5], $result[6], $result[7]);
}
foreach($recruiters AS $recruiter){
$test = unserialize($recruiter[3]);
echo $test[0].'<br>';
}
So basically $test[0] prints nothing although the new lines are printed. Please help!
try printing the $test array and the $recruiters and the $recruiter arrays. See if the result is fine before the unserialisation of the data. If the query returns any data. Also try the while loop with mysql_fetch_assoc. Let me know of the results and if this solves the problem
test = unserialize($recruiter[3]); should become test = unserialize($recruiter[5]); since the sector field is the sixth column .
However what if somewhere in the future you might need to select rows where sectors equal smth ? serialize whont help you then so i suggest you have a look at a different implementation for the sector filed witch is called bitwize http://www.litfuel.net/tutorials/bitwise.htm
Edit
Asuming you hit the right column and the column contains a:1:{i:0;s:27: a:1:{i:0;s:27: a:38:{i:0;s:27: a:9:{i:0;s:39:, it looks like the serialized array is not fully saved in you're db, it's only part of it . So the unserialize function whont return you an array . Have a look at the length of the mysql field i assume you've set it smaller than you need so you're data is trimmed on insert/update .
Edit
a:1:{i:0;s:27: you're still missing the rest of the serialized array . s:27: means a string is following containint 27 characters, and you're serialized array stops there when it should look like
a:1:{i:0;s:27:"123456789012345678901234567";}
( a:1 stands for an array containing 1 value with it's content between {}, i:0; is the array key 0, s:27:""; stands for a string containing 27 characters as the value for the i:0 key ) .