I am new in php and I don't know how to execute SQL query for where clause with php parameter from url please help me.
Url: http://myurl.com/eng.php?name=fab
PHP Code
<?php
header('Content-Type: application/json; charset=utf-8');
$mysqli = new mysqli ( 'localhost', 'mabhim92', '9993115300', 'gcm_chat');
//PROBLEM LANGUAGE ?????
if( function_exists('mysql_set_charset') ){
mysqli_set_charset($mysqli , 'utf8');
}else{
mysqli_query($mysqli , "SET NAMES 'utf8'");
}
// Check if album id is posted as GET parameter
$id = intval($_GET['name']);
$myq = $mysqli ->query ("SELECT * FROM gkfordate WHERE monthforgk=$id");
while ($myr = $myq->fetch_assoc()) {
$array["Questions"][] = (array(
'month' => $myr['monthforgk'],
'date' => $myr['dateforgk'],
));
}
echo json_encode($array, JSON_UNESCAPED_UNICODE);
?>
Result: In my database table only two columns monthforgk and dateforgk
{
"Questions": [
{
"month": "jan",
"date": "2016-04-13 20:30:49"
},
{
"month": "jan",
"date": "2016-04-13 20:30:49"
},
{
"month": "jan",
"date": "2016-04-13 20:30:49"
},
{
"month": "fab",
"date": "2016-04-13 20:30:49"
},
{
"month": "fab",
"date": "2016-04-13 20:30:49"
}
]
}
If I pass parameter for name=fab so why give me all result from database.
monthforgkvalues are strings -jan/fab, etc, then why are you doing$id = intval($_GET['name']);? You are basically doing... WHERE monthforgk=1$id = intval($_GET['name']);you would do$id = $mysqli->real_escape_string($_GET['name']);(to sanitize the$_GET). And then quote the value in your query ->... WHERE monthforgk= '$id' ", as the value is a string, not an int.