Array converted to int [duplicate] - php

This question already has an answer here:
Why SUM of column values return 1?
(1 answer)
Closed 9 days ago.
I have a problem, I need to convert from an array to a full number, but I don't understand how. i`m using redbeanphp. Help please
print_r(R::getRow('SELECT SUM(view) FROM posts WHERE author LIKE ? LIMIT 1', ['dffdfghdfgdf']));
Result:
Array ( [SUM(view)] => 27 )
if through echo, then it simply gives an error that it is an array.
Result:
Warning: Array to string conversion in

You should print the value of the array element, not the array itself.
Also, you may use an alias for a result column using the as operator.
$result = R::getRow('SELECT SUM(view) as s FROM posts WHERE author LIKE ? LIMIT 1', ['dffdfghdfgdf']);
print_r($result['s']);
echo would work either in this case.

Related

Passing an array to a query using a WHERE clause - works in phpmyadmin, not in my script [duplicate]

This question already has answers here:
Can I bind an array to an IN() condition in a PDO query?
(23 answers)
Closed 12 months ago.
I've searched for the answer online, if I've missed something obvious, I would appreciate links. Otherwise, I'd be grateful for direct help. This is the first time I've ever tried a query like this.
I have the following query:
SELECT * FROM `dice_t` WHERE qty IN (:qty) AND opacity IN (:opacity) AND color IN (:color)
To which I am feeding the following array:
Array
(
[qty] => 1,2
[opacity] => 3
[color] => 467,1007
)
It works perfectly (retrieves 163 rows) in phpMyAdmin (when I type in the values), but in my script, it retrieves only 114 rows, which corresponds to it using only the first value in each field (i.e. qty: 1; opacity: 3; color: 467). I have verified this by running the query with only those values in phpMyAdmin.
My code looks like this:
$statement = $dbConn->prepare($sql);
$statement->execute($queryData);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
When I print the values of $sql and $queryData I get the values listed in the first two code blocks above.
The fields are all integers. I tried searching with single quotes around the values, but got an error.
I can't figure out what I'm doing wrong.
the Varable :qty is handled as Strinf So you have '1,1'
So you must use FIND_IN_SET
SELECT * FROM `dice_t` WHERE FIND_IN_SET(`qty`,:qty) AND FIND_ON_SET(`opacity`,:opacity) AND FIND_IN_SET(`color`,:color)
other ways are in this thread Can I bind an array to an IN() condition?

use an array of unknown length in where clause of mysql [duplicate]

