mysql - Alternative method of SQL WHERE in PHP -
dear coders , stackoverflow users;
yesterday i've fixed sql queries joins of stackoverflow users - post found here
while sql query works wonders, haven't been able find replacement queries below;
select pid, pname, pstartdate, penddate, photel, pcity projects pstatus = 'active' select pid, pname, pstartdate, penddate, photel, pcity projects pstatus = 'inactive'
perhaps php alternative work better sql query, although not entirely sure. trying while() , if() statements gives endless loop far issue has not been solved. tried followings;
while( $pstatus == "active" ) { // this. }
and
if( $pstatus == "active" ) { //do this. }elseif( $pstatus == "inactive" ) { //do that. };
i have 2 tables in first 1 shows 'active' projects while on other 1 shows 'inactive' projects. did sql queries above although since implemented new sql select projects, wondering if there method of replacing said 2 queries php equivalent.
perhaps in order clarify i'll add code;
if ($pactive = $mysqli->prepare("select pid, pname, pstartdate, penddate, photel, pcity projects pstatus = 'active'")) { $pactive->execute(); $pactive->store_result(); $pactive->bind_result($apid, $apname, $apsdate, $apedate, $aphotel, $apcity); $pacount = $pactive->num_rows; }
as can see having 2 queries quite inefficient.
does know how in php or perhaps single line of sql query implement on code?
a quick example of think you're trying achieve follows:
$pactive = $mysqli->prepare("select pid, pname, pstartdate, penddate, photel, pcity projects (pstatus = 'active' or pstatus = 'inactive')"); $pactive->execute();
php:
while($row = $pactive->fetch()) { if($row['pstatus'] == 'active') { //do } elseif($row['pstatus'] == 'inactive') { //do } }
you may want consider using switch
such short if/else statement: php switch
Comments
Post a Comment