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.) How do I change my MySQL strings into SQL Server strings?
2.) Can I still use PHP to manipulate the SQL Server strings?
3.) Are there any good tutorials that you guys recommend that will help me understand this better?
Reference
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!
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.
Look at this SQL Server example:
Anywhere you find a function like
mysql_*in your code, you will want to lookup the proper way to do that using PDO.