我有这样的代码,工作正常,我需要得到相同的结果然后我转换我的数据库连接
$dbh = new PDO('mysql:host=localhost;dbname=DB', 'root', '');
$dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
$stmt = $dbh->prepare('SELECT * FROM `Details` WHERE `ID`="2";');
$stmt->execute();
$result = $stmt->fetchAll();
$json=json_encode($result);
echo $json;
现在我将此代码转换为:
$con = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message());
if($con){
// echo "connected";
}
// Select a database:
mssql_select_db('DB')
or die('Could not select a database.');
$SQL = "SELECT * FROM Details WHERE ID='2'";
// Execute query:
$result = mssql_query($SQL)
如何通过PDO连接获得与我的第一个代码相同的结果?
$result = $stmt->fetchAll();
$json=json_encode($result);
echo $json;
在第一个PDO示例中,我得到 echo $ json 这样的数据结构:
[{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"0","3":"0","WtTimeFrom":"01:30:00","4":"01:30:00","WtTimeTo":"03:00:00","5":"03:00:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"1","3":"1","WtTimeFrom":"06:00:00","4":"06:00:00","WtTimeTo":"06:30:00","5":"06:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"1","3":"1","WtTimeFrom":"07:00:00","4":"07:00:00","WtTimeTo":"07:30:00","5":"07:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"1","3":"1","WtTimeFrom":"07:30:00","4":"07:30:00","WtTimeTo":"08:00:00","5":"08:00:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"1","3":"1","WtTimeFrom":"08:00:00","4":"08:00:00","WtTimeTo":"08:30:00","5":"08:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"2","3":"2","WtTimeFrom":"03:00:00","4":"03:00:00","WtTimeTo":"05:00:00","5":"05:00:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"2","3":"2","WtTimeFrom":"07:30:00","4":"07:30:00","WtTimeTo":"12:30:00","5":"12:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"3","3":"3","WtTimeFrom":"03:00:00","4":"03:00:00","WtTimeTo":"04:30:00","5":"04:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"3","3":"3","WtTimeFrom":"07:30:00","4":"07:30:00","WtTimeTo":"11:30:00","5":"11:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"3","3":"3","WtTimeFrom":"12:00:00","4":"12:00:00","WtTimeTo":"12:30:00","5":"12:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"3","3":"3","WtTimeFrom":"13:00:00","4":"13:00:00","WtTimeTo":"13:30:00","5":"13:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"3","3":"3","WtTimeFrom":"14:30:00","4":"14:30:00","WtTimeTo":"15:00:00","5":"15:00:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"4","3":"4","WtTimeFrom":"07:00:00","4":"07:00:00","WtTimeTo":"09:30:00","5":"09:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"},{"ID":null,"0":null,"TimeID":"2","1":"2","MyToDate":null,"2":null,"MyDayOfW":"4","3":"4","WtTimeFrom":"03:00:00","4":"03:00:00","WtTimeTo":"03:30:00","5":"03:30:00","EntryDate":"2018:05:17 05:44:53.906","6":"2018:05:17 05:44:53.906"}]
答案 0 :(得分:0)
试试这个:
$con = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message());
if($con){
// echo "connected";
}
// Select a database:
mssql_select_db('DB')
or die('Could not select a database.');
$SQL = "SELECT * FROM Details WHERE ID='2'";
$result = mssql_query($SQL);
$record = mssql_fetch_assoc($result);
$json=json_encode($record);
echo $json;
答案 1 :(得分:0)
工作示例:
<?php
// Settings
$server = '127.0.0.1\instance,port';
$user = 'user';
$password = 'password';
$db = 'DB';
// Connection
$con = mssql_connect($server, $user, $password);
if (!$con) {
mssql_close($con);
echo 'Error: '.mssql_get_last_message();
exit;
}
// Query
$sql = "SELECT * FROM Details WHERE ID='2'";
if (!mssql_select_db($db, $con)) {
mssql_close($con);
echo 'Error: '.mssql_get_last_message();
exit;
};
$result = mssql_query($sql);
if ($result === false) {
mssql_close($con);
echo 'Error: '.mssql_get_last_message();
exit;
}
// Result
$json = array();
do {
while ($row = mssql_fetch_assoc($result)) {
$json[] = $row;
};
} while (mssql_next_result($result));
// JSON
mssql_close($con);
$json = json_encode($json);
echo $json;
?>
注意:
使用MS SQL Server 2012和PHP 5.2.10测试了示例。