public static function get($response, $token, $string, $flag) { if (!TokensDB::check($token)) { return putError('invalid token', Users::ERROR_AUTH_INVALID, $response); } $search = SearchDB::get($token[Users::ID_KEY], $string, $flag); if ($search === FALSE) { return putError('database connection error', DATABASE::ERROR_DATABASE_CONN, $response); } return putJsonBody(array('error' => false, 'results' => $search), 200, $response); }
public static function get($user_id, $string, $flag) { if (!($mysqli = SearchDB::connect())) { return false; } if ($flag == "mobile") { $string = $string . '%'; $query_sql = "SELECT\n concat(user_fname, ' ', user_lname), \n users_info.user_id,\n picture_path,\n user_mobile\n FROM\n users_info\n INNER JOIN\n friendships\n ON\n friendships.user_id = ? \n LEFT JOIN \n pictures\n ON \n pictures.picture_id = user_picture \n WHERE \n friendships.friendship_type = 'accepted'\n AND\n user_mobile\n LIKE \n ?"; $query = $mysqli->prepare($query_sql); $query->bind_param("ss", $string, $user_id); $query->bind_result($user_name, $user_id, $user_thumbnail, $user_mobile); $query->execute(); $search = array(); while ($query->fetch()) { $search[] = array('type' => $flag, 'mobile' => $mobile, Posts::USER_NAME_KEY => $user_name, Posts::USER_ID_KEY => $user_id, Posts::USER_THUMBNAIL_KEY => $user_thumbnail); } $query->close(); $mysqli->close(); return $search; } else { if ($flag == "email") { $string = $string . '%'; $query_sql = " SELECT\n concat(user_fname, ' ', user_lname), \n users.user_id,\n picture_path,\n user_email\n FROM\n users_info\n INNER JOIN \n users\n ON\n users_info.user_id = users.user_id\n LEFT JOIN \n pictures\n ON \n pictures.picture_id = user_picture \n WHERE \n user_email\n LIKE \n ?"; $query = $mysqli->prepare($query_sql); $query->bind_param("s", $string); $query->bind_result($user_name, $user_id, $user_thumbnail, $user_email); $query->execute(); $search = array(); while ($query->fetch()) { $search[] = array('type' => $flag, 'email' => $user_email, Posts::USER_NAME_KEY => $user_name, Posts::USER_ID_KEY => $user_id, Posts::USER_THUMBNAIL_KEY => $user_thumbnail); } $query->close(); $mysqli->close(); return $search; } else { if ($flag == "fullname") { $string = '%' . $string . '%'; $query_sql = " SELECT\n concat(user_fname, ' ', user_lname), \n user_id,\n picture_path\n FROM\n users_info\n LEFT JOIN \n pictures\n ON \n pictures.picture_id = user_picture \n WHERE \n concat(user_fname, ' ', user_lname)\n LIKE \n ?"; $query = $mysqli->prepare($query_sql); $query->bind_param("s", $string); $query->bind_result($user_name, $user_id, $user_thumbnail); $query->execute(); $search = array(); while ($query->fetch()) { $search[] = array('type' => $flag, Posts::USER_NAME_KEY => $user_name, Posts::USER_ID_KEY => $user_id, Posts::USER_THUMBNAIL_KEY => $user_thumbnail); } $query->close(); $mysqli->close(); return $search; } else { if ($flag == "hometown") { $string = $string . '%'; $query_sql = " SELECT\n concat(user_fname, ' ', user_lname), \n user_id\n FROM\n users_info\n WHERE \n user_hometown\n LIKE \n ?"; $query = $mysqli->prepare($query_sql); $query->bind_param("s", $string); $query->bind_result($user_name, $user_id); $query->execute(); $search = array(); while ($query->fetch()) { $search[] = array('type' => $flag, Posts::USER_NAME_KEY => $user_name, Posts::USER_ID_KEY => $user_id, Posts::USER_THUMBNAIL_KEY => $user_thumbnail); } $query->close(); $mysqli->close(); return $search; } else { if ($flag == "post") { $string = '%' . $string . '%'; $query_sql = " SELECT \n posts.post_id, \n posts.user_id,\n concat(user_fname, ' ', user_lname), \n up.picture_path,\n post_privacy, \n post_timestamp, \n post_text, \n pp.picture_path \n FROM \n posts \n LEFT JOIN \n pictures as pp\n ON \n pp.picture_id = post_picture \n LEFT JOIN\n users_info\n ON\n posts.user_id = users_info.user_id \n LEFT JOIN \n pictures as up\n ON \n up.picture_id = users_info.user_thumbnail \n\n WHERE\n posts.post_id\n IN \n ( \n SELECT \n post_id\n FROM\n posts\n WHERE\n post_privacy = 1\n OR\n user_id = ?\n \n UNION \n \n SELECT \n post_id\n FROM\n friendships\n INNER JOIN\n posts\n ON\n friendships.friend_id = posts.user_id \n AND\n friendships.friendship_type = 'accepted'\n AND \n friendships.user_id = ? \n )\n AND \n post_text \n LIKE\n ?\n ORDER BY\n post_timestamp DESC"; $query = $mysqli->prepare($query_sql); $query->bind_param("sss", $user_id, $user_id, $string); $query->bind_result($post_id, $post_user_id, $post_user_name, $post_user_thumbnail, $post_privacy, $post_timestamp, $post_text, $post_picture); $query->execute(); $search = array(); while ($query->fetch()) { $search[] = array('type' => $flag, Posts::USER_NAME_KEY => $post_user_name, Posts::USER_ID_KEY => $post_user_id, Posts::TEXT_KEY => $post_text, Posts::ID_KEY => $post_id, Posts::USER_THUMBNAIL_KEY => $post_user_thumbnail); } $query->close(); $mysqli->close(); return $search; } } } } } return false; }