alicia1234
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","m ypassword" );
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\meatorm otion\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!
In PHP, if I connect to my database directly, like this:
<?php
$connection = mysql_connect("localhost",
?>
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"
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\meatorm
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
for lower version of MySQL, you may need this:
http://dev.mysql.com/downloads/connector/odbc/3.51.html#win32
ASKER
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","m ypassword" );
So now instead of this (which I used for the direct connection):
Warning: odbc_execute(): supplied argument is not a valid ODBC result resource in C:\Inetpub\wwwroot\meatorm otion\php_ sandbox\db _DSN.php
So once I've set up the datasource and connected to it, how do I query the database? Thanks.
My odbc_connect now works: $connection = odbc_connect("connWidgets"
So now instead of this (which I used for the direct connection):
$result = mysql_query("I am trying this:
SELECT menu_name, position
FROM subjects
", $connection);
$result = odbc_execute("And I'm getting this error:
SELECT menu_name, position
FROM subjects
");
Warning: odbc_execute(): supplied argument is not a valid ODBC result resource in C:\Inetpub\wwwroot\meatorm
So once I've set up the datasource and connected to it, how do I query the database? Thanks.
ASKER
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);
ASKER
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.
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.
ASKER
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 />";
}
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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 />";
}
?>
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?
ASKER
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.
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 />";
}
?>
hey, your codes seems working fine for me...
ASKER
Yes, it does work fine. I posted it as the "final solution". ;-)
ASKER
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.