Issues with using implode after pushing the variables into the array - php

I am currently testing how to use a simple implode for a mysql query after I have pushed the variables into the array. I just can't get around the error, I know it says, invalid arguments, but the array has been set up and I know it worked in another part of my page with an almost identical code.. I guess there are somewhere missing some ' or " or . or but no matter what I change it doesn't work.
I appreciate any help!
Here is the part where I set up the array:
$LFBsubjects = Array();
$LFBsubjects[] = $dataset2['subject1'];
$LFBsubjects[] = $dataset2['subject2'];
And the output I have printed via print_r is:
Array ( [0] => Mathematics [1] => English )
Now comes the query, which uses the implode function:
$SelectTSubjectsQuery = "
SELECT subject_id FROM subjects
WHERE subject IN (".implode(',', $LFBSubjects).")";
$statement = $pdo->query($SelectTSubjectsQuery);
The error is:
Warning: implode(): Invalid arguments passed in /var/www/xxx/html/lfb.php on line 626

Invalid argument error means you need to use quotes between string for MYSQL QUERY like IN ("test")
You can use as like:
$values = implode("','", $LFBsubjects);
$SelectTSubjectsQuery = " SELECT subject_id FROM subjects WHERE subject IN ('".$values."')";
Explanation:
Your array consists on string values when you use IN RANGE in MYSQL for string values than you must need to pass it in quotes.
Basic example:
$SelectTSubjectsQuery = "
SELECT subject_id FROM subjects
WHERE subject IN ('val1','val2')";
Update 1
After checking your comments, you are using wrong variable name in implode
$LFBSubjects
This should be this:
$LFBsubjects // with small s

Related

PHP Red Bean MySQL multi-value binding evaluation in getAll()

I have an array in php containing strings, which I want to use in a query with Red Bean MySQL in the following manner:
$someString = '\'abc\',\'def\',\'ghi\'';
R::getAll("select * from table where name not in (:list)", array(':list'=> $someString));
The problem is that the list is not being evaluated correctly no matter how I set the values in the array string, and the names abc, def, ghi are returned in the result. I've tried the following:
$someString = '\'abc\',\'def\',\'ghi\''
$someString = 'abc\',\'def\',\'ghi'
$someString = 'abc,def,ghi'
running the query in the SQL server manually works and I don't get those values returned, but running it within the php code with redbean is not working, and it seems that the list is not being interpreted correctly syntax-wise.
Can anyone shed some light on the matter?
Thanks to RyanVincent's comment I managed to solve the issue using positional parameters in the query, or more specifically, the R::genSlots function.
replaced the following:
$someString = '\'abc\',\'def\',\'ghi\'';
R::getAll("select * from table where name not in (:list)", array(':list'=> $someString));
with:
$someArray = array('abc', 'def', 'ghi');
R::getAll("select * from table where name not in (". R::genSlots($someArray) .")", $someArray);
This creates a $someArray length positions for parameters in the query, which are then filled with the values in the second parameter passed to the getAll function.
Notice that in this case I used a set content array (3 variables) but it will work dynamically with any length array you will use.
Furthermore, this can also work for multiple positions in the query, for example:
$surnameArray = array('smith');
$arr1 = array('john', 'pete');
$arr2 = array('lucy', 'debra');
$mergedVarsArray = array_merge($surnameArray,$arr1);
$mergedVarsArray = array_merge($mergedVarsArray,$arr2);
R::getAll("select * from table where surname != ? and name in (." R::genSlots($arr1).") and name not in (". R::genSlots($arr2) .")", $mergedVarsArray);
This code will effectively be translated to:
select * from table where surname != 'smith' and name in ('john','pete') and name not in ('lucy', 'debra')
Each '?' placed in the query (or generated dynamically by genSlots() ) will be replaced by the correlating positioned item in the array passed as parameter to the query.
Hope this clarifies the usage to some people as I had no idea how to do this prior to the help I got here.

Trouble with select query using implode variable in php?

