Link to home
Start Free TrialLog in
Avatar of Captain Cree
Captain Cree

asked on

PHP FreeTDS unable insert into MSSQL

I'm able to run select statements to my sql server
 $link = mssql_connect('10.10.50.xxx', 'userx', 'pasx');

if (!$link)
    die('Unable to connect!');
if (!mssql_select_db('Camstat', $link))
    die('Unable to select database!');

$result = mssql_query("INSERT INTO [DB1].[dbo].[Footage_Age] ([DVRId] ,[CameraId]  ,[Last_Date] ) VALUES    (135456,525685,'2013-08-27')");

while ($row = mssql_fetch_array($result)) {
    var_dump($row);
}

mssql_free_result($result);

Open in new window


But I'm unable to run inserts into the DB.  The user has write permission to the sql db.  I've tried a lot of examples I've found on the net but with no success.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Riddles
When performing an insert the $result on line 10 of your code is either going to be TRUE or FALSE.  It is not an object which you can use in your mssql_fetch_array() function call.

You should simply be checking if the value was TRUE or FALSE.

Try something like the following:
$link = mssql_connect('10.10.50.xxx', 'userx', 'pasx');

if (!$link) {
    die('Unable to connect!');
}
if (!mssql_select_db('Camstat', $link)) {
    die('Unable to select database!');
}

if (!mssql_query("INSERT INTO [DB1].[dbo].[Footage_Age] ([DVRId] ,[CameraId]  ,[Last_Date] ) VALUES    (135456,525685,'2013-08-27')")) {
    echo 'Unable to insert the data');
}

Open in new window

Note that there should be no need to free the result, as your insert would not generate more than one result.