-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to write a delete query #8
Comments
Hi Narasimha, You can use below example to DELETE with IN. /**
* run simple delete query with 'IN' operator
*
* showQuery = display executed query
* affectedRows = get no of affected rows
*/
// here is id's which gonna delete
$arrayCustomerNumber = array(103, 112, 114, 119, 121);
// here is delete query with ? bind params
$deleteQuery = 'DELETE FROM `customers` WHERE customerNumber IN (?, ?, ?, ?, ?)';
// execute delete query and pass id's array
$exec = $db->pdoQuery($deleteQuery, $arrayCustomerNumber)->showQuery()->affectedRows();
// print affected rows by delete operation
$helper->PA($exec);
/**
* Here is raw query which will be generate
*
* Executed Query -> delete from `customers` where customernumber in (103, 112, 114, 119, 121)
*
* No of affected rows : 5
*/
// here is your query
// execute delete query and pass id's array
$exec = $db->pdoQuery('DELETE FROM students WHERE studentID IN (?, ?, ?, ?, ?)', array(1, 2, 3, 4, 5))->showQuery()->affectedRows(); |
Hi Thank you for the quick reply. This is fine if there are fixed nnumber of id's, bu iif the id's vary from 1 to N, is there is any other way to execute Thank you once again. Regards |
Hi Narasimha, The pdoQuery accepts second param as array, if the array is associative then PDO Class Wrapper use namespace bindparam (:param, value) and if numeric then its work bindparam with '?' (1, value). So, you are free to use 'N' number of values within 2nd param array but ensure that you have to have same no of '?' too. // array sample
$N_No_Of_Array_IDs = range(1,45);
// define vars
$idsCollection = $placeholder = array();
// parse each ids and create ids array and placeholder
foreach ($N_No_Of_Array_IDs as $key => $ids) {
$idsCollection[] = $ids;
$placeholder[] = '?';
}
$affected_rows = $db->pdoQuery("DELETE FROM students WHERE studentID IN (".implode(',',$placeholder).")", $idsCollection)->affectedRows(); -Best |
I don't mean to re-open this, but since the question pertains to IN (WHERE) clause, and seeing the example in this is the same... is pdoQuery basically meant for running multi-row (custom/complex) queries? The "delete", "update" and "select" functions are meant for single WHERE clause queries? |
Hi @futuretechmag, Example: Custom Where Clause with Select Method:### You can set your own custom where clause
$whereConditions = array('lastname ='=>'bow', 'or jobtitle ='=> 'Sales Rep', 'and isactive ='=>1, 'and officecode ='=> 1 );
$data = $db->select('employees','',$whereConditions)->results();
Raw Query:
SELECT * FROM `employees` WHERE lastname = "bow" OR jobtitle = "sales rep" AND isactive = 1 AND officecode = 1 ;
OR
$whereConditions = array('lastname ='=>'bow', 'or jobtitle ='=> 'Sales Rep', 'and isactive ='=>1, 'and officecode ='=> 1 );
$data = $db->select('employees',array('employeenumber','lastname','jobtitle'),$whereConditions)->results();
Raw Query:
SELECT employeenumber, lastname, jobtitle FROM 'employees' WHERE lastname = "bow" OR jobtitle = "sales rep" AND isactive = 1 AND officecode = 1 ; Hope, you got me :), Please feel free to ask questions, we would like to take care all of them. Cheers!! |
Hi Admin
I have a delete query not able to execute using the class.
Here is my query, please let me know how to execute
DELETE FROM students WHERE studentID IN (1,2,3,4,5)
Regards
Narasimha
The text was updated successfully, but these errors were encountered: