php - If/else for INSERT query -
i running situation cannot figure out best way run if/else query. first, not sure if can run if/else sql statement. below have. through process, added if/else around query.
basically want run first query if equals $user_id
otherwise, run else, want make value of user_id
(database column - not variable listed above) equal $profile_viewer
.
so, in nutshell, if can this, not sure how modify execute:
to include $profile_viewer
parameter else query.
$select_img_stmt->execute(array($user_id));
$user_id = ( isset( $_session['user'] ) ? $_session['user'] : "" ); $profile_viewer = $_get['user']; if ($profile_viewer == $user_id) { $img_select_sql = " select * profile_img user_id = ? order id desc limit 1 "; } else { //echo "this not image"; $img_select_sql = " select * profile_img user_id = ? order id desc limit 1 "; } if ($select_img_stmt = $con->prepare($img_select_sql)) { $select_img_stmt->execute(array($user_id));
updated code:
$user_id = ( isset( $_session['user'] ) ? $_session['user'] : "" ); $profile_viewer = $_get['user']; if ($profile_viewer == $user_id) { /*$img_select_sql = " select * profile_img user_id = ? order id desc limit 1 ";*/ $img_select_sql = " select i.* profile_img user_id in (?, ?) order id desc limit 1; "; } else { //echo "this not image"; echo $profile_viewer; } if ($select_img_stmt = $con->prepare($img_select_sql)) { $select_img_stmt->execute(array($user_id, $profile_viewer));
how doing in 1 query?
select i.* profile_img user_id in ($user_id, $profile_viewer) order (user_id = $user_id) desc limit 1;
this has put variable values directly in code clarity. of course, recommend using parameters rather munging query string values.
Comments
Post a Comment