Ross Turner
asked on
Convert PHP MySql Connection Script to PHP ODBC Connection Script
Hi EE,
I've been spending days trying to convert the below PHP MySql script into an ODBC connection script to insert details into a ms sql database.
I've tried the odbc_prepare statements but i've been doing it for so long, it is all starting to look like hieroglyphics.
can someone help me please
I've been spending days trying to convert the below PHP MySql script into an ODBC connection script to insert details into a ms sql database.
I've tried the odbc_prepare statements but i've been doing it for so long, it is all starting to look like hieroglyphics.
can someone help me please
<?php
header('Content-type: application/json');
if($_POST) {
$username = $_POST['username'];
$password = $_POST['password'];
$c_password = $_POST['c_password'];
$gender = $_POST['gender'];
if($_POST['username']) {
if ( $password == $c_password ) {
$db_name = '********';
$db_user = '********';
$db_password = '********';
$server_url = '********';
$mysqli = new mysqli($server_url, $db_user, $db_password, $db_name);
/* check connection */
if (mysqli_connect_errno()) {
error_log("Connect failed: " . mysqli_connect_error());
echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}';
} else {
$stmt = $mysqli->prepare("INSERT INTO users (username, password, gender) VALUES (?, ?, ?)");
$password = md5($password);
$stmt->bind_param('sss', $username, $password, $gender);
/* execute prepared statement */
$stmt->execute();
if ($stmt->error) {error_log("Error: " . $stmt->error); }
$success = $stmt->affected_rows;
/* close statement and connection */
$stmt->close();
/* close connection */
$mysqli->close();
error_log("Success: $success");
if ($success > 0) {
error_log("User '$username' created.");
echo '{"success":1}';
} else {
echo '{"success":0,"error_message":"Username Exist."}';
}
}
} else {
echo '{"success":0,"error_message":"Passwords does not match."}';
}
} else {
echo '{"success":0,"error_message":"Invalid Username."}';
}
}else {
echo '{"success":0,"error_message":"Invalid Data."}';
}
?>
I use php with sql server 2012. I use PDO for everything. Also, do you have the ms sql drivers in your php.ini?
ASKER
Hi Scott,
i'm using php 5.4 installed via Web Platforms on IIS 8 on window server 2012 and on a seperate server i'm using sql server 2012.
This is my ExtensionList
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_mbstring.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_curl.dll
extension=php_exif.dll
extension=php_xmlrpc.dll
extension=php_openssl.dll
extension=php_soap.dll
extension=php_pdo_mysql.dl l
extension=php_pdo_sqlite.d ll
extension=php_imap.dll
extension=php_tidy.dll
extension=php_ldap.dll
extension=php_pdo_sqlsrv.d ll
extension=php_sqlsrv.dll
PDO?
i'm using php 5.4 installed via Web Platforms on IIS 8 on window server 2012 and on a seperate server i'm using sql server 2012.
This is my ExtensionList
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_mbstring.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_curl.dll
extension=php_exif.dll
extension=php_xmlrpc.dll
extension=php_openssl.dll
extension=php_soap.dll
extension=php_pdo_mysql.dl
extension=php_pdo_sqlite.d
extension=php_imap.dll
extension=php_tidy.dll
extension=php_ldap.dll
extension=php_pdo_sqlsrv.d
extension=php_sqlsrv.dll
PDO?
ASKER
HI Scott,
I've now added
extension=php_sqlsrv_54_ts .dll
extension=php_pdo_sqlsrv_5 4_ts.dll
And tested it connecting to my DB with the standard scripts under
http://php.net/manual/en/f unction.sq lsrv-conne ct.php
Does this make is possible to construct the same principle MySqli Script into PDO SQL ?
I've now added
extension=php_sqlsrv_54_ts
extension=php_pdo_sqlsrv_5
And tested it connecting to my DB with the standard scripts under
http://php.net/manual/en/f
Does this make is possible to construct the same principle MySqli Script into PDO SQL ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had to do some serious studying but managed to finally get this scenario to work, thank you for your advice and help on this matter.
Cheers
Ross
Cheers
Ross
Please change your code accordingly
<?php
//set connection parameters
$user = " *********";
$password = "******";
$database_name = "******";
$hostname = "localhost";
//setting the msyql driver along with dbname and host
$dsn = "DRIVER={MySQL ODBC 3.51 Driver};Server=$hostname;D
//connect to the database
$conn = odbc_connect($dsn, $user, $password);
//insert some rows
$rs = odbc_exec($conn, 'INSERT INTO `mytest` (`name`, `job`) VALUES ("Roy","senator"), ("Bill","engineer");');