Jump to content

Insert Statement Error


andyc209

Recommended Posts

Sorry i'm a total noobie to PHP having spent years on ASP

 

i am trying to connect and post into an MSSQL database and whilst i seem to be able to connect ok and do not get any errors - nothing goes into the database

 

my code is:

<?php

print "Hello world!";

$name = 'andy';

echo $name;


$serverName = "*******";
$connectionInfo = array( "Database"=>"TEST_PHP", "UID"=>"sa", "PWD"=>"*******");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}


$query = mssql_query("INSERT INTO TBL_PHP (NAME) VALUES ('$name')");  

mssql_query($query) or die ('Error Updatating Database');

?>

I get the 'Connection established' fine and i also get the echo bit displaying 'Andy' fine - nothing else is on the page and there is nothing posted into the database

 

can someone see where i am obviously going wrong

 

thanks

Link to comment
Share on other sites

You could try using mssql_get_last_message() to see if the database server is throwing an error. More information can be found here:

http://php.net/manual/en/function.mssql-get-last-message.php

 

Also note that you don't need to call mssql_query() twice. The following line is trying to process $query which will only contain "true" or "false":

mssql_query($query) or die ('Error Updatating Database');
Link to comment
Share on other sites

sorry - like i said i am a total noobie - only been at PHP for a few hours :(

 

i have added the code you suggest Ginerjm and get 

 

Fatal error: Call to undefined function mssql_query() 

 

which i assume means i am missing some drivers or something on the servers?

 

Mac_gyver - on my code i have SQLsrv_connect for the database connection - do i just need to change the line

 

$query = mssql_query("INSERT INTO TBL_PHP (NAME) VALUES ('$name')");  

 

to

 

$query = sqlsrv_query("INSERT INTO TBL_PHP (NAME) VALUES ('$name')");  

Link to comment
Share on other sites

do i just need to change the line

 

 

no. you cannot just change the name. the parameters that the two different sets of functions took are different. you must read the php.net documentation, for whatever function you are trying to use, so that you will gain an understanding of what parameters it requires and what they mean. there are also basic examples to be found in the php.net documentation along with user contributed notes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.