【问题标题】:php connection to sql serverphp连接到sql服务器
【发布时间】:2013-01-14 16:20:57
【问题描述】:

我正在尝试连接到在 MS Studio 中创建的数据库,但不是我创建的。 db 的创建者让我创建一个网站,我使用 PHP/HTML 完成了该网站。现在我需要将两者连接起来,并将他的数据显示到页面上。

我希望在这里得到一些帮助,我很新(很明显),我独自在这个项目上工作,因为创建者只使用 VB。

我要做的第一件事是建立与数据库的连接,但我没有运气,我只是收到 500 错误,据我所知是语法错误。

我已经在http://webcheatsheet.com/php/connect_mssql_database.php 使用创建者发送的信息尝试了此代码,但我觉得我错过了一些东西。

提前致谢!

我正在使用此代码。

<?php
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB"); 

//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'"; 

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result); 
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 

//display the results 
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

【问题讨论】:

  • 你能告诉我们你尝试过的代码吗?
  • 请显示您的实际连接和PHP代码(删除数据库用户名/密码)。
  • 500 错误在服务器日志中有更多详细信息。从那里开始。
  • 在第一篇文章中添加了代码。

标签: php sql-server


【解决方案1】:
 mssql_get_last_message() 

Try this to get the error message to see why you cant connect to the db server.
something like
if(!$connect) // This is your db handler
{
die('I cannot connect because:'.$mssql_get_last_message());
}

【讨论】:

  • 我认为问题的一部分是术语,他给了我:Data Source=XXX;Initial Catalog=XXX;User ID=XXX;Password=XXX 我觉得奇怪...
  • 它可能因维护而停机或配置不正确。以下是一些建议: 稍后重新加载此网页。 HTTP 错误 500(内部服务器错误):服务器尝试完成请求时遇到了意外情况。
  • *这是添加你上面提到的代码的结果。
【解决方案2】:

除了数据库名称错误之外,服务器似乎缺少驱动程序(由数据库管理员解决)。

现在开始构建 html 页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 2013-09-15
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多