请帮助我在墙上花了好几个小时试图在php类方法中查询mssql数据库。使用过程方法我可以查询并显示结果,但在php类中,我在包含连接文件的任何时候都会得到状态500(内部服务器错误)。这是我的代码。
学生班
<?php class Student{
var $name;
function setName($item){
include_once($_SERVER['DOCUMENT_ROOT'] . '/eazycal/con_string.php');
$sql="SELECT * FROM Students WHERE StdID='".$item."'";
$stmt=sqlsrv_query($conn,$sql);
$row=sqlsrv_fetch_array($stmt);
$this->name = $row['Surame'];
}
function getName(){
return $this->title;
}
}
?>
数据库连接文件
<?php
$serverName="localhost\DBSERVER";
$connectionInfo=array( "Database"=>"school","UID"=>"John","PWD"=>"love");
$conn=sqlsrv_connect( $serverName,$connectionInfo);
?>
实施
require_once($_SERVER['DOCUMENT_ROOT']'/classes/stdClass.php');
$entity = new Student;
$entity->setName( "34" );
$student=$entity->getName();
$resp=array('name'=>$student);
echo json_encode($resp);