gpt4 book ai didi

php - 如何将 MySQL 字符串转换为 SQL Server 等价物

转载 作者:行者123 更新时间:2023-11-29 04:02:31 25 4
gpt4 key购买 nike

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

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

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

3.) 你们有什么好的教程可以帮助我更好地理解这一点吗?


引用

这是我正在使用的 MySQL 字符串,包括由 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";
}

如有任何建议,我们将不胜感激。提前致谢!

最佳答案

由于数据库不再是MySQL,你将不得不重写一些使用MySQL函数的代码。这可以使用 PDO(PHP 数据对象)轻松完成,并且对于将来的更改更便于移植。

看看这个SQL Server example :

<?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_* 的函数,您将希望使用PDO 查找正确的方法来执行此操作。 .

关于php - 如何将 MySQL 字符串转换为 SQL Server 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9746245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com