php - Sending "null" through a variable on mysql won't delete the row -


i have code/query:

if ($a == $b) {     $type = 1; } else {     $type = null; }  $this->query(     'delete users userid = ? , type = ?',     array($userid, $type) ); 

buy mysql doesn't understand second case , won't delete row if $type null.

i know proper way define null in query is null, in case, won't fit.

can somehow pass null through variable?

it's possible make where clause aware switch between value , is null requires additional conditions , additional placeholders. in case, since query simple, build dynamically , append parameters array() execution accordingly.

// basic query, conditions common either case: $sql = 'delete users userid = ? , ';  // bound parameters... // $userid present $params = array($userid); if ($a == $b) {   // add additional parameter type   $params[] = 1;   // , add clause type condition   $sql .= ' type = ?'; } else   // or null  type condition   $sql .= 'type null'; } // execute $params array, has 1 or 2 elements. $this->query($sql, $params); 

in order stuff 1 query, where clause have detect type variable non-null or condition. 1 way (not way, , maybe not nicest way) looks this:

delete users   userid = ?   , ((? = 1 , type = ?) or type null) 

in case, value of $type passed in twice, first make condition 1=1 true, compare type column. can see, when using placeholders, becomes sort of confusing. easier build string dynamically in first place.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -