简体   繁体   中英

How to connect sql server to php

Hi I had a question about connecting php to sql server I install odbc driver and confige php.ini file. But when execute I get error. I change the computer but still get same error.

    <?php
$serverName = "YASSERAHMMED\YASOFT";
$connectionInfo = array( "Database"=>"ELECTERC_DB", "UID"=>"sa", "PWD"=>"google");
$con = sqlsrv_connect( $serverName, $connectionInfo );
if( $con === false ) {
    //echo sqlsrv_errors();
    die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT * FROM ACCOUNT_CUSTMERS";
$stmt = sqlsrv_query( $con, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      echo $row['date'].", ".$row['id']."<br />";
}

sqlsrv_free_stmt( $stmt);
?>

the result is: error 5701 and 5703

I finally use PDO string connection to resolve the problem.. thanks every one...

$serverName = "YASSERAHMMED\YASOFT";  

try  
{  
  $conn = new PDO( "sqlsrv:server=$serverName ; Database=ELECTERC_DB", "", "");  
  $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 
  if($conn){
     // echo'ggggg';
  } else{
      echo'undone';
  }
}  
catch(Exception $e)  
{   
  die( print_r( $e->getMessage() ) );   
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM