odbc php connection failure

Hi
I'm trying to connect to a Mysql db that is on my web server.  I found the following code to test my connection :
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php 
$c = odbc_connect("Username", "Password", "Driver={MySQL ODBC 5.3 ANSI Driver};Server=localhost;Port=3306;Database=DBname; User=maestrodb;Password=123456;Options=3;");
  if ($c) {
   echo "Successfully connected to MySQL.";
   odbc_close($c);
 }
 else {  
   echo "MySQL Connect Error ";
 }
 
?>
</body>
</html>

Open in new window


The connection is failing and I do not know why.  Here's my setup:
Server 2008R2 ( on Hyper-V)
Wamp Server 2.5
Apache 2.4.9
PHP 5.5.12
MySQL 5.6.17
ODBC drivers: MySQL ODBC 3.51 Driver, MySQL ODBC 5.3 ANSI Driver, MySQL ODBC 5.3 Unicode Driver.


I'm not sure where to look why the connection is failing since it's all on the same server...  The user that I'm using is DB administrator.
Daniel VachonIT ProfessionalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SStoryCommented:
Is that driver installed on the machine?  

Is your connection string right?  You can check that at: https://www.connectionstrings.com/mysql/

Is the firewall on? Try turning it off for kicks to see if it works, and then add appropriate entries to make work when turning back on.
0
Daniel VachonIT ProfessionalAuthor Commented:
Yes, the driver is installed, firewall is disabled and the string seems fine from what I can see, but I'm no expert on php code or MySQL...
Here's the error I get:
  Error.JPG
0
SStoryCommented:
I think you need to remove beginning User and Password
I also assume DBName is not a variable but just you hiding the real name. If it is a variable it will need be concatenated with the string instead of inlined. Example
"first of connection string Database=" . $DBNameVariable . "continue with string"  Any variables have to be done in that fashion.


$c = odbc_connect( "Driver={MySQL ODBC 5.3 ANSI Driver};Server=localhost;Port=3306;Database=DBname; User=maestrodb;Password=123456;Options=3;");

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Daniel VachonIT ProfessionalAuthor Commented:
Thanks for the code, here's the new generated error:
Warning: odbc_connect() expects at least 3 parameters, 1 given in C:\wamp\www\test.php on line 7

DBname is not a variable, just hiding the real name.  I noticed there's a space between database and user but no space with user password and option.  Is this normal?
Also, should I put in the ip adress or fqdn instead of localhost?
0
SStoryCommented:
if you ping localhost from a command line
ping localhost

Open in new window

 does it work? It should be 127.0.0.1 on IPV4

Also from a browser does
http://localhost

Open in new window

work?

You can remove the space before database and try it but I don't think it matters.
let's try this:
$c = odbc_connect( "Driver={MySQL ODBC 5.3 ANSI Driver};Server=localhost;Port=3306;Database=DBname Options=3;",maestrodb,123456);

Open in new window


as this (http://php.net/manual/en/function.odbc-connect.php) shows username and password as separate arguments to the function than the DNS string.
0
Daniel VachonIT ProfessionalAuthor Commented:
Ping works, http://localhost also works (opens up wampserver page)
The new code generates this error:

Warning: odbc_connect(): SQL error: [MySQL][ODBC 5.3(a) Driver]Unknown database 'maestro options=3', SQL state S1000 in SQLConnect in C:\wamp\www\test.php on line 7
0
SStoryCommented:
Sorry, missing a semicolon:
$c = odbc_connect( "Driver={MySQL ODBC 5.3 ANSI Driver};Server=localhost;Port=3306;Database=DBname; Options=3;",maestrodb,123456);

Open in new window


Actually you may need to double quote username and password like so:
$c = odbc_connect( "Driver={MySQL ODBC 5.3 ANSI Driver};Server=localhost;Port=3306;Database=DBname; Options=3;","maestrodb","123456");

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Daniel VachonIT ProfessionalAuthor Commented:
It works!!! first success in 3 weeks :)  I was using this because I have a php site that connects using odbc but does not work, so I wanted to test the connection to the odbc.
The code that does not work is this:

if(!isset($_POST['MODE'])) { $_POST['MODE'] = 'ODBC'; }

