我正在尝试将我的php连接到SQL服务器数据库,但每次我尝试加载页面时,都会收到500错误。
这是我正在使用的语法,虽然下面是垃圾数据,但实际上我在生产文件中传递了有效的用户名和密码
<?php
$db_host = '.\SQLEXPRESS';
$db_user = 'sa';
$db_pwd = 'pass@word1';
$database = 'test';
$table = 'testIDtable';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT Top 1 saleID FROM {$table}";
$result = sqlsrv_query( $conn, $tsql);
if (!$result) {
die("Query to show fields from table failed");
}
while ($row = sqlsrv_fetch_array($result)){
echo "<option vale=\"{$row['saleID']}\">{$row['saleID']}</option>";
}
?>
编辑....-&GT - → - → - → - →;
这是整页语法,我有语法问题吗?页面将按原样加载,直到我添加此块
$db_host = 'Host';
$db_user = 'User';
$db_pwd = 'PWD';
$database = 'DB';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "Select Top 1 saleID From testIDtable";
$result = sqlsrv_query( $conn, $tsql);
if (!$result) {
die("Query to show fields from table failed");
}
while ($row = sqlsrv_fetch_array($result)){
echo "<option vale=\"{$row['saleID']}\">{$row['saleID']}</option>";
}
现在这是我的页面的完整语法..
{source}
<form method="POST">
<html>
<tbody>
Start Date:<input type="date" name="startdate" value="<?=$_POST['startdate']?>">
End Date:<input type="date" name="enddate" value="<?=$_POST['enddate']?>">
<input type="submit" name="submit" value="Get Info">
</tbody>
</html>
</form>
<?php
$db_host = 'Host';
$db_user = 'User';
$db_pwd = 'PWD';
$database = 'DB';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "Select Top 1 saleID From testIDtable";
$result = sqlsrv_query( $conn, $tsql);
if (!$result) {
die("Query to show fields from table failed");
}
while ($row = sqlsrv_fetch_array($result)){
echo "<option vale=\"{$row['saleID']}\">{$row['saleID']}</option>";
}
if(isset($_POST['submit'])){
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'Host';
$option['user'] = 'User';
$option['password'] = 'PWD';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance( $option );
$query = $db->getQuery(true);
$query = "Query 2 to populate a table";
$db->setQuery($query);
$query = $db->loadObjectList();
?>
<table border="1">
<thead>
<tr>
//Building out headers here
</tr>
</thead>
<tbody>
<?php
foreach( $query as $res ) {
print "<tr>";
//Writing results from query here
print "</tr>";
}
}
?>
</tbody>
</table>
{/source}
第二次编辑发布....-&gt; - &gt; - &gt; - &gt; - &gt;
我将问题MSSQL连接字符串更改为以下内容并且页面加载现在应该(不会再出现500错误)但是,是否未创建Select?
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'Host Name';
$option['user'] = 'user';
$option['password'] = 'password';
$option['database'] = 'Database';
$option['prefix'] = '';
$db = JDatabase::getInstance( $option );
$query = $db->getQuery(true);
$query = "Select Top 1 saleID From testIDtable";
$db->setQuery($query);
$query = $db->loadObjectList();
foreach( $query as $res ) {
echo "<option vale=\"{$row['saleID']}\">{$row['saleID']}</option>";
}