Link to home
Start Free TrialLog in
Avatar of cescentman
cescentmanFlag for United Kingdom of Great Britain and Northern Ireland

asked on

MySQL Connection Strings

I have a 2008 R2 server wit, MySQL 5.6.14 installed on it. I am running IIS and have PHP installed, phpinfo() gives me:-

PHP Version 5.3.24

System       Windows NT CURR-SERVER 6.1 build 7601 (Windows Server 2008 R2
Standard Edition Service Pack 1) i586
Compiler       MSVC9 (Visual C++ 2008)
Configure Command       cscript /nologo configure.js
"--enable-snapshot-build" "--enable-debug-pack" "--disable-zts"
"--disable-isapi" "--disable-nsapi" "--without-mssql" "--without-pdo-mssql"
"--without-pi3web"
"--with-pdo-oci=C:\php-sdk\oracle\instantclient10\sdk,shared"
"--with-oci8=C:\php-sdk\oracle\instantclient10\sdk,shared"
"--with-oci8-11g=C:\php-sdk\oracle\instantclient11\sdk,shared"
"--with-enchant=shared" "--enable-object-out-dir=../obj/"
"--enable-com-dotnet=shared" "--with-mcrypt=static"
"--disable-static-analyze"

I have downloaded and installed the MySQL Connector from here:-

http://dev.mysql.com/downloads/connector/odbc/

... I chose: Windows (x86, 64-bit), MSI Installer

In Data sources I can see MySQL ODBC 5.3 Unicode Driver. I can create a DSN in data sources with tests fine

I am trying to connect my PHP code to MySQL. I have tried the following connection strings without success:-

$strCon = 'Driver={MySQL ODBC 5.3 Unicode Driver};Server=localhost;Database=' . $myDataBase . ';User=' .  $myUsername . ';Password=' .  $myPassword . ';Option=3;';
$strCon = 'Provider=MSDASQL;Driver={MySQL ODBC 5.3 Unicode Driver};Server=localhost;Database=' . $myDataBase . ';User=' .  $myUsername . ';Password=' .  $myPassword . ';Option=3;';
$strCon = 'Driver={MySQL ODBC 5.3 Unicode Driver};Server=localhost;Database=' . $myDataBase . ';User=' .  $myUsername . ';Password=' .  $myPassword . ';Option=3;';

Open in new window


I would be grateful in help identifying how to progress this.
Avatar of Mohamed Magdy
Mohamed Magdy
Flag of Egypt image

You are i586, then you have to choose x86, 32-bit.
Avatar of cescentman

ASKER

Even though it's a 64bit operating system? Is the DSN unconnected with the MySQL connector then?
Avatar of Dave Baldwin
Since you have PHP, you should use the MySQL drivers for PHP.  http://php.net/manual/en/book.mysqli.php  You don't need the MySQL Connector for PHP though you might find it useful for other languages.
Thanks Dave

Any thoughts on the connection string I should be using?
OOP:

private db_host = ‘’; 
private db_user = ‘’; 
private db_pass = ‘’; 
private db_name = ‘’; 
 
public function connect()
    {
        if(!$this->con)
        {
            $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
            if($myconn)
            {
                $seldb = @mysql_select_db($this->db_name,$myconn);
                if($seldb)
                {
                    $this->con = true; 
                    return true; 
                } else
                {
                    return false; 
                }
            } else
            {
                return false; 
            }
        } else
        {
            return true; 
        }
    }

Open in new window


Traditional PHP:

<?php

//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="HOSTNAME";
$database="DBNAME";
$username="DBUSER";
$password="myPassword";

//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
     echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}

$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
    die ('Can\'t select database: ' . mysql_error());
}
else {
    echo 'Database ' . $database . ' successfully selected!';
}

mysql_close($link);

?>

Open in new window

You can't use the code above because the 'mysql' functions are obsolete http://php.net/manual/en/intro.mysql.php .  Please read the articles I linked about 'mysqli' which is the current MySQL driver.   This page http://php.net/manual/en/mysqli.construct.php shows you where to start.  You won't be using connection strings like you would in MS SQL server.
I'm using PDO:-

$strCon = 'Driver={MySQL ODBC 5.3 Unicode Driver};Server=localhost;Database=' . $myDataBase . ';User=' .  $myUsername . ';Password=' .  $myPassword . ';Option=3;';
    $objThumbs = new PDO($strCon)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Dave, I'll look at this. Will be a few days but will get back to you.
Works a treat I can see now I was trying to add an unnecessary layer. Thanks
You're welcome, glad to help.