Reputation: 148
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.
Upvotes: 2
Views: 2792
Reputation: 84
you try with query
"SELECT * FROM gkfordate WHERE monthforgk='$id'";
Upvotes: 4
Reputation: 3813
You are running the query string through the intval() function, which returns the integer value of the string 'fab' which is 0.
Try this:
echo 'intval of fab = '.intval('fab');
What you need is simply this:
if(isset($_GET['name'])){
$id = $mysqli->real_escape_string($_GET['name']);
}
And then in the query:
$myq = $mysqli ->query ("SELECT * FROM gkfordate WHERE monthforgk='$id'");
Upvotes: 2