简体   繁体   English

使用PHP连接到MSSQL服务器时出错

[英]Error connecting to MSSQL server using PHP

I am getting an error when trying to connect to my database using PHP. 尝试使用PHP连接到我的数据库时出错。 If anybody wouldn't mind taking a look at it for me it would be greatly appreciated! 如果有人不介意为我看一下,我将不胜感激! (Need a different set of eyes lol). (需要一组不同的眼睛哈哈)。

Here is the error I'm getting: 这是我得到的错误:

Warning: mssql_query() [function.mssql-query]: Unable to connect to server: (null) in H:\root\home\... on line 24

Warning: mssql_query() [function.mssql-query]: A link to the server could not be established in H:\root\home\... on line 24

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in H:\root\home\... on line 25

Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near ','. (severity 15) in H:\root\home\... on line 35

Warning: mssql_query() [function.mssql-query]: Query failed in H:\root\home\... on line 35

Here is the PHP file: 这是PHP文件:

<?php
    $hostname_localhost ="********";
    $database_localhost ="*****";
    $username_localhost ="******";
    $password_localhost ="*****";

    $table_name = "VehicleInformation";
    //$phone = $_REQUEST['Phone'];
    //$rpm = $_REQUEST['RPM'];
    //$fuelLevel = $_REQUEST['FuelLevel'];
    //$engineRunTime = $_REQUEST['EngineRunTime'];
    //$speed = $_REQUEST['Speed'];
    //$troubleCode = $_REQUEST['TroubleCodes'];
    //$oilPressure = "0";
        $phone = "2125559852";
        $rpm = "1000";
        $fuelLevel = "20";
        $engineRunTime = "200";
        $speed = "25";
        $oilPressure = "420";


    $ownedByQuery = "Select o.Id from OwnedBy o JOIN tblUser u ON o.UserId = u.ID where u.Phone = '$phone'";
        $sql2 = mssql_query($ownedByQuery);
        $row = mssql_fetch_array($sql2);

    $query = "INSERT INTO $table_name (OwnedById,RPM,FuelLevel,OilPressure,EngineRunTime,Speed) VALUES ($row[0],$rpm,$fuelLevel,$oilPressure,$engineRunTime,$speed)";

    $server = mssql_connect($hostname_localhost,$username_localhost,$password_localhost)
    or
    die('MSSQL Error: ' . mssql_get_last_message());

    mssql_select_db("*******", $server);
    //$sql2 = mssql_query($ownedByQuery);
    $sql=mssql_query($query);

    //$row = mssql_fetch_array($sql2);
        //$row = mssql_fetch_array($sql);

    //echo $row[0];


?>
<?php
    $myServer = "localhost";
    $myUser = "your_name";
    $myPass = "your_password";
    $myDB = "examples";

    //connection to the database
    $dbhandle = mssql_connect($myServer, $myUser, $myPass)
      or die("Couldn't connect to SQL Server on $myServer");

    //select a database to work with
    $selected = mssql_select_db($myDB, $dbhandle)
      or die("Couldn't open database $myDB");

    //declare the SQL statement that will query the database
    $query = "Select o.Id from OwnedBy o JOIN tblUser u ON o.UserId = u.ID where u.Phone = '$phone'";


    //execute the SQL query and return records
    $result = mssql_query($query);

    $numRows = mssql_num_rows($result);
    echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

    //display the results
    while($row = mssql_fetch_array($result))
    {
       // do something
    }
    //close the connection
    mssql_close($dbhandle);

?>

Try Using this code.. 尝试使用此代码..

This error seems database connection failed. 此错误似乎数据库连接失败。 So you first connect the database after you fetch sql quires. 因此,在获取sql quires后首先连接数据库。 Below you see the example coding. 您可以在下面看到示例编码。

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

echo "Connected successfully";

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
}
else{
    echo "0 results";
}

mysqli_close($conn);

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

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