This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
MySQL, PHP: Select * from table where id is not in array
(3 answers)
Select all field where field value not in array
(3 answers)
Closed 3 years ago.
I am writing an SQL query. I have an array of unknown length and I want to select the data fromMySQL by using that array in the WHERE clause of the query. This is my query right now and it is working fine
$sql = "SELECT DISTINCT messagesutou.SenderID from messagesutou where (messagesutou.SenderID !='$items[1]' AND messagesutou.SenderID !='$items[0]' AND messagesutou.SenderID !='$items[2]') AND messagesutou.RecieverID='$uid'";
But in this I know the length of array ( 3) and I just used the array name with index for testing purpose. Now i want to know if array length is unknown then how would I write this query?
$list = implode(',', $items);
and
SELECT DISTINCT SenderID
FROM messagesutou
WHERE 0 = FIND_IN_SET(SenderID, '$list')
AND RecieverID='$uid'
or (taken from Jens's answer which was deleted by him)
SELECT DISTINCT SenderID
FROM messagesutou
WHERE SenderID NOT IN ($list)
AND RecieverID='$uid'
The difference - both variants are applicable when SenderID and $items values have a numeric type, only the former when they have string type, none when they have string type and contain commas or ticks.
But the latter may be adapted:
$list = '\''.implode('\',\'', $items).'\'';
and
SELECT DISTINCT SenderID
FROM messagesutou
WHERE SenderID NOT IN ($list)
AND RecieverID='$uid'
It now acccepts any datatype and allows commas (but not ticks - they must be quoted before imploding).

Laravel Query: Php implode array string values [duplicate]

This question already has answers here:
Querying MySQL with IN clause using PHP [duplicate]
(4 answers)
php mysql IN clause not working with CSV variable. only first row is affected
(1 answer)
Laravel Eloquent "WHERE NOT IN"
(12 answers)
Closed 3 years ago.
Currently I have this variable
$arrays = implode(", ", $request->pmChck);
and If I try to return this variable. I will be getting this kind of output
2019-100,2018-100
As you can see the values are separated by commas
Now in my laravel query, I'm trying to get all the records of the employee except for these two company_id above.
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN('".$arrays."')");
The query is not working, it shows all the data and also the data with the company_id of 2019-100 and 2018-100
It should listing all the data except for these two company_id 2019-100 and 2018-100
Is there anything wrong with my format or syntax?
Assuming $request->pmChck is an array of companies you want to exclude, the query will be :
DB::table(..)->select(..)->whereNotIn('company_id', $request->pmChck)->get();
Your imploded array looks like
2019-100, 2018-100
and so is query:
SELECT * FROM view_employee_info WHERE company_id NOT IN('2019-100, 2018-100')
try imploding like this:
implode("', '", $arr)
And query will be ok:
SELECT * FROM view_employee_info WHERE company_id NOT IN('2019-100', '2018-100')
The problem in your query are the single quotes in the IN part.
If you change from
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN('".$arrays."')");
To
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN(".$arrays.")");
it should work as you expect it. It currently does not work, because your query would be like the following:
SELECT * FROM table WHERE company_id IN ('123, 234')
which would treat your input values as a single value, 123, 234

Get any of the item from the Array with whereIn in Laravel 5.3 [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 10 months ago.
I'm using laravel 5.3 and trying to build a query with multiple where and wherein function. Here is my code:
$posts_id = "1,3,4"; //from my query
$results = Post::select('*');
$results->whereIn('posts_id', [$posts_id]);
$resutls->get();
There is 1 post which have an id of 4 in my database, but my query doesn't returning anything. Though there is 4 in my post ids $posts_id = "1,3,4";.
Need help, to get posts from any of the id, which I've included in my $posts_id variable.
Thanks in Advance!
You need to pass an array to whereIn() method:
$results = Post::whereIn('posts_id', explode(',' $posts_id))->get();
Note that you don't need to call each method in a separate line. You can chain them. Also, the select('*') is not required.
You can create your array in any way you want, no need to use explode().
$ids = [1, 2, 3];
$results = Post::whereIn('posts_id', $ids)->get();
It may be good to check if the array isn't empty before making that query.

PHP MYSQL CASE substitution in array [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 years ago.
Morning all, happy new year!
I am trying to select all records within a MYSQL database into an array where 1 column matches a list, and then replace the output of the cells when selected from that column.
It's a sports table, and I have the Positions as MF, DF, CF and want to replace them with Midfield, Defence, and Forward respectively.
I was hoping that the following would crack it, but get an error message, which lines up to the FROM line:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/peterborough/www/www/wp-content/plugins/insert-php/insert_php.php(48) : eval()’d code on line 14
$result = mysql_query("SELECT *,
CASE Position
WHEN 'DF' THEN 'Defence'
WHEN 'MF' THEN 'Midfield'
WHEN 'CF' THEN 'Forward'
FROM People
WHERE
(Position='DF' or
Position='MF' or
Position='CF') and
Season = '2014'
ORDER BY Number");
while($row = mysql_fetch_assoc($result)){...}
Thanks guys
you have a syntax error in your SQL:
the CASE expression requires an END
SELECT *,
CASE Position
WHEN 'DF' THEN 'Defence'
WHEN 'MF' THEN 'Midfield'
WHEN 'CF' THEN 'Forward'
END AS position_long
FROM People
WHERE
(Position='DF' or
Position='MF' or
Position='CF') and
Season = '2014'
ORDER BY Number
Explanation:
when the syntax error hits, you get a FALSE from the mysql_query() call and
when that's passed into mysql_fetch_assoc() it complains about being given a boolean instead of a resource

Categories