繁体   English   中英

使用php执行SQL查询

[英]Execute sql query using php

我已经在PHP中使用FreeTDS连接了sql数据库服务器。 我试图在php中执行sql select查询。 我无法执行查询。 我在下面提到了程序

 <?php
    $empid = 10068;
    try
    {
        $db = new PDO('odbc:Driver=FreeTDS; Server=XXXXXXX; Port=1433; Database=XXXX; UID=XXXXXX; PWD=XXXXX;');
    }
    catch(PDOException $exception)
    {
        die("Unable to open database.<br />Error message:<br /><br />$exception.");
    }
    echo '<h1>Successfully connected!</h1>';
    echo gettype($empid);
    $query = "select Employee_ID from REPT_Employee where Employee_ID=$empid";
    echo $query
    **$statement = $db->prepare($query);** //Getting error in this line
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_NUM);
    echo $result 
 ?>

我无法执行查询。 谁能帮助我找出我犯的错误。

像这样改变

 $statement = $db->query("select Password from REPT_Employee where Employee_ID=$empid");
 $statement->execute();

如果不使用bindParam函数,则必须以这种方式使用prepare函数:

$statement = $db->prepare("select Employee_ID from REPT_Employee where Employee_ID='$empid'");

只需在$ query字符串中的$ empid字符串中加上引号''

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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