我是php上的新手,我尝试为数据库中的人们旅行信息制作一个日历,并希望在日历当天显示这些信息,所以我的问题是连接MSSQL Server并采取值形式db.My php网页上的本地主机(wampp)和我的日历视图完美地工作,但不能从DB.It值没有给出error.But不能拉data.Could请你帮我找到我fault.Thanks。
connect.php;
<?php
class DBO
{
private $server = "servername";
private $db_name = "dbname";
private $password = "pass";
private $username = "username";
private $conn;
private $database;
function Connect()
{
$this->conn = mssql_connect($this->server, $this->username, $this->password)
or die("Couldn't connect to SQL Server on " . $this->server);
$this->database = mssql_select_db($this->db_name, $this->conn)
or die("Couldn't open database " . $this->db_name);
}
function RunSql($sqlQuery)
{
return mssql_query($sqlQuery);
}
}
?>
查询代码;
<?php
include_once("db_connect.php");
$DBO = new DBO();
$DBO->Connect();
$query = "SELECT ID, TYPE, STARTDATE, ENDDATE FROM TABLENAME";
$query_results = $DBO->RunSql($query)
or die('Error in $query_menu. Error code :' . mssql_get_last_message() );
$calendar = array();
while( $results = mssql_fetch_assoc($query_menu_results) )
{
$calendar[] = array('ID' =>$rows['ID'],'TYPE' => $rows['TYPE'],'url' => "#","class" =>'event-important','start' => "$start",'end' => "$end");
}
$calendarData = array(
"success" => 1,
"result"=>$calendar);
echo json_encode($calendarData);
exit;
?>
<?php
答案 0 :(得分:0)
我相信您的问题在于通过......执行SQL。
$query_results = $DBO->RunSql($query)
...但是通过...
使用它while( $results = mssql_fetch_assoc($query_menu_results) )
基本上,$ query_results变为$ query_menu_results。
我使用它的变体来测试MsSQL连接:
<?php
// connect to the server
$dbconn = mssql_connect('SEVER_NAME_OR_IP', 'MsSQL-User', 'MsSQL-User-Password');
if (!$dbconn) {
$message='unable to connect to the database: ' . mssql_get_last_message();
die($message);
}
// select the database
if (!mssql_select_db('DATABASE_NAME', $dbconn)) {
die('Unable to select the database!');
}
// execute a query. NOTE: Never use string concatenation in SQL statements. EVER!!!
$sql = "select * from information_schema.tables;";
$query = mssql_query($sql, $dbconn);
// consume the results
echo "<table border='1'><tr><th>Column A</th><th>Column B</th></tr>";
while ($row = mssql_fetch_row($query)) {
echo "<tr><td>";
echo $row[0];
echo "</td><td>";
echo $row[1];
echo "</td></tr>";
}
echo "</table>";
// clean up
mssql_free_result($query);
mssql_close($dbconn);
?>
安全注意:最后一次检查,mssql*不支持预备语句。请参阅PDO或sqlsrv*。 This Question在MsSQL准备好的语句中有很好的信息。
答案 1 :(得分:0)
<强>解决方案:强>
我已经复制了您的代码,但我发现了这些错误:
include_once("db_connect.php");
必须为include_once("connect.php");
while ($results = mssql_fetch_assoc($query_menu_results)) {
必须为while ($results = mssql_fetch_assoc($query_results)) {
'ID' =>$rows['ID'],
必须为'ID' => $results['ID'],
'TYPE' => $rows['TYPE'],
必须为'TYPE' => $results['TYPE'],
$start
和$end
个变量; 工作示例:
示例基于您的代码。我已经把你的错误留给了评论。表定义只是为了进行工作查询。
数据库:
CREATE TABLE [dbo].[TABLENAME] (
[ID] [numeric](10, 0) NULL,
[TYPE] [numeric](10, 0) NULL,
[STARTDATE] datetime NULL,
[ENDDATE] datetime NULL
) ON [PRIMARY]
INSERT [TABLENAME] (ID, TYPE, STARTDATE, ENDDATE)
VALUES (1, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
INSERT [TABLENAME] (ID, TYPE, STARTDATE, ENDDATE)
VALUES (1, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
SELECT ID, TYPE, STARTDATE, ENDDATE
FROM [TABLENAME]
connect.php
<?php
class DBO {
private $server = "servername";
private $db_name = "dbname";
private $password = "pass";
private $username = "username";
private $conn;
private $database;
function Connect() {
$this->conn =
mssql_connect($this->server, $this->username, $this->password) or
die("Couldn't connect to SQL Server on " . $this->server);
$this->database =
mssql_select_db($this->db_name, $this->conn) or
die("Couldn't open database " . $this->db_name);
}
function RunSql($sqlQuery) {
return mssql_query($sqlQuery);
}
}
?>
query.php
<?php
//include_once("db_connect.php");
include_once("connect.php");
$DBO = new DBO();
$DBO->Connect();
$query = "SELECT ID, TYPE, STARTDATE, ENDDATE FROM [TABLENAME]";
$query_results =
$DBO->RunSql($query)
or die('Error in $query_menu. Error code :' . mssql_get_last_message() );
$calendar = array();
//while ($results = mssql_fetch_assoc($query_menu_results)) {
while ($results = mssql_fetch_assoc($query_results)) {
$calendar[] = array(
//'ID' =>$rows['ID'],
'ID' => $results['ID'],
//'TYPE' => $rows['TYPE'],
'TYPE' => $results['TYPE'],
'url' => "#",
"class" => 'event-important',
//'start' => "$start",
//'end' => "$end"
);
}
$calendarData = array(
"success" => 1,
"result"=>$calendar
);
echo json_encode($calendarData);
exit;
?>
输出:
{
"success":1,
"result":[
{"ID":"1","TYPE":"1","url":"#","class":"event-important"},
{"ID":"1","TYPE":"1","url":"#","class":"event-important"}
]
}