PHP连接到Sql server,但查询不起作用

时间:2014-05-05 13:38:51

标签: php sql-server

如果我的问题有一个简单的答案,我道歉,但我是新手,并且有点卡住...... 所以我应该为一个项目制作一个烹饪食谱网站,我正在使用PHP 5.5.9和SQL Server 2012.我已经按照视频教程创建了一个搜索框,应该列出从数据库中检索的结果。这是我的代码:

<?php
include ("dbconnect.php");

if (!isset($_POST['search']))    {
    header("Location:index.php");
}
$search_sql="SELECT * FROM Recipe WHERE name LIKE '%".$_POST['search']."%' OR description LIKE '%".$_POST['search']."%'";
$search_query=sqlsrv_query($conn, $search_sql);
if (sqlsrv_num_rows($search_query)!=0) {
    $search_rs=sqlsrv_fetch_assoc($search_query); }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
</head>
<body>
<h3>Search results</h3>
<?php
if (sqlsrv_num_rows($search_query)!=0) {
    do { ?>
        <p><?php echo $search_rs['name']; ?></p>
    <?php }
    while ($search_rs=sqlsrv_fetch_assoc($search_query));
}
else {
    echo "No results found";
}
?>
</body>
</html>

这是我的dbconnect.php:

<?php
$serverName = "USER\SQLEXPRESS"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"iCook project");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
    echo "Connection established.<br />";
}else{
    echo "Connection could not be established.<br />";
    die( print_r( sqlsrv_errors(), true));
}
?>

安装php 5.5和SQL Server的驱动程序后,数据库确实设法连接..但是,即使我对值进行硬编码,查询也无法正常工作。我不会再出现错误,但总是“找不到结果”......可能值得一提的是我接下来的教程是使用MySQL,所以我在代码中将mysql更改为sqlsrv。此外,查询DOES直接在SQL server ...

中执行

我非常感谢任何帮助...

编辑: 好吧,但我没有运气,但我发现了一些东西 - PhpStorm说“未定义的函数sqlsrv_fetch_assoc。在内置库和项目文件中找不到引用函数的声明。”我试过谷歌搜索,但什么也没出现你是怎么做到的? 另外,我使用的教程是this one ...

4 个答案:

答案 0 :(得分:1)

更改

$search_sql="SELECT * FROM Recipe WHERE name ='%".$_POST['search']."%' OR description ='%".$_POST['search']."%'";

$search_sql="SELECT * FROM Recipe WHERE name LIKE '%".$_POST['search']."%' OR description LIKE '%".$_POST['search']."%'";

让我知道它是怎么回事。

答案 1 :(得分:0)

这就是我为{strong> SQL Server 构建我db_connect.php的方式,可能问题出在连接中了吗?

<?php 
$server = "localhost"; // aka "localhost" or "192.168.10.110" 
$username = "user"; // aka "ChuckNorris" 
$password = "password"; // aka "cankickyourass" 
$database = "TestDB1"; // database you want to connect to 

$connectionOptions = array("Database" => $database, "UID" => $username, "PWD" => $password);

/* Connect using Windows Authentication. */
$conn = sqlsrv_connect($server, $connectionOptions);

if( $conn ) {
    // Connection Established!
} else {
    echo "Connection could not be established.<br />";
    die( print_r( sqlsrv_errors(), true));
}
?>

这就是我执行 SQL Server 查询的方式

$sql = "SELECT *
        FROM Users
        WHERE UserName LIKE '%user%'";

// Execute query:
$result = sqlsrv_query($conn, $sql) or die('A error occured: ' . sqlsrv_errors());

if(!$result)
{
    echo 'There are no users. ' . sqlsrv_errors();
}
else
{
    while($row = sqlsrv_fetch_array($result))
    {   
        //use $row['column'] here
    }
}

答案 2 :(得分:0)

只需更改“ while ”,如下所示,它将起作用

while( $search_rs = sqlsrv_fetch_array( $search_query, SQLSRV_FETCH_ASSOC) );

您也可以点击链接http://www.php.net/manual/en/function.sqlsrv-fetch-array.php,它会对您有所帮助......

答案 3 :(得分:0)

我切换到MySQL,查询现在正常工作。感谢所有试图帮助的人!