简体   繁体   English

如何使用 PHP 连接到 SQL 服务器数据库

[英]How to connect to SQL Server database with PHP

I want to feed my html page with data that came from SQLServer database.我想为我的 html 页面提供来自 SQLServer 数据库的数据。 I wanted to use Javascript, but I've seen that wasn't the best option.我想使用 Javascript,但我发现这不是最好的选择。 So I tried with php (that is not my favorite language).所以我尝试了 php (那不是我最喜欢的语言)。

I tried this:我试过这个:

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/index.js"></script>


    <link rel="shortcut icon" href="res/MPS.png" type="image/x-icon"/>
    <link rel="icon" href="res/MPS.png" type="image/x-icon"/>
    <title>Historique</title>
  </head>
  <body>
    <?php
      $servername = "192.168.102.232\SQLEXPRESS"
      $connectionInfo = array("Database" => "PROFACE", "UID" => "userName", "PWD" => "password");
      $conn = sqlsrv_connect($servername, $connectionInfo);

      if($conn) {
        echo "Connexion OK. <br/>"; 
      } else {
        echo "Connexion NOK <br/>";
        die( print_r(sqlsrv_errors(), true));
      }
    ?>
  </body>
</html>

UserName and password are replace with real informations in my original code.用户名和密码替换为我原始代码中的真实信息。

This code doesn't work fine, and I have this error: Parse error: syntax error, unexpected '$connectionInfo' (T_VARIABLE)此代码无法正常工作,并且出现此错误: Parse error: syntax error, unexpected '$connectionInfo' (T_VARIABLE)

I don't see what is the problem and if my code could work.我看不出问题出在哪里,我的代码是否可以工作。

Your PHP code should be like this您的 PHP 代码应该是这样的

<?php
      $servername = "192.168.102.232\SQLEXPRESS";
      $connectionInfo = array("Database" => "PROFACE", "UID" => "userName", "PWD" => "password");
      $conn = sqlsrv_connect($servername, $connectionInfo);

      if($conn) {
        echo "Connexion OK. <br/>"; 
      } else {
        echo "Connexion NOK <br/>";
        die( print_r(sqlsrv_errors(), true));
      }
    ?>

You forgot to add semicolon on this line您忘记在此行添加分号

$servername = "192.168.102.232\SQLEXPRESS"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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