1

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'];
}   
3
  • It might be better to use PDO Commented Aug 20, 2016 at 16:36
  • $pdo = new PDO("sqlsrv: Server=SERV;Database=DB","",""); $sql = 'SELECT dbo.[DATABASE$Employee].No_ AS empID, dbo.[DATABASE$Employee].[First Name] AS fname, FROM dbo.[DATABASE$Employee]'; $q = $pdo->query($sql); $q->setFetchMode(PDO::FETCH_ASSOC); ?> <?php while ($row=$q->fetch()):?> <?php echo $row['fname'];?> <?php endwhile ; ?> i hade error "Fatal error: Call to a member function setFetchMode() on a non-object in C:\xampp\htdocs\kekportal\test1.php on line 30" Commented Aug 20, 2016 at 16:44
  • Please edit your question Commented Aug 20, 2016 at 17:00

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.