I have the following multidimensional array, generated after some database Selects:
<?php
$array = array (
array("X"=>500, Y="400"),
array("X"=>234, Y="347"),
array("X"=>845, Y="345"),
array("X"=>264, Y="916")
);
?>
Now I need to do a select in a table where BOTH field X and Y are in the array. How to do that? Like:
SELECT FROM table WHERE
(X=500 AND Y=400) OR
(X=234 AND Y=347) OR
(X=845 AND Y=345) OR
(X=264 AND Y=916)
;
I can only find solutions here on StackOverflow for a single item in a query, but not for two values in a multidimensional array that needs to be exactly the same. Thank you!
If I understood, you need to read your array and pass it to your SQL.
Something like:
<?php
$where = null;
foreach ($array as $a)
{
if (is_null($where))
{
$where = " WHERE (X = " . $a['X'] . " AND Y = " . $a['Y'] . ")";
}else
{
$where .= " OR (X = " . $a['X'] . " AND Y = " . $a['Y'] . ")";
}
}
$sql = "SELECT * FROM table " . $where;
?>
Hope it helps.
Related
So the code below gets a name from a db(which is a string) and it outputs with "" and a comma.Well it was suppose to, but for some reason it only adds the quotation marks and not the commas.I need it to add the commas too.Thank for you help.
<?php
$sl = "SELECT _eName FROM vrt;";
$ret = mysqli_query($conn, $sl);
$resultck = mysqli_num_fields($ret);
if ($resultck > 0){
while ($row = mysqli_fetch_assoc(($ret))){
$b = $row['e_Name'];
$temp = array($b,);
$req = "'" . implode ( "' , '", $temp ) . "'";
echo $req;
}}
?>
You're echoing each row, your not building out $temp with all rows then imploding, so it would only contain the one item.
Instead, simply do:
<?php
$conn = ...
$result = mysqli_query($conn, "SELECT e_Name FROM vrt");
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$data = array_map(fn($row) => $row['e_Name'], $data);
echo "'" . implode ("' , '", $data) . "'";
?>
implode joins array elements with a string. Your variable $temp contains only a single element, so there is nothing to join and the element itself is returned.
this is my code :
<?php
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
$arr = array();
while ( $tam = mysqli_fetch_array($qqq)) {
$date = $tam['tanggal_awal'];
$reformat_date = date("n-j-Y", strtotime($date));
$arr[] = $reformat_date;
}
$array_date = implode(",", $arr);
?>
> output : 8-9-2018,8-10-2018,8-17-2018
> output i want to is : "8-9-2018", "8-10-2018", "8-17-2018"
could someone help me to get the output i want, thank before.
You can make your code a bit shorter:
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
while ( $tam = mysqli_fetch_array($qqq)) {
$arr[] = date("n-j-Y", strtotime($tam['tanggal_awal']));
}
$array_date = '"' . implode('", ', $arr) . '"';
In php you don't need to declare an array before pushing values to it.
Your $date variable is not needed as it's just a copy of the $tam['tanggal_awal'] value.
$reformat_date is also not needed as you can place it in the array directly.
The imploding is what is placed between the items, by having ", there you just need to add a " at each end of the string.
$arr=['8-9-2018','8-10-2018','8-17-2018'];
$array_date = '"'.implode('", "', $arr) . '"';
echo $array_date;
Output ---> "8-9-2018", "8-10-2018", "8-17-2018"
Try to implode your array like this.
Hi I have this array called $allextensionsforque. See print_r below.
Array ( [0] => Local/101#from-queue/n,0 [data] => Local/101#from-queue/n,0 )
I'm trying a foreach like below but its not showing any data on the echo. Can anyone help?
$allextensionsforqueu = mysqli_query($conqueue,"SELECT data FROM queues_details WHERE id = '1' AND keyword ='member'");
$allextensionsforque = mysqli_fetch_array($allextensionsforqueu);
$foo = "";
foreach ($allextensionsforque as $extensionrow)
{
$extensionrowstriped = substr($extensionrow['data'],6,3);
$foo = "' " . $extensionrowstriped . " ' ,";
}
echo $foo;
You don't have an array in $extensionrow, since it's only an one dimensional array. So you just need to replace $extensionrowstriped = substr($extensionrow['data'],6,3); with $extensionrowstriped = substr($extensionrow,6,3);.
Check for errors using mysqli_error(). Here is a simple approach, but you may want to improve it for production use:
$allextensionsforqueu = mysqli_query($conqueue,"...") or die(mysqli_error($conqueue));
Next, append your data to $foo instead of overwritting it in each loop:
$foo .= "' " . $extensionrowstriped . " ' ,";
Finally, verify that $extensionrow contains the data that you expect. substr() will return false if a match is not found.
This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Closed 6 months ago.
I need to select ids from database with arrays.
my english not good. I think the best way to show my codes
the form result print_r($malayalam); like this Array ( [0] => helo [1] => hi[2] => how)
I need to select its ids from table. my code is not correct. any way let me show you here
$results=mysql_query("SELECT ml_id FROM ml_table WHERE word = '$malayalam'");
$numrows=mysql_num_rows($results);
if($numrows!=0){
$ml_row = mysql_fetch_array($results);
$ml_id = $ml_row['ml_id'] ;
echo "Malayalam ID " . $ml_id . "<br />";
}
I need to add all my result in to another array.
is that possible ?
if u have any idea could you answer to me please
If I understood properly, the following is what you need
$results=mysql_query("SELECT * FROM ml_table WHERE word = '$malayalam'");
if(mysql_num_rows($results)>0){
$newArray = array(); //Create a new array
while($ml_row = mysql_fetch_array($results)) {
$ml_id = $ml_row['ml_id'] ;
$newArray[$ml_id] = $ml_row;
echo "Malayalam ID " . $ml_id . "<br />";
}
//$newArray is your new array accesible from id
}
You should write something like this:
$ids = array();
$in = '"' . implode('","', array_map('mysql_real_escape_string', $malayalam)) . '"';
$result = mysql_query('SELECT id FROM ml_table WHERE word IN(' . $in . ')');
while($row = mysql_fetch_assoc($result)) {
$ids[] = $row['id'];
}
$anotherArray = array_merge($ids, $anotherArray);
OR
$anotherArray += $ids;
$i=0;
foreach($malayalam as $key=>$value)
{
$results=mysql_query("SELECT * FROM ml_table WHERE word = '$value'");
$numrows=mysql_num_rows($results);
if($numrows!=0)
{
$ml_row = mysql_fetch_array($results);
$ml_id = $ml_row['ml_id'] ;
$ml_ad[$i]=$ml_id;
echo "Malayalam ID " . $ml_id . "<br />";
$i++;
}
}
Now when you printr($ml_ad); it will show all your id's in an array.
finally fainally found solution with the help of answers
$rArray = mysql_query("SELECT ml_id FROM ml_table WHERE word IN ('".implode("', '", $malayalam)."')");
if(mysql_num_rows($rArray)>0){
$temp_rows = array();
while(($row = mysql_fetch_array($rArray))) {
$temp_rows[] = $row['ml_id'];
}
}
the result of print_r($temp_rows) coming like this Array ( [0] => 123 [1] => 234 [2] => 312)
thank to all
My result is spitting out a duplicate of each record as opposed to a unique foreach match. As you can see I am trying to loop through and get each unique value WHERE $s=id.. I know the query needs to be within the while(),
The array $seminar being passed from checkboxes on my form.
if ($seminar) {
foreach ($seminar as $s)
$interest .= "$s ";
}
NOTE: The $interest is what I am using to insert into my table (it's not part of this problem)
For array data, this is what is being returned by $seminar
Array
(
[0] => 1
[1] => 8
[2] => 9
[3] => 10
[4] => 13
)
Here is my code:
$query_seminars = mysql_query("SELECT id, name, UNIX_TIMESTAMP(moment) FROM seminars WHERE id={$s}");
while ($sem_row = mysql_fetch_assoc($query_seminars))
{
$names .= "-- " . $sem_row['id'] . " " . $sem_row['name'] . date('D, F j, Y, g:i a', $sem_row['moment']) . "<br />";
}
You do not need the foreach in the while loop, here:
$query_seminars = mysql_query("SELECT id, name, UNIX_TIMESTAMP(moment) FROM seminars WHERE id={$s}");
while ($sem_row = mysql_fetch_assoc($query_seminars))
{
$names .= "-- " . $sem_row['id'] . " " . $sem_row['name'] . date('D, F j, Y, g:i a', $sem_row['moment']) . "<br />";
}
Also to use named indexes you use mysql_fetch_assoc, not mysql_fetch_array. (Already in above code)
The problem is not that MySql is returning duplicate (actualy tri-plicate) items. The problem is that you are iterating over each record with your foreach, as others pointed out.
One usual idiom used with MySql is:
$sql = "SELECT id, name, UNIX_TIMESTAMP(moment) FROM seminars WHERE $s=id";
if ($q = mysql_query($sql)) {
while ($f = mysql_fetch_assoc($q)) {
//($f is an associative array of the record value,
//with keys for each field)
//... do something with $f
echo "--" . htmlspecialchars($f['name'])
. date('D, F j, Y, g:i a', $f['moment'])
. "<br /><br />";
}
} elseif ($e = mysql_error()) {
//do something with the error message
//...
}
You are seeing "duplicates" because you are using a foreach inside the while loop to iterate by each field of the returned record. Since your query is returning records with three fields each (id, name and moment), you are processing each record three times.