mysqli subquery string didn't works [duplicate] - php

This question already has answers here:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given [duplicate]
(2 answers)
Closed 8 years ago.
I have a mysql query which doesn't work. It gives me the following error:
mysqli_query() expects parameter 1 to be mysqli, null given
my sql query is:
$select = mysqli_query($sql, "SELECT title FROM category WHERE id LIKE (SELECT categorie_id FROM categories_sub WHERE file LIKE '".$site."')")

The query will not work, what your query is doing
WHERE id LIKE (SELECT categorie_id FROM categories_sub WHERE file = 'some val')
This is similar as
WHERE id = {multiple categorie_id}
when the subquery has more than one categorie_id and this will return error.
So replace
category WHERE id LIKE
to
category WHERE id IN ( ...

This error tends to mean that you didn't connect correctly to the database. Double check your connection. Also, file LIKE 'x' is equivalent to file='x'. To search for a string containing 'x', use file LIKE '%x%'.
Edit: The other answers are right that you need to use IN instead of LIKE for the first one (i.e. a member of the set defined on another query)

Related

The query is using a variable but i dont know why, can you help me? [duplicate]

This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
What does this code do? Image to show code
$q57 = mysqli_query($Link, 'select count(*) Registos from t57 where c18="1"') ;
$t57 = mysqli_fetch_array($q57) ;
if($t57['Registos'] == 0) {
what i don't understand is that variable "Registos" in front of the count. Is it doing what there?
That is simply an alias, to help you access the result of count(*).
Otherwise that would become the column name in the result set - remove Registos from the query, and then do a var_dump($t57);, you’ll see what I mean.
The as keyword between the two is optional; select count(*) as Registos would be the “long” way of writing this.
https://www.mysqltutorial.org/mysql-alias/

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 One Normal Table and Counts From Another Table In One SQL Query (JOIN)

I need a SQL query which gives content of one table and count of second table. I tried, could not.
I have a "comments" table. And in the another table named "likings", there are numbers of likes of comments.
In my algorithm, in the "likings" table, there are a column named "likeType", if it is "1" that means a like, otherwise (if it is "0") means a dislike. I need to bring comment from "comments" table, counts of likeType=1 and likeType=0 from "likings" table in one query.
Here is my best attempt for that, which didn't work:
PHP side:
$getFirst8Comments = "SELECT
episodecomments.cmtID,
episodecomments.cmtConEpisode,
episodecomments.cmtOwner,
episodecomments.cmtDate,
episodecomments.cmtContent,
episodecomments.cmtSpoiler,
SUM(IF(episodecommentsliking.liType='1', 1, 0)) AS likes,
SUM(IF(episodecommentsliking.liType='0', 1, 0)) AS dislikes
FROM episodecomments
LEFT JOIN episodecommentsliking
ON episodecomments.cmtID = episodecommentsliking.liCmtID
GROUP BY
episodecomments.cmtID,
episodecomments.cmtConEpisode,
episodecomments.cmtOwner,
episodecomments.cmtDate,
episodecomments.cmtContent,
episodecomments.cmtSpoiler
WHERE episodecomments.cmtConEpisode='$epID'
ORDER BY episodecomments.cmtID
DESC LIMIT 8";
while ($getF8C = mysqli_fetch_array($getFirst8Comments))
{
echo "Something coming through-<br>";
}
Error pops out: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in J:\file.php on line 3484 (error in variable $getFirst8Comments)
Thanks in advance.
MySQL doesn't support COUNT in that fashion. You'll also need to inform it what to GROUP BY for those aggregate (sum) functions.
Try using SUM instead: (COUNT would work if you're counting NULL vs non-NULL values however - see the MySQL documentation)
SELECT
comments.cmtID,
comments.cmtConEpisode,
comments.cmtOwner,
comments.cmtDate,
comments.cmtContent,
comments.cmtSpoiler,
SUM(IF(commentsliking.liType='1', 1, 0)) AS likes,
SUM(IF(commentsliking.liType='0', 1, 0)) AS dislikes
FROM comments
INNER JOIN commentsliking
ON comments.cmtID = commentsliking.liCmtID
GROUP BY
comments.cmtID,
comments.cmtConEpisode,
comments.cmtOwner,
comments.cmtDate,
comments.cmtContent,
comments.cmtSpoiler
ORDER BY comments.cmtID
DESC LIMIT 8
Also - use "LEFT JOIN" if there's the possibility that a comment record will exist with no like data.
I've further updated my answer to address the error you've since reported being thrown by the mysqli_* commands.
Check the PHP documentation for full details on how to correctly use the mysqli_fetch_array() function - especially with regards to it requiring a mysqli_result type as an argument, as opposed to a string [of SQL])

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