Reputation: 77
I have a database table with this name dbo.[DATABASE$Employee]
and I would like to fetch records from the table but I get an error message
Notice: Undefined variable: Employee in C:\xampp\htdocs\kekportal\test1.php on line 24
Notice: Undefined variable: Employee in C:\xampp\htdocs\kekportal\test1.php on line 28
Notice: Undefined variable: Employee in C:\xampp\htdocs\kekportal\test1.php on line 28
My code:
$pdo = new PDO("sqlsrv: Server=U4BUO1D;Database=AmanfoHR","**","**");
$stmt = $pdo->prepare("SELECT
dbo.[DATABASE$Employee].No_ AS empID,
dbo.[DATABASEG$Employee].[First Name] AS fname
FROM
dbo.[DATABASE$Employee]");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
echo $row['fname'];
}
Upvotes: 1
Views: 2477
Reputation: 15977
I guess that prepare statement takes $Employee
as variable and this variable is not defined. Change your query part:
$stmt = $pdo->prepare("SELECT
e.No_ AS empID,
e.[First Name] AS fname
FROM
dbo.[DATABASE"."$"."Employee] e");
Also you can use another code to work with SQL Server.
$serverName = "U4BUO1D";
$connectionInfo = array("Database"=>"AmanfoHR", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "
SELECT No_ AS empID,
[First Name] AS fname
FROM dbo.[DATABASE"."$"."Employee];";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false ) {
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}
while ($obj = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $obj['fname'];
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
You need this two extensions to be enabled in php.ini:
extension=php_pdo_sqlsrv.dll
extension=php_sqlsrv.dll
Upvotes: 3