andyc209 Posted January 19, 2016 Share Posted January 19, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/300547-insert-statement-error/ Share on other sites More sharing options...
cyberRobot Posted January 19, 2016 Share Posted January 19, 2016 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'); Quote Link to comment https://forums.phpfreaks.com/topic/300547-insert-statement-error/#findComment-1530047 Share on other sites More sharing options...
ginerjm Posted January 19, 2016 Share Posted January 19, 2016 You are definitely not reading the manual's examples for how to do this. You execute your query twice - did you know that? Also - try turning on php error checking to see what errors may be troubling you. See my signature. Quote Link to comment https://forums.phpfreaks.com/topic/300547-insert-statement-error/#findComment-1530048 Share on other sites More sharing options...
mac_gyver Posted January 19, 2016 Share Posted January 19, 2016 you have to use all sqlsrv_ functions. the mssql_ functions are obsolete and haven't been available on windows since php 5.3. Quote Link to comment https://forums.phpfreaks.com/topic/300547-insert-statement-error/#findComment-1530051 Share on other sites More sharing options...
andyc209 Posted January 19, 2016 Author Share Posted January 19, 2016 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')"); Quote Link to comment https://forums.phpfreaks.com/topic/300547-insert-statement-error/#findComment-1530060 Share on other sites More sharing options...
mac_gyver Posted January 19, 2016 Share Posted January 19, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300547-insert-statement-error/#findComment-1530065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.