if($_POST['MODE']=='ODBC' AND $_POST['ACTION']=='Soumettre') {

	$message = '<font color="#FF0000"><b>';	
	if(!odbc_connect($_POST['DB1_ODBC'],$_POST['DB1_USER'],$_POST['DB1_PWD'])) { $odbc1 = 0; $message .= 'Erreur dans les paramètres du connecteur ODBC de votre logiciel<br>'; } else { $odbc1 = 1; odbc_close(); }
	if(strlen($_POST['DB2_ODBC'])>0) {
		if(!odbc_connect($_POST['DB2_ODBC'],$_POST['DB2_USER'],$_POST['DB2_PWD'])) { $odbc2 = 0; $message .= 'Erreur dans les paramètres du connecteur ODBC de DOFIN<br>'; } else { $odbc2 = 1; odbc_close(); }
	} else { $odbc2 = 1; }
     $conn = ftp_connect($_POST['FTP_HOST'],$_POST['FTP_PORT']);
	 if(!conn) { $ftp = 0; $message .= 'Erreur dans les paramètres de connection FTP<br>'; } 
		else { 
			if(!ftp_login($conn,$_POST['FTP_USER'],$_POST['FTP_PWD'])) {
				$ftp = 0;			
				$message .= 'Erreur d\'authentification FTP<br>';
			} else {
				$ftp = 1;
				ftp_close($conn);	 			
			}
		}
	$message .= '</b></font>';

Open in new window


Info is fetched from this html code:
<html>

<head>
  <title>Installation</title>  
</head>

<body topmargin=20 leftmargin=20 onload="F1.DB1_ODBC.focus()">

	<FORM NAME="F1" ACTION="install.php" method="POST">

		<input type="hidden" name="MODE" value="<?php echo $_POST['MODE']; ?>">
	
		<h1>Installation du logiciel</h1>

		<hr>

		<?php if($_POST['MODE']=='ODBC') { ?>

			<?php if(isset($message)) { echo '<br>' . $message . '<br><hr>'; } ?>

				<table border="0" width="100%">
					<tr>
						<td width="50%"><br>
			
							<h3>Paramètres ODBC du logiciel</h3>
							
								<table border="0">
									<tr>
										<td width="200"><label for="DB1_ODBC">Nom du connecteur ODBC</label></td>
										<td><input name="DB1_ODBC" type="text" size="25" value="<?php echo $_POST['DB1_ODBC']; ?>"></td>
									</tr>
									<tr>									
										<td><label for="DB1_USER">Utilisateur</label></td>
										<td><input name="DB1_USER" type="text" size="25" value="<?php echo $_POST['DB1_USER']; ?>"></td>
									</tr>
									<tr>
										<td><label for="DB1_PWD">Mot de passe</label></td>
										<td><input name="DB1_PWD" type="text" size="25" value="<?php echo $_POST['DB1_PWD']; ?>"></td>
									</tr>
								</table>	
						</td>
						<td width="50%"><br>						
							
							<h3>Paramètres ODBC de DOFIN (facultatif)</h3>
							
								<table border="0">
									<tr>
										<td width="200"><label for="DB2_ODBC">Nom du connecteur ODBC</label></td>
										<td><input name="DB2_ODBC" type="text" size="25" value="<?php echo $_POST['DB2_ODBC']; ?>"></td>
									</tr>
									<tr>									
										<td><label for="DB2_USER">Utilisateur</label></td>
										<td><input name="DB2_USER" type="text" size="25" value="<?php echo $_POST['DB2_USER']; ?>"></td>
									</tr>
									<tr>
										<td><label for="DB2_PWD">Mot de passe</label></td>
										<td><input name="DB2_PWD" type="text" size="25" value="<?php echo $_POST['DB2_PWD']; ?>"></td>
									</tr>
								</table>								

						</td>
					</tr>
				</table><br>	
			
			<hr>

				<table border="0" width="100%">
					<tr>
						<td width="100%"><br>			
			
							<h3>Paramètres FTP</h3>

								<table border="0">
									<tr>
										<td width="200"><label for="FTP_HOST">Nom de l'hôte FTP</label></td>
										<td><input name="FTP_HOST" type="text" size="25" value="<?php echo $_POST['FTP_HOST']; ?>"></td>
									</tr>
									<tr>									
										<td><label for="FTP_PORT">Numéro du port</label></td>
										<td><input name="FTP_PORT" type="text" size="10" value="<?php echo $_POST['FTP_PORT']; ?>"></td>
									</tr>									
									<tr>									
										<td><label for="FTP_USER">Utilisateur (Licence)</label></td>
										<td><input name="FTP_USER" type="text" size="25" value="<?php echo $_POST['FTP_USER']; ?>"></td>
									</tr>
									<tr>
										<td><label for="FTP_PWD">Mot de passe</label></td>
										<td><input name="FTP_PWD" type="text" size="25" value="<?php echo $_POST['FTP_PWD']; ?>"></td>
									</tr>
								</table>															

						</td>
					</tr>
				</table><br>

			<hr>

			<br><br>	
			<input type="submit" name="ACTION" value="Soumettre">
			&nbsp;&nbsp;
			<input type="submit" name="ACTION" value="Passer">			
		
		<?php } elseif($_POST['MODE']=='INSTALL') { ?>		
			
			<h2>Les tests ont été effectués avec succès !</h2>
			
			<br><br>

			<h4>	
				L'installation peut prendre quelques minutes. 
				<br><br>
				Veuillez ne rien toucher avant l'affichage de la page de confirmation.
			</h4>
			
			<br><br>			
			
			<input type="checkbox" name="DROP">&nbsp;Exécuter un DROP TABLE avant la création<br> 
			<input type="checkbox" name="ADD" CHECKED>&nbsp;Créer les tables<br> 			
			<input type="checkbox" name="DATA" CHECKED>&nbsp;Insérer les données de base	

			<br><br>									
			
			<input type="checkbox" name="COPY" CHECKED>&nbsp;Transférer les fichiers 			
			
			<br><br>						

			<input type="radio" name="SQL" VALUE="1">&nbsp;Microsoft SQL<br> 
			<input type="radio" name="SQL" VALUE="2" CHECKED>&nbsp;MySQL 
			
			<br><br><br><br>						
			
			<input type="submit" name="ACTION" value="Installer">
		
		<?php } elseif($_POST['MODE']=='FIN') { ?>		
			
			<h3>INSTALLATION TERMINÉE !</h3>
			
			<br><br><br>
			
			<a href="index.html">Cliquez ici pour utiliser le logiciel de gestion de dossiers</a>
			
		<?php } ?>			

	</FORM>

</body>
</html>

Open in new window

Is there something I change in the code for the connection to work?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.