Skip to content
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

Closed
narasimha-kvl opened this issue May 25, 2016 · 5 comments
Closed

how to write a delete query #8

narasimha-kvl opened this issue May 25, 2016 · 5 comments

Comments

@narasimha-kvl
Copy link

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

@noncent
Copy link
Owner

noncent commented May 25, 2016

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();

@narasimha-kvl
Copy link
Author

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
Narasimha

@noncent
Copy link
Owner

noncent commented May 26, 2016

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
Neeraj Singh

@noncent noncent closed this as completed May 26, 2016
@futuretechmag
Copy link

futuretechmag commented Mar 4, 2017

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?

@noncent
Copy link
Owner

noncent commented Mar 5, 2017

Hi @futuretechmag,
The pdoQuery works only for single query at a time, its doesn't support multi query yet, but definitely it's a good idea and we will make this in future release. Well, delete, update and select has where clause functionality, you can find more example in doc file. Yes, you can use where as multi param like multiple where clause or multiple where with OR clause.

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants