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

时间:2014-11-28 04:32:01

标签: php sql-server

尝试使用PHP连接到我的数据库时出错。如果有人不介意为我看一下,我将不胜感激! (需要一组不同的眼睛哈哈)。

以下是我遇到的错误:

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

这是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];


?>

2 个答案:

答案 0 :(得分:2)

<?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);

?>

尝试使用此代码..

答案 1 :(得分:1)

此错误似乎数据库连接失败。因此,在获取sql quires后首先连接数据库。您可以在下面看到示例编码。

// 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);