简体   繁体   中英

How to get parameter from php url and use for query result

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.

您尝试查询

"SELECT * FROM  gkfordate WHERE monthforgk='$id'";

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'");   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM