1

I am getting an error when trying to connect to my database using 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
    $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];


?>
1
  • Try moving the connection before start your db process. Commented Nov 28, 2014 at 4:57

2 Answers 2

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

?>

Try Using this code..

Sign up to request clarification or add additional context in comments.

Comments

1

This error seems database connection failed. So you first connect the database after you fetch 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.