I dont know whether it's a duplicate one or not?
Here is what i'm trying:
<?php
session_start();
include('db.php');
$valid_data=$_POST['data'];
if(isset($_POST['data']))
{
$list = isset($_SESSION['strtpnt1']) ? $_SESSION['strtpnt1'] : array();
$_SESSION['strtpnt1'][] =$valid_data;
$a=implode(',',$_SESSION['strtpnt1']);
}
?>
when i use print_r for $_SESSION['strtpnt1'] i could see like this:
array (size=5)
0 => string 'trivandrum' (length=10)
1 => string 'kochi' (length=5)
2 => string 'nagercoil' (length=9)
3 => string 'thrissur' (length=8)
I found nothing wrong with that
when i echoed the imploded variable i find the value like this:
trivandrum,kochi,nagercoil,thrissur
When i tested the imploded data in wampserver phpmyadmin's sql like this i could get an error message:
select start from tbl_place where start NOT IN(trivandrum,kochi,nagercoil,thrissur)
the error message is:
Unknown column 'trivandrum' in 'where clause'
But the query works when string value is passed ie 'trivandrum','kochi' etc.
i dont know what is wrong with my query ...here is the query part
$sql21 = "select start from tbl_place where start NOT IN('".$a."')";
For now you can try this but prepared statement is best for it. You can use PDO or MYSQLi . it doesn't work for you because NOT IN needs the list to individually be quoted and separated by commas.
$a=$_SESSION['strtpnt1'];
$new_a= "'" . implode("','", $a) . "'";
$sql21 = "select start from tbl_place where
start NOT IN($new_a)";
Similar Ideas
:Can I bind an array to an IN() condition?
Proper format for PDO and MySQL IN/NOT IN queries
There is error in your parameter $a value, all values inside IN() should be quoted and separated by commas.
Change your code like following:
$a=implode("','",$_SESSION['strtpnt1']); // ',' => "','"
$sql21 = "select start from tbl_place where start NOT IN('".$a."');";// no change
// NOW the SQL query will became: select start from tbl_place where start NOT IN('trivandrum','kochi','nagercoil','thrissur');
Now it should work.
$sql21 = "select start from tbl_place where start NOT IN('".$a."')";
will querying like this :
$sql21 = "select start from tbl_place where start NOT IN('trivandrum,kochi,nagercoil,thrissur')";
which will treat as a whole string
you have to do in passing variable
$a=implode("','",$_SESSION['strtpnt1']);

Converting exploded array into string

In the project that I am creating, I have a mysql column that is an array of different pieces of data from the database. These pieces of data have info that I want to display. In order to separate these items in the array I used,
$get_query = mysql_query('Select array FROM data');
while($dataRow = mysql_fetch_assoc($getfrnd_query)) {
$array = $dataRow['array'];
if($array != "") {
$data_from_array_column = explode("," $array);
$getdata = mysql_query("SELECT * FROM info WHERE item = $data_from_array_column");
//Then I used a mysql_fetch_assoc to get data based on the item.
}
}
When I run this code, I get an error "Array to string conversion on the line with $getdata".
Is there any way to get this to work?
Thank you.
The problem is that explode returns an array containing the strings in between the character(s) you used as a delimiter (, in your case), not a string.
PHP is getting mad because it doesn't know how to convert your array to a string automatically. So, you will need to convert it yourself. The options are:
You want to select the row where item is equal to the nth element in the array, $data_from_array_column. If this is the case, you need to insert the following line of code after your explode:
$data_from_array_column = $data_from_array_column[0];
If you want to select where it matches any of the elements in the $data_from_array_column array, it will get more complicated. You would need to add this line after the explode:
$data_from_array_column = implode("' OR item='",$data_from_array_column);
and change your query on the next line to:
$getdata = mysql_query("SELECT * FROM info WHERE item='$data_from_array_column'");
This will create a MySQL query that looks some thing like this:
SELECT * FROM info WHERE item='foo' OR item='bar' OR item='bla'

how to compare input array with mysql column?

Following is my code showing some error in mysql query:
<?php
$con=mysql_connect('localhost','root','');
$str=$_GET["message"];
$stor=explode(" ",$str);// converting message into array
mysql_select_db('words',$con);
for($j=0;$j<=30; $j++)
{
mysql_query($con,"UPDATE blacklist SET $stor=1 where $stor=0");//if column name=element in array then make it as 1 in database
}
mysql_close($con);
?>
Your code is vulnerable to SQL Injection. Read up on prepared statements and use PDO/MySQLi.
$stor is an array object and cant be used directly in the query. If you want to use it, try using
IN('.implode(",", $stor).')
the code above does the following:
implode() - takes an array and turns it into a comma separated string.
IN() - compares the given comma separated values and returns true if at least one of them exists.
Example (implode):
implode(",", array(1,2,3)) IS EQUAL TO "1,2,3"
Example (IN):
TestID IN (1,2,3) IS SAME AS (TestID = 1 OR TestID = 2 OR TestID = 3)
You're probably getting a mysql error because your query ends up looking like this
UPDATE blacklist SET Array=1 where Array=0;
If you're just echoing out a full array, you get Array instead, you'll need to specify an array element ($stor[1] for example).
What you'll want to do is replace your for loop with a foreach so that you can just throw out the elements one at a time.
Also, your arguments are backwards.
foreach($stor as $word)
{
mysql_query("UPDATE blacklist SET $word=1 where $word=0", $con);
}

PHP to PostgreSQL insert unnested arrays into multiple rows

I have some data in PHP arrays/variables ready to insert into a PostgreSQL table via an INSERT statement.
For a simple example say I have the following information:
$name= 'someName';
$time = array(1,2,3);
$elevation = array(100,200,300);
(In my real application these are double precision and potentially 1,000+ values)
I also have my postgresql table with columns "name","time","elevation"
I want to insert these in a single INSERT statement, and have been given multiple different ways to do this.
Loop and insert one data point (row) at a time.
Use unnest() on the arrays and do a single insert (fastest)
My question is can I pass a single variable name, and the un-nested arrays and have name repeated every row (ever array element), or do I need to construct a repeated array for name the equivalent count() as the other arrays?
An example statement:
*cr_query is a custom PHP pg_query wrapper we use
cr_query($conn,"INSERT INTO sometable (name,time,elevation) VALUES ({$name},{unnest($time)},{unnest($elevation)}););
This would insert into sometable:
ID name time elevation
1 someName 1 100
2 someName 2 200
3 someName 3 300
Am I correct here or do I need to do something else?
EDIT:
Lets say I also have another variable "surface". Surface can be a double value or can be NULL. So I want to insert into the table to look like so:
ID name time elevation surface
1 someName 1 100 50
2 someName 2 200 NULL
3 someName 3 300 100
In PHP, using the method perscribed by klin below an array for surface in the unnest statement would become unnest(array[50,,100]); This throws an error like so:
(Error from my real data)
ERROR: syntax error at or near "," LINE 3: ...-6,5.75E-6,5.75E-6,5.75E-6,5.75E-6]),unnest(array[,,,,,,,,,]... ^
EDIT 2:
Now that all of the "encoding" is working a new problem has popped up. Say the example column "surface" above is type double precision.
Say I am inserting an array, but for this set all of the data is null.
The essential piece is:
unnest(array[null,null,null,null,null,null,null,null,null,null])
However, this array is of type string. Add a single value to it and it becomes the type of that numeric value, but I need to be able to handle this.
My question is: How do I insert an unnested array of all null values into a double precision column? (I tried to cast ::double precision) but it's not possible.
Assuming your cr_query() function is not doing any magic things, your code is going to raise postgres syntax error. You can use unnest but you must prepare proper query text.
Try your code:
$name= 'someName';
$time = array(1,2,3);
$elevation = array(100,200,300);
echo "INSERT INTO sometable (name,time,elevation) VALUES ".
"({$name},{unnest($time)},{unnest($elevation)})"
echo: INSERT INTO sometable (name,time,elevation) VALUES (someName,{unnest(Array)},{unnest(Array)})
Obviously it is not what we want to send to postgres. How to repair this?
$name= 'someName';
$time = array(1,2,3);
$elevation = array(100,200,300);
$timestr = 'array['. implode(',', $time). ']';
$elevstr = 'array['. implode(',', $elevation). ']';
echo "INSERT INTO sometable (name,time,elevation) ".
"VALUES ('$name',unnest($timestr),unnest($elevstr));"
echo: INSERT INTO sometable (name,time,elevation) VALUES ('someName',unnest(array[1,2,3]),unnest(array[100,200,300]));
I think this is correct query. Note that I enclosed text variable '$name' in single quotes.
If you have nulls in your arrays you have to replace all empty strings to 'null' in prepared text for query.
Probably the simplest way to do it is to use str_replace().
As the conversion is getting more complicated it is handy to write a function (say "pgstr()") for that purpose.
function pgstr($array) {
$str =
str_replace('[,', '[null,',
str_replace(',]', ',null]',
'array['. implode(',', $array). ']'));
while (strpos($str, ',,') > 0) $str = str_replace(',,', ',null,', $str);
return $str;
}
$name= 'someName';
$time = array(1,2,3,4);
$elevation = array(100,null,300,null);
$surface = array(null,null,3.24,null);
$timestr = pgstr($time);
$elevstr = pgstr($elevation);
$surfstr = pgstr($surface);
echo
"INSERT INTO sometable (name,time,elevation,surface) ".
"VALUES ('$name',unnest($timestr),unnest($elevstr),unnest($surfstr));";

Categories