如何将MySQL字符串转换为SQL Server等效项 [英] How to turn MySQL strings into SQL Server equivalents

查看:101
本文介绍了如何将MySQL字符串转换为SQL Server等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在客户网站上工作,遇到了一些问题.我正在使用一个使用MySQL字符串的同事向我展示的登录系统.我将客户端的信息插入其中并加载,并出现连接错误.梳理完我的代码后,我发现了一些错误,但修复它们并不能解决连接问题.在客户端的Web主机上四处浏览后,我注意到他们的数据库托管在Microsoft SQL服务器上,而不是MySQL服务器上.现在出现了问题:

I am currently working on a clients web site and I ran into a bit of a problem. I am using a log in system that was shown to me by a colleague that uses MySQL strings. I plugged the client's info into it and loaded it and got a connection error. After combing through my code I found some errors but fixing them did not fix the connection issue. After digging around on the client's web host I noticed that their database is hosted on a Microsoft SQL server instead of a MySQL server. Now here comes the questions:

1.)如何将我的MySQL字符串更改为SQL Server字符串?

1.) How do I change my MySQL strings into SQL Server strings?

2.)我仍然可以使用PHP来操纵SQL Server字符串吗?

2.) Can I still use PHP to manipulate the SQL Server strings?

3.)你们是否推荐任何好的教程来帮助我更好地理解这一点?

3.) Are there any good tutorials that you guys recommend that will help me understand this better?

参考

这是我正在使用的MySQL字符串,其中包括由PHP操纵的行:

Here is the MySQL strings I'm using including the lines that are manipulated by PHP:

    // Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE user_name='$myusername' and user_pswd='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
    $_SESSION['myusername'];
    $_SESSION['mypassword']; 

    header("location:Inside.php");
} else {
    echo "Wrong Username or Password";
}

任何建议将不胜感激.预先感谢!

Any suggestions would be greatly appreciated. Thanks in advance!

推荐答案

由于数据库不再是MySQL,因此您将不得不重写一些使用MySQL函数的代码.使用PDO(PHP数据对象)可以轻松完成此操作,并且对于将来的更改更容易移植.

Since the database is no longer MySQL, you will have to rewrite some of the code that uses MySQL functions. This is easily done with PDO (PHP Data Objects) and is far more portable for future changes.

请参见以下 SQL Server示例:

<?php

   $user = 'myUsername';
   $pass = 'myPassword';

   // Connect to mssql database
   $conn = new PDO('mssql:host=127.0.0.1; dbname=tempdb;', $user, $pass);

   $query = "SELECT * FROM table1";

   // Prepare query and run it. This is where you can use prepared statements
   // to avoid SQL injection
   $sth = $conn->prepare($query);
   $sth->execute();

   // Fetch the returned db rows and dump them as output
   $retRows = $sth->fetchAll();
   var_dump($retRows);

   // Clean up resources
   unset($sth); unset($conn);

?>

在代码中找到mysql_*之类的函数的任何地方,您都想使用

Anywhere you find a function like mysql_* in your code, you will want to lookup the proper way to do that using PDO.

这篇关于如何将MySQL字符串转换为SQL Server等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