使用来自URL的$ _GET ['id']在php中显示mysql数据

时间:2013-12-13 10:52:02

标签: php mysql get

我从URL获取'id',但我似乎无法用它来从同一行中选择其他数据。我正在尝试使用Film_ID显示Film_Title(MySQL中的行名称)。在这种情况下,Film_ID来自URL,如下所示:/film.php?id = 58。

如何使用该ID选择Film_Title?这是我的代码:

if (isset($_GET['id'])) {
    $id = trim($_GET['id']); 
    $Film_Title = trim($_POST['Film_Title']);

    $sql = "SELECT Film_Title FROM Films WHERE Film_ID=?";

    require_once 'includes/MySQL.php';  
    require_once 'includes/db.php';

    $db = new MySQL($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['database']);
    $stm = $db->dbConn->prepare($sql);
    $stm->execute(array($id, $Film_Title));
    echo "<h3>film record $Film_Title has been selected</h3>";

}

2 个答案:

答案 0 :(得分:0)

你只放一个?在您的查询中,但execute()数组中有两个元素。我更喜欢将查询放在我正在使用它之前,或者只是在准备之内,因为它更接近执行,因此更不容易出错。此外,您没有提取数据。

if (isset($_GET['id'])) {
  $id = trim($_GET['id']); 
  $Film_Title = trim($_POST['Film_Title']);

  require_once 'includes/MySQL.php';    
  require_once 'includes/db.php';

  $db = new MySQL($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['database']);
  $stm = $db->dbConn->prepare("SELECT Film_Title FROM Films WHERE Film_ID=?");
  // There's only one placeholder, so only 1 element in the array is needed
  $stm->execute(array($id));
  // Also you need to fetch the results
  $result = $stm->fetch();
  echo "<h3>film record " . $result['Film_Title'] . " has been selected</h3>";
  }

答案 1 :(得分:-1)

$ sql =“SELECT Film_Title FROM Films WHERE Film_ID = $ id”;