Link to home
Start Free TrialLog in
Avatar of alicia1234
alicia1234Flag for United States of America

asked on

"Data source name not found" trying to connect to DSN - using MySQL and PHP

I have lots of experience using MySQL and DSN's with ColdFusion but this is my first attempt to do it with PHP. In ColdFusion, I created the datasource using the ColdFusion MX administrator. I wasn't sure how to do it for PHP, so I relied upon Dreamweaver 8. In Dreamweaver, I created a MySQL Connection called "connWidgets" and it tested successfully.

In PHP, if I connect to my database directly, like this:
<?php
$connection = mysql_connect("localhost", "root", "mypassword");
?>
it works fine. I can issue queries and get stuff out of the database.

But when I try to connect using the DSN, with this statement:
$connection =  odbc_connect("connWidgets","root","mypassword");

I get this error:
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\Inetpub\wwwroot\meatormotion\php_sandbox\db_DSN.php on line 3

My development environment is Windows XP SP3 and I'm running IIS as my webserver.

If I go to the Control Panel > Administrative Tools > Data Sources (ODBC)
and look on the System DSN and File DSN tabs, there is nothing there. (I don't know if that matters or not?)

Help!

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore 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
Avatar of alicia1234

ASKER

I thought that I might need to and indeed I did try ... but there is no driver there for MySQL, and the only driver I see that mentions ODBC is "Microsoft ODBC for Oracle".
So what driver am I supposed to use? OR ... where do I get the appropriate driver and how do I "install" it so it shows up in the list of drivers?
I do have MySQL installed on my PC. As I mentioned, accessing the database directly works just fine.
Thanks.
SOLUTION
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
Yes, ushastry is right, you need to install the MySQL OCBC driver.

for lower version of MySQL, you may need this:
http://dev.mysql.com/downloads/connector/odbc/3.51.html#win32
Thanks. I downloaded the 5.1 connector and installed it, then created the datasource through CP > Administrative Tools > Data Sources.
My odbc_connect now works:   $connection =  odbc_connect("connWidgets","root","mypassword");
So now instead of this (which I used for the direct connection):
$result = mysql_query("
  SELECT menu_name, position
  FROM subjects
 ", $connection);
I am trying this:
$result = odbc_execute("
  SELECT menu_name, position
  FROM subjects
 ");
And I'm getting this error:
Warning: odbc_execute(): supplied argument is not a valid ODBC result resource in C:\Inetpub\wwwroot\meatormotion\php_sandbox\db_DSN.php
So once I've set up the datasource and connected to it, how do I query the database? Thanks.
 
Nevermind my last post. I figured out what I needed to do. See code snippet below. Still have one more piece that needs solving ... see next post.
 

$connection =  odbc_connect("connWidgets","","");	
	
$sql_statement = odbc_prepare($connection,"
	SELECT menu_name, position
	FROM subjects
	");
	
$result = odbc_execute($sql_statement);
	

Open in new window

So now that I have $result:
Previously, when I got $result through a direct connection and used "mysql_query" to get it, I then used "mysql_fetch_array" to access the result.
What's the equivalent of "mysql_fetch_array" for ODBC? And where can I find these ODBC functions?
Thanks.
 
Here's the code for how I did it before (with direct connect and mysql_ commands)

<?php
	//Use returned data
	while ($row = mysql_fetch_array($result)) {
	echo $row["menu_name"]." ".$row["position"]."<br />";
	}
?>

Open in new window

SOLUTION
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. I actually had already tried that (see code below). I get this error:
Warning: odbc_fetch_array(): supplied argument is not a valid ODBC result resource  

<?php
	//Use returned data
	while ($row = odbc_fetch_array($result)) {
	echo $row["menu_name"]." ".$row["position"]."<br />";
	}
?>

Open in new window

you may add this ?
<?php
if (!function_exists('odbc_fetch_array')) {
    function odbc_fetch_array($result, $rownumber=null) {
        $array = array();
        if (!($cols = odbc_fetch_into($result, $result_array, $rownumber))) {
            return false;
        }
        for ($i = 1; $i <= $cols; $i++) {
            $array[odbc_field_name($result, $i)] = $result_array[$i - 1];
        }
        return $array;
    }
}
 
?>
 
please read more on the comments on that article and see if that helpful?

Open in new window

Yes, I did read that comment. They are actually defining the function if it does not exist. In my case, I'm pretty sure the function already exists because the error is complaining about an argument not being valid. It's not saying that the function does not exist.
I did figure out, though, that the problem was the odbc_execute returns a boolean, not a resource. I should have used odbc_exec instead.
So now everything works, and I know where to find the odbc functions. Thanks!
Below is all of the code that I used to make the connection, execute a query, and use the result, just to have all the correct code in one place.
 

<?php
//Create a connection
$connection =  odbc_connect("connWidgets","","");
if (!$connection) {
     die("Database connection failed: " . mysql_error());
}
	
//Execute the query
$result = odbc_exec($connection, "
	SELECT menu_name, position
	FROM subjects
	");
 
//Use the returned data
while ($row = odbc_fetch_array($result)) {
	echo $row["menu_name"]." ".$row["position"]."<br />";
	}		
?>

Open in new window

hey, your codes seems working fine for me...
Yes, it does work fine. I posted it as the "final solution". ;-)