duanmei1850 2017-02-14 11:08
浏览 59
已采纳

IONIC:从MYSQL到MSSQL

Am creating an ionic application using php, at the beginning I've linked the login form with a mysql database that I've created using wamp as a server and everything works fine! then my boss saids that I have to change it to another database which is on a Microsoft sql server, so I've linked the wamp server with MsSQL and the Connection with database on MSSQl established correctly

but I didn't know how to change the syntaxe on login.php

Here are the old and new cofig.php that works fine

(New config.php)

<?php
$serverName = "HAMDI-PC";
$connectionInfo = array ("Database"=>"MAINT","UID"=>"sa","PWD"=>"sql") ;
$conn = sqlsrv_connect( $serverName, $connectionInfo);
?>

(OLD config.php)

<?php
$conn = new mysqli("localhost", "root", "", "MAINT");
?>

And here is the (login.php) that works with mysql database

<?php 
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

if(isset($_GET["username"]) && isset($_GET["password"]) ) {
    if( !empty($_GET["username"]) && !($_GET["password"]) ) {
        include"config.php";

        $username=$_GET["username"];
        $password=$_GET["password"];

        $query=" SELECT * FROM D_PROTUSERS WHERE PROT_User='$username' AND    PROT_Password ='$password' ";
        $result = $conn->query($query);

        $out="";
        if ($rs=$result->fetch_array()) {
            if($out != "") {$out .="";}
            $out .='{"PROT_User":"'. $rs["PROT_User"] . '",';
            $out .='"PROT_Password":"'. $rs["PROT_Password"] . '"}';
        }
        $out='{"recods":'.$out.'}';
        $conn->close();
        echo($out);
    }
}
?>

So please I want to know what should I change to make it work at the new database'MSSQl) , I'll be thankful to everyone who will tries to help :*

  • 写回答

1条回答 默认 最新

  • drryyiuib43562604 2017-02-14 11:41
    关注

    Forget about mysql, you're using ms sql.. so you have to use sqlsrv

    Here we go.

    config.php

    $serverName = "HAMDI-PC\SQLEXPRESS";
    $dbname = "MAINT";
    $uid = "sa";
    $pwd = "sql";
    global $con;
    
    $connectionInfo = array( "UID"=>$uid,
                         "PWD"=>$pwd,
                         "Database"=>$dbname);
    
    /* Connect using SQL Server Authentication. */
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if( $conn === false )
    {
     die( print_r( sqlsrv_errors(), true));
    }
    

    And

    login.php

    <?php
        session_start();
        include"config.php";
    
        $username=$_REQUEST["username"];
        $password=$_REQUEST["password"];
    
        $query = " SELECT * FROM D_PROTUSERS WHERE PROT_User='$username' AND PROT_Password ='$password' ";
        $result = sqlsrv_query($conn, $query, array() , array(
          "Scrollable" => 'keyset'
        ));
        $num = sqlsrv_num_rows($result);
    
        if ($num > 0)
        {
          $_SESSION["valid_user"] = true;
          $_SESSION['username'] = $username;
          sqlsrv_close($conn);
        }
        else
        {
          echo "Login Failed: Connection could not be established.";
          exit();
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