I am trying to delete category and products belong to it in products_TABLE using pdo INNER JOIN.
it works if I delete only category without deleting products.
Here is my code :
$catid = filterString($_GET['cat_id']);
$stmt = $pdo->prepare('DELETE FROM categories AS c
INNER JOIN products AS p ON c.cat_id = p.catid
WHERE cat_id = :cat_id
');
$delete = $stmt->execute(array('cat_id' =>$catid));
This is the error I am having :
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter
number: parameter was not defined in
D:\wamp\www\p\employees\DelStore.php:25 Stack trace: #0
D:\wamp\www\p\employees\DelStore.php(25): PDOStatement->execute(Array)
1 {main} thrown in D:\wamp\www\p\employees\DelStore.php on line 2
I understand it says given parameters are invalid, But couldnt solve how to give parameters in:
$delete = $stmt->execute(array('cat_id' =>$catid));
Thanks for any advice
The error you are getting is because you have :products.catid
Change that to products.catid
Also if you want to delete the entries from both tables you shoul use aliases.
DELETE c,p FROM categories c
INNER JOIN products p ON c.cat_id = p.catid
WHERE cat_id = :cat_id
Also you need to change
$delete = $stmt->execute(array('cat_id' =>$catid));
to
$delete = $stmt->execute(array(':cat_id' =>$catid));
The above example doesn't work if you are using a SQL Server. In that case you should be using 2 seperate delete queries.
Also when you bind parameters in the execute function as an array thay are binded as stings. This might also cause some problems depending on your database structure. Using $stmt->bindParam() is usually a better option.
Related
I need to run a custom query and I have tried all these methods
$sql = "SELECT acd.*, DATE_FORMAT(acd.cdate, '%d/%m/%Y') cdate_disp,
GROUP_CONCAT(CONCAT_WS(', ', car.type, car.session, crd.name) SEPARATOR '<br />') rd_names,
acb.booking_status my_booking_status
FROM app_data acd
INNER JOIN crmaccounts ca ON ca.id = acd.client_crm_id
LEFT JOIN crmaccount_rdids car ON car.account_id = ca.id
LEFT JOIN crmrd_ids crd ON crd.id = car.rd_id
LEFT JOIN app_bookings acb ON acb.call_ref_id = acd.call_ref AND acb.user_id = 12391
WHERE 1=1
AND acd.client_crm_id NOT IN (select account_id from bstaff WHERE user_id=12391)
GROUP BY acd.id
ORDER BY acd.cdate, ctiming";
DB:select($sql);
throws
Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY (SQL: ....)
DB::select($sql);
DB::raw($sql);
DB::select(DB::raw($sql));
//even with pdo
$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(\PDO::FETCH_OBJ);
PDOException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY
It seems like the only way to get working is to list all the table columns in the group by which is doable but not convenient.
I am able to run the same query directly in the phpmyadmin. So I am not sure why when I run it through Laravel it asks me add all columns.
I have MariaDB installed and both Laravel and PhpMyAdmin are connecting to the same instance. Larvel version is 5.8.5.
Please take a look at full query as I asked question here too but couldn't find any answer - https://laracasts.com/discuss/channels/eloquent/query-runs-ok-in-phpmyadmin-but-dbselect-throws-exception
You should try disabling the strict mode in config/database.php, in mysql connection section.
This is not a Laravel issue, but a logical limitation by MySQL. When you add columns to your results set that are not in the GROUP BY clause, different values could be possible for these columns in the result set.
When executing queries in strict mode (which is on by default), MySQL will not allow this because it could lead to unexpected results. If you turn off strict mode MySQL will use the first result it finds for these columns.
You can turn off strict mode in your Laravel config files in config/database.php. However, it is recommended to change your query instead because of the unpredictability.
A full list of checks in strict mode can be found in the MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict
I have installed Laravel 4.8 and also configured Auth. further more,I add two tables called as role and states and tried your pdo code - its perfectly works for me! Here, I put my code.
$sql = "SELECT *
FROM users u
INNER JOIN roles r ON r.id = u.role_id
LEFT JOIN states s ON s.id = u.state_id
WHERE 1=1
GROUP BY u.id
ORDER BY u.id ";
$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(\PDO::FETCH_OBJ);
dd($data);
I am eager to know what is your db structure.
I have these two tables in my database:
Statements
and Tags
I'm trying to execute this query
SELECT s.id, s.name, s.preview, s.approved FROM Statements s JOIN Tags t ON s.id = t.probID WHERE s.approved = TRUE AND t.tag IN ("Geometry") GROUP BY s.id, s.name, HAVING COUNT(DISTINCT t.tag) = 1,
but getting "Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\file.php on line 46", where 46th line is while($row = $res->fetch_assoc()){
Is something wrong with my query?
I suppose you are using MySQLi, correct me if I am wrong.
In the span of time I used it, whenever this kind of problem was presented to me, I had a problem with the query which I was not able to identify before the execution. Given that my query had failed, no SQLStatement object (or whatever its name is) was created. Therefore, no fetch_* method could be called on that!
I believe your problem is a very simple one: you have a trailing comma before your HAVING clause which is messing your query up. I reproduced your SQL structure and query on my PC, and everything worked correctly.
SELECT
s.id, s.name, s.preview, s.approved
FROM
Statements s JOIN Tags t ON s.id = t.probID
WHERE
s.approved = TRUE AND t.tag IN ('Geometry')
GROUP BY s.id, s.name
HAVING COUNT(DISTINCT t.tag) = 1
Try this one out.
Just a note: whenever I have to deal with a true or false in SQL and I am storing it as a *INT UNSIGNED, I feel like using 1 as true and 0 as false never creates any problem, while using TRUE or FALSE sometimes does! This was not the case, anyway :)
If you use var_dump($result); don't use it. Instead of that, put the result in the $result variable. $result = $stmt->get_result();
Haven't been able to find a solid solution for this, but I have a mySQL query that I want to translate to Doctrine. It is a select from a subquery with joins and I might have read somewhere that joins are not allowed in subqueries in Doctrine.
Here is the SQL:
SELECT part, SUM(qty) as qty FROM (SELECT part, SUM(qty) as qty FROM sub LEFT JOIN main ON main.id = main_id WHERE hold != 1 GROUP BY name, part) AS tbl GROUP BY part
This is what I tried and it is all wrong.
$em = $this->getDoctrine()->getManager();
$q = $em->createQuery('v');
$q2 = $em->createSubQuery()
->select('m.part, sum(s.qty) qty')
->from('Sub s')
->leftJoin('s.main m')
->where('s.hold != 1')
->groupBy('m.part');
$q->select('m.part, sum(qty)', $q2->getDQL());
One of the first error I got was:
FatalErrorException: Error: Call to undefined method Doctrine\ORM\EntityManager::createSubQuery() in ....Controller.php line 238
I am pretty sure it isn't just this that I'm doing wrong but it is the first thing that's coming up. So getManager() apparently doesn't have a createSubQuery() function? What is the right way to do this?
There's no method like createSubQuery(). Take a look at that answer: https://stackoverflow.com/a/10763358
New to Doctrine, and trying to get a JOIN to work.
I've banged my head on this for awhile, and am slowly beginning to get the whole picture - its not as simply as I thought it would be.
I have two Entities with corresponding sql tables. I'm attempting to run a query that will return data from both of them, linked. A JOIN.
If I was doing this in straight SQL, it would look like this:
SELECT *
FROM mail_links a
INNER JOIN mail_msgs b
ON a.msg_id = b.id
I've tried a few things to make this work, including:
$query = $this -> doctrine -> em -> createQuery ("SELECT a FROM ORM\Dynasties2\Maillinks a JOIN ORM\Dynasties2\Mailmsgs b ON a.msgId = b.id");
That of course did not work. I realized that this relies on having Entities created as Classes, or at least that's what some examples seemed to be doing.
And that's where I'm completely lost.
Is it correct that I need those entities to be Classes? Are there any complete code examples of this (using CI) that would help me understand how to write the classes necessary for this JOIN?
Edit:
It just dawned on me that I DO have classes setup. That's what my entities ARE.
So how do I make this work:
$query = $this -> doctrine -> em -> createQuery ("SELECT a FROM ORM\Dynasties2\Maillinks a JOIN ORM\Dynasties2\Mailmsgs b ON a.msgId = b.id");
It gives the following error:
Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message '[Semantical Error] line 0, col 70 near 'b ON a.msgId': Error: Identification Variable ORM\Dynasties2\Mailmsgs used in join path expression but was not defined before.' in /PATH/applicationFolder/libraries/Doctrine/ORM/Query/QueryException.php:47 Stack trace: #0 /PATH/applicationFolder/libraries/Doctrine/ORM/Query/Parser.php(413): Doctrine\ORM\Query\QueryException::semanticalError('line 0, col 70 ...') #1 /PATH/applicationFolder/libraries/Doctrine/ORM/Query/Parser.php(914): Doctrine\ORM\Query\Parser->semanticalError('Identification ...') #2 /PATH/applicationFolder/libraries/Doctrine/ORM/Query/Parser.php(1567): Doctrine\ORM\Query\Parser->JoinAssociationPathExpression() #3 /PATH/ in /PATH/applicationFolder/libraries/Doctrine/ORM/Query/QueryException.php on line 47
I have the following query:
$latestcontent = $em->createQuery('
SELECT c.title, c.content, c.lastedit, a.firstname, a.surname
FROM ShoutMainBundle:Content c, ShoutMainBundle:Admin a
WHERE c.author = a.id
ORDER BY c.lastedit ASC'
);
What I need to do, is limit the amount of records returned from this query. However, when I add LIMIT 10 to the SQL query, it returns this error:
Error: Expected end of string, got 'LIMIT'.
So, I had a look and found that you could do add ->limit(10) to the code (after the query). But this then throws up this PHP error:
Fatal error: Call to undefined method Doctrine\ORM\Query::limit() in C:\wamp\www\src\Shout\AdminBundle\Controller\DefaultController.php on line 22
What am I doing wrong?
There is no statement like LIMIT for DQL currently, as far as I know.
You have to use Query::setMaxResults().