1

I know my question is a duplicate but I can't find the solution today. I got a tqo files db connection:

<?php
$serverName ="host\SQLEXPRESS";
 $usr="sa";
 $pwd="SysAdmin";
 $db="db_name";

//$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);

//$conn = sqlsrv_connect($serverName, $connectionInfo);

$conn = new PDO("sqlsrv:Server=host\SQLEXPRESS;Database=db_name", "sa", "SysAdmin");
?>

My action file:

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require_once 'db1.php';

$name='11111';
$sql_user = $conn -> sqlsrv_prepare("SELECT * FROM table_name WHERE id= :name");

$sql_user -> sqlsrv_execute(array(':name' => $name));
while($row = sqlsrv_fetch_array($sql_user))
  {
echo $row['name'];

}
?>

I get this error:

Fatal error: Call to undefined method PDO::sqlsrv_prepare() on line 7 
CodeShark
  • 1,709
  • 2
  • 14
  • 22
Klapsius
  • 3,273
  • 6
  • 33
  • 56

1 Answers1

2

sqlsrv_* doesn't exist in PDO.

The PDO-API is in all database drivers the same. Thats the idea behind PDO.

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require_once 'db1.php';

$name='11111';
$sql_user = $conn->prepare("SELECT * FROM table_name WHERE id= :name");
$sql_user->execute(array(':name' => $name));

$row = $sql_user->fetchAll();
var_dump($row);

update: to get only the name row:

$row = $sql_user->fetch(PDO::FETCH_ASSOC);
var_dump($row['name']);

PHP Manual PDOStatement::fetch

PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set

spinsch
  • 1,415
  • 11
  • 23