mysql to sqlserver php code

Hi - I am trying to update an existing code and I want to convert the mysql implementation to sqlserver.
but all the changes I made those are not working
so what's the best way to modify the below code in php to sql server.
$insert_query = 'insert into '.$this->tablename.'(
		name,
		email,
		username,	
		password,
		salt,
		confirmcode
		)
		values
		( 
		"' . $this->SanitizeForSQL($formvars['name']) . '",
		"' . $this->SanitizeForSQL($formvars['email']) . '",
		"' . $this->SanitizeForSQL($formvars['username']) . '",
		"' . $encrypted_password . '",
		"' . $salt . '",
		'" . $confirmcode . "'
		)';  

Open in new window


Thanks,
shragiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
If that worked for MySQL, I see no obvious reason that it would fail with SQL Server.  What OS are you on and what PHP driver are you using?
shragiAuthor Commented:
I am using windows and php5
Sql server takes input as single string but here it is given in double quotes

I received the following error

Error inserting data to the table
query:insert into Users(
name,
email,
username,
password,
salt,
confirmcode
)
values
(
"abc",
"abc@outlook.com",
"abc",
"HbYlL/aIX/ZcHHSjh7/qzDQ1jU03YjQ2ZTE1MDgz",
"7b46e15083",
"d9773bae7c34d0514e9a8bd553c20505"
)

Open in new window

Randy PooleCommented:
use single quotes for SQL Server
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Randy PooleCommented:
$insert_query = "insert into ".$this->tablename."(
		name,
		email,
		username,	
		password,
		salt,
		confirmcode
		)
		values
		( 
		'" . $this->SanitizeForSQL($formvars['name']) . "',
		'" . $this->SanitizeForSQL($formvars['email']) . "',
		'" . $this->SanitizeForSQL($formvars['username']) . "',
		'" . $encrypted_password . "',
		'" . $salt . "',
		'" . $confirmcode . "'
		)";  

Open in new window

Ludwig DiehlSystems ArchitectCommented:
I suggest you change to PDO so if it'll be easier to change to any other database.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ludwig DiehlSystems ArchitectCommented:
By the way. This is what I recommend as the basic script using PDO:

$table='yourtable';

$insert_query = 
"INSERT INTO $table
(
	name,
	email,
	username,	
	password,
	salt,
	confirmcode
)
VALUES
(?,?,?,?,?,?)";
$type   ='dblib'; //SQL
$db     ='db_name';
$usr    ='user';
$pwd    ='password';
$pdo    = new PDO("$type:host=$host;dbname=$db",$usr,$pwd);
$stmt   = $pdo->prepare($insert_query);
$stmt->execute(array(
    $formvars['name'],
    $formvars['email'],
    $formvars['username'],
    $encrypted_password,
    $salt,
    $confirmcode)
); 

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.