inserting record into SQL Server DB

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
arichins
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Nov 22, 2017 10:04 am

Wed Nov 22, 2017 10:25 am

I'm a PHP newb, trying to learn this language on my own. The first thing I want to do is just write a record into a sql server database table (sql server 2014).

I downloaded Netbeans, installed PHP on the web server and wrote the PHP code that I thought would write the record.

I put in an "echo" tests, in the code, so that it would report back to me if there were errors with the connection or the query, but the page is not showing me the string for either the true or false conditions. I'm also not seeing any errors when I debug it. So I'm baffled.

Here is the code: I put an initial echo "hello" in there just to make sure it's processing the PHP, and it is.

<?php
echo "hello";
$serverName = "trans";
$connectionInfo = array("Database"=="transdb");

$conn = sqlsrv_connect($serverName,$connectionInfo);

if ($conn==false){
echo "could not connect";
} else{ echo "connection suceeded";}

$sql1 = "insert into testtable values ('232','dog','444') " ;

$stmt = sqlsrv_query($conn, sql1);
if ($stmt==false){
echo "query error occured";
} else{ echo "query successful";}


?>

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 23
Joined: Mon Oct 16, 2017 1:06 pm

Wed Nov 22, 2017 12:06 pm

Do you see a problem here? If you used a proper IDE it would have been obvious.

$stmt = sqlsrv_query($conn, sql1);
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

arichins
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Nov 22, 2017 10:04 am

Wed Nov 22, 2017 2:44 pm

I figured out the problem. The sql server extensions are not installed by default. I found the php error log and see these entries
PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect()

And now I actually do see NetBeans throwing that same error in the output. I must have just missed it before.

Leaving off the $ was another typo, but PHP wasn't even getting that far in the processing.

Now that I got the 4.3 drivers installed, it's showing the echo statements. But it seems the connection is failing.

arichins
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Nov 22, 2017 10:04 am

Wed Nov 22, 2017 4:26 pm

I don't think this is really a coding issue anymore. I'm trying to connect to sql server through windows authentication. So I set the application pool to run with a user that has access to the DB, but I'm getting an error that implies that the php engine is trying to connect to the sql server with anonoymous authentication. I'll post this in another forum.

Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

Post Reply