php - slim framework no output displayed -
i'm trying json file using slim framework. code i'm trying mentioned below
$app->get('/forum/:id', function ($id) { $user_name = "abc"; $password = "123"; $database = "test"; $server = "localhost"; $db_handle = mysqli_connect($server, $user_name, $password); mysqli_set_charset($db_handle, "utf8"); mysqli_select_db($db_handle, $database); $arr = array(); $sql = "select y123_forum.post_id, y123_forum.posttext y123_forum inner join y123_users on y123_forum.user_id = y123_users.id type = 1 , y123_users.email = 'id'"; $result = mysqli_query($db_handle, $sql); while ($row = mysqli_fetch_assoc($result)) { array_push($arr, $row); } mysqli_close($db_handle); echo json_encode($arr); });
the output displayed on browser []
when try above code without passing parameter, i.e
$app->get('/faqs/', function () { $user_name = "abc"; $password = "123"; $database = "test"; $server = "localhost"; $db_handle = mysqli_connect($server, $user_name, $password); mysqli_set_charset($db_handle, "utf8"); mysqli_select_db($db_handle, $database); $arr = array(); $sql = select y123_forum.post_id, y123_forum.posttext y123_forum inner join y123_users on y123_forum.user_id = y123_users.id type = 1 , y123_users.email = 'abc@gmail.com'" $result = mysqli_query($db_handle, $sql); while ($row = mysqli_fetch_assoc($result)) { array_push($arr, $row); } mysqli_close($db_handle); echo json_encode($arr); });
then works fine
how fix this, need working json file passing email id's database
you're forgetting $ in parameter, thinks you're looking email address of 'id', not contents of $id.
select * y123_forum email = '$id';
note horrible, bad, unsafe way pass parameters sql query. correct way parameterize query , execute way:
$sql = 'select * y123_forum email = ?'; $stmt = mysqli_stmt_init($db_handle); if (mysqli_stmt_prepare($stmt, $sql)) { mysqli_stmt_bind_param($stmt, 's', $id); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); while ($row = mysqli_fetch_assoc($result)) { array_push($arr, $row); } }
the 's' in mysql_stmt_bind_param
tells driver $id variable should treated string, , escapes appropriately.