mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in on line 285 - xampp

$get_list_pro = "select * from products where product_list='$list_id'";
$run_list_pro = mysqli_query($con, $get_list_pro);
$count_lists = mysqli_num_rows($run_list_pro);*

That error occurs when your select query is wrong. The item that you are selecting using select * from products where product_list='$list_id. Does not exists in table product.

Related

With spatie/laravel-tags plugin get related items

Using spatie/laravel-tags plugin (https://docs.spatie.be/laravel-tags/v2/basic-usage/using-tags) in my Laravel 5.7 app.
My Vote model (app/Vote.php) has tags:
<?php
namespace App;
use DB;
use App\MyAppModel;
...
use Spatie\Tags\Tag as SpatieTag;
use Spatie\Tags\HasTags;
class Vote extends MyAppModel
{
use HasTags;
protected $table = 'votes';
protected $primaryKey = 'id';
and I try by found Tag get all related Votes, which has this tag, like:
$activeTag = Tag::containingSlug($tag_slug)->first();
$tagRelatedVotes= Vote::withAnyTags( [$activeTag->slug], 'votesTagType' )->get();
But tagRelatedVotes is empty and looking at sql trace I see next:
SELECT *
FROM `tags`
WHERE LOWER(JSON_EXTRACT(slug, "$.en")) like '"%animals%"' limit 1
SELECT *
FROM `tags`
WHERE `name`->'$."en"' = '{"en": "animals"}' AND `type` = 'votesTagType' limit 1
SELECT *
FROM `votes`
WHERE
EXISTS ( SELECT *
FROM `tags`
INNER JOIN `taggables` on `tags`.`id` = `taggables`.`tag_id`
WHERE `votes`.`id` = `taggables`.`taggable_id` AND `taggables`.`taggable_type` = 'App\Vote' AND `id` in (''))
The 1st statement find the row, but the second statement finds nothing and that is strange why name field is used in request ?
So the 3rd statement is invalid. Which is the valid way?
MODIFIED BLOCK # 2:
Thank you for your feedback!
I tried as you wrote, but I got empty results.
I sql trace I see next:
SELECT *
FROM `tags`
WHERE LOWER(JSON_EXTRACT(slug, "$.en")) like '"%thriller%"' limit 1
SELECT *
FROM `tags`
WHERE `name`->'$."en"' = '{"en": "Thriller"}' AND `type` = 'votesTagType' limit 1
SELECT *
FROM `votes`
WHERE
EXISTS ( SELECT *
FROM `tags`
INNER JOIN `taggables` on `tags`.`id` = `taggables`.`tag_id`
WHERE `votes`.`id` = `taggables`.`taggable_id` AND `taggables`.`taggable_type` = 'App\Vote' AND `id` in (''))
The 1st statement returned 1 row, but the second returned nothing, but I have 1 row with name= 'Thriller' and type = 'votesTagType'.
I am not sure what expression
`name`->'$."en"' = '{"en": "Thriller"}'
means, that is beyond my mysql expierence. CXan it be some mysql or this plugin options?
I have :
SELECT version() : 5.7.23-0ubuntu0.18.04.1
Thanks!
You have to pass the name of the Tag to the withAnyTags() method. docs
$activeTag = Tag::containingSlug($tag_slug)->first();
$tagRelatedVotes= Vote::withAnyTags( [$activeTag], 'votesTagType' )->get();

Invalid identifier when using subset query

I have the following query:
SELECT (SELECT SUM(adults+children) as qty from reservation where id = 11407) as qty,
SUM((price * qty) - (price * nvl(qty_excluded,0)))
FROM reservation_product
WHERE id = 11407
I get the following message: qty invalid identifier.
Your query looks strange. There is some ID in the tables, and you say it's neither their ID, nor the reservation ID. But you have it in both tables, so a reservation can refer to a different as its reservation products do.
And in your query you don't care whether the reservation products you select belong to the reservations you select.
Anyway, your query simply transfered is:
SELECT MAX(r.qty),
SUM((rp.price * r.qty) - (rp.price * nvl(rp.qty_excluded,0)))
FROM reservation_product rp
CROSS JOIN
(
SELECT SUM(adults+children) as qty
FROM reservation
WHERE id = 11407
) r
WHERE id = 11407;

SQL query multiple joins not working

I am using oracle database and trying to run the following query but it gives the error:
"ERROR at line 17: ORA-00904: "FRH"."NS": invalid identifier"
What is the problem with it?
Following is the query:
SELECT *
FROM
(SELECT *
FROM ROOMS R
WHERE R.Prix<'50') FRM
JOIN
(SELECT *
FROM
(SELECT *
FROM HOTELS H
WHERE H.CatH=2) FH
JOIN
(SELECT *
FROM RESORTS R
WHERE TypeS='montagne') FR
ON FH.NS=FR.NS) FRH
ON (FRH.NS=FRM.NS AND FRH.NH=FRM.NH);
Thanks in advance
You have way too many nested selects here. Your query can be simplified to:
SELECT *
FROM rooms rm
JOIN hotels ht ON ht.ns = rm.ns AND ht.nh = rm.nh
JOIN resorts rs ON rs.ns = ht.ns
WHERE rm.prix < 50
AND ht.cath = 2
AND ss.types = 'montagne';
I am not entirely sure which tables need to be joined using just the ns column and which need both the ns and nh column because you have obfuscated your query so much and did not show us the table definitions.
Alternatively you can move the restrictions on the joined tables into the join condition. This isn't necessary for the inner joins you are using, but could be needed if you ever want to change that to an outer join:
SELECT *
FROM rooms rm
JOIN hotels ht ON ht.ns = rm.ns AND ht.nh = rm.nh AND ht.cath = 2
JOIN resorts rs ON rs.ns = ht.ns AND rs.types = 'montagne'
WHERE rm.prix < 50;
You should also not compare numbers and strings. Assuming rooms.prix is a number column, the condition R.Prix<'50' is wrong. You need to compare the number to a number r.prix < 50

Syntax error on date sort

I am using Ultralite 12. I have a table with a date field called "Date." (I didn't name it.) I want to sort my results by this field.
The query:
SELECT * FROM ItemHistory
where itemid = 'BC-2000-005' and customerid = '227B05'
works and returns the results
HistoryType,ItemID,UM_ID,CustomerID,Date,OrderHeaderID
1,'BC-2000-005',1,'227B05',2014-11-24,'849446-1'
1,'BC-2000-005',1,'227B05',2014-12-17,'852747-1'
1,'BC-2000-005',1,'227B05',2015-01-02,'854701-1'
1,'BC-2000-005',1,'227B05',2015-02-09,'859811-1'
I want to return the top answer when the results are sorted by date (in other words, the last one).
SELECT top 1 * FROM ItemHistory
where itemid = 'BC-2000-005' and customerid = '227B05'
order by date DESC
gives me a syntax error by DESC. I've tried this as well:
SELECT top 1 * FROM ItemHistory
where itemid = 'BC-2000-005' and customerid = '227B05'
order by [date] DESC
Try with "date". You could find more info from http://dev.cs.uni-magdeburg.de/db/sybase9/help/dbrfen9/00000010.htm

SQLite3 Match on VIEW?

So I am attaching multiple databases, then creating a TEMP VIEW which combines all the virtualTables from the various databases as such.
theDatabase.execSQL("CREATE TEMP VIEW virtualView AS SELECT * FROM Virtual_Sites UNION SELECT * FROM db1.Virtual_Sites UNION SELECT * FROM db2.Virtual_Sites");
Is it possible to use the MATCH query on this VIEW?
theDatabase.rawQuery(SELECT * FROM virtualView WHERE all_text MATCH 21033, null)
I am currently getting this error.
sqlite returned: error code = 1, msg = statement aborts at 46: [SELECT * FROM virtualView WHERE all_text MATCH 21033] unable to use function MATCH in the requested context, db=xxx
exception: SQL logic error or missing database; query: SELECT * FROM virtualView WHERE all_text MATCH 21033
I have read that creating VIEW's in this way will not inherit the underlying tables indexes, is this why I am unable to do a MATCH? If so any work arounds?
Thanks
MATCH works only directly on virtual tables that implement this operator, not on views.
You will have to rewrite all your queries to use MATCH on the individual tables, and to combine those results with UNION ALL.
Alternatively, copy the data of all your tables into one single temporary table.
This is what I found. VIEWS are in-efficient when combining multiple databases/tables as they loose their underlining indexes once they contain more than 2 tables.
ATTACH
String newDbPath = DB_PATH + DB_NAME;
theDatabase.execSQL("ATTACH DATABASE '" + newDbPath + "' as db1;");
The in-efficient way
Create VIEW
theDatabase.execSQL("CREATE TEMP VIEW view1 AS SELECT * FROM Sites_hsf UNION SELECT * FROM db1.Sites_hsf UNION SELECT * FROM db2.Sites_hsf;");
Query VIEW
sqlStatement = "SELECT * FROM view1 s INNER JOIN view2 con ON s.site_uid = con.site_uid AND con.ap_active = 1 WHERE s.site_uid = 1114331";
Execution Time
16281 Milliseconds
The efficient way
sqlStatement = "SELECT * FROM Sites_hsf s INNER JOIN Connections_hsf con ON s.site_uid = con.site_uid AND con.ap_active = 1 WHERE s.site_uid = 1114331 " +
"UNION ALL " +
"SELECT * FROM db1.Sites_hsf s1 INNER JOIN db1.Connections_hsf con1 ON s1.site_uid = con1.site_uid AND con1.ap_active = 1 WHERE s1.site_uid = 1114331 " +
"UNION ALL " +
"SELECT * FROM db2.Sites_hsf s2 INNER JOIN db2.Connections_hsf con2 ON s2.site_uid = con2.site_uid AND con2.ap_active = 1 WHERE s2.site_uid = 1114331";
Execution Time
4 Milliseconds
MATCH using CL.'s answer of UNION ALL
sqlStatement = "SELECT * FROM Virtual_Sites_hsf WHERE all_text MATCH '40227' " +
"UNION ALL SELECT * FROM db1.Virtual_Sites_hsf WHERE all_text MATCH '40227'" +
"UNION ALL SELECT * FROM db2.Virtual_Sites_hsf WHERE all_text MATCH '40227'"
Hopefully this helps someone.

Resources