0

Hi I am out of ideas on why is php prompting me this error.

I have tested my codes in MS SQL and I am able to execute the following codes without any issues.

INSERT INTO MEMB_INFO
    (memb___id, memb__pwd, memb_name, sno__numb, bloc_code, ctl1_code)
    VALUES('test', '12345678', 'test', '1111111111111', '0', '0');

However when going back to my PHP I tried to execute the code through sqlsrv_query(), it doesn't work.

I am able to retrieve data through my PHP thus I can verify that my connection has been establish.

This is the code that is prompting me the error.

<?php 
try
{
$conn = new PDO("sqlsrv:Server=127.0.0.1,1433;Database=MyPhpDB", "sa", 'password');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e)
{
die(print_r($e->getMessage()));
}


$sql = "INSERT INTO MEMB_INFO
    (memb___id, memb__pwd, memb_name, sno__numb, bloc_code, ctl1_code)
    VALUES(?, ?, ?, ?, ?, ?);";
$params = array('test3', '12345678', 'test3', '1111111111111', '0', '0');

$stmt = sqlsrv_query($conn, $sql, $params);

if( $stmt === false ) {
 die( print_r( sqlsrv_errors(), true));
}

sqlsrv_close($conn);
?>

any help is greatly appreciated!! thank you.

shin1234
  • 217
  • 1
  • 3
  • 12
  • 1
    While the dupe just mentions PDO, mysql_* and mysqli_*, the same reasoning goes for every other MySQL API. Pick one and stick with it, they do not mix. – aynber Apr 29 '19 at 18:19
  • @aynber Uhm, may i know which portion are you referring to? because I am using MSSQL – shin1234 Apr 29 '19 at 18:22
  • 3
    `new PDO` vs `sqlsrv_query`. Just use the available PDO methods `->prepare()` and `->execute()` – mario Apr 29 '19 at 18:24
  • 1
    You can't create a connection object using PDO and then expect sqlsrv_query to be able to make use of it. They are different libraries with different structures and method names etc. I don't know if you got confused, or you are actively in the process of changing your code to to switch to PDO? If it's the latter you have to do it all at once, you can't keep half the old code mixed with half the new code. If it's the former, then see my first sentence. Choose a single library and stick to it. – ADyson Apr 29 '19 at 19:05
  • Thanks @ADyson I am rather new with PDO connection and I did not know i couldn't mix these codes as I assume both were calling and executing the same functions. THANKS! – shin1234 Apr 30 '19 at 02:02
  • Ultimately they may access the same API in SQL Server but on the PHP side they each manage it in a different way. If they were both the same there would be no need for two different libraries :-) – ADyson Apr 30 '19 at 07:02

0 Answers0