Trying to connect to myql - No database selected error
HI Folks,
The code below keeps generating the error, I have a wordpress site using the same db which connects fine using exactly the same credtials. Any ideas folks?
Many thanks
PHP Code:
<?php
require("config.php");
$link = mysql_connect("$database_host","$database_user","$database_pass") or die(mysql_error());
mysql_select_db($database_name);
$query = "SELECT id,title,start, DATE_FORMAT(start, '%Y-%m-%d %H:%i' ) AS startDate, end, url FROM events ORDER BY startDate DESC";
$result = mysql_query($query) or die(mysql_error());
$arr = array();
while($row = mysql_fetch_assoc($result)){
$arr[] = $row;
}
echo json_encode($arr);
?>
What's the exact error message? Also, are you sure $database_name is defined and has a non-empty value? Finally, you never check to see if mysql_select_db() was successful.
WordPress unfortunately is built on the MySQL extension.
One of the primary reasons why I hate wordpress and avoid if it possible.
Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail I'd rather be a comma, then a full stop. User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning
One of the primary reasons why I hate wordpress and avoid if it possible.
To be fair it's far too popular to just switch. They just made the minimum version of PHP version 5.0 this summer I believe. Let's face it, people just don't upgrade.
True, but if you throw enough warnings in their face ahead of time, when it comes time that change is no longer optional (e.g. the 'mysql' extension gets fully deprecated and tossed in the trash), you can just step back, smile sweetly, and shrug your shoulders saying "I told you so!"
The problem? No variable interpolation in the select_db call. [edit]: which was what brad grafleman was telling you [/edit]
You're welcome.
/!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade!/!!!\ ereg() is deprecated --- don't use it!
dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket
True, but if you throw enough warnings in their face ahead of time, when it comes time that change is no longer optional (e.g. the 'mysql' extension gets fully deprecated and tossed in the trash), you can just step back, smile sweetly, and shrug your shoulders saying "I told you so!"
Heh, if only it were that easy.
Reading the back and forth on php.net between the devs about deprecating and removing the extension in future versions was pretty interesting. I'm not quite sure what the best approach is to removing it. I think putting up warnings/notices and notices in the manual (like they have now) is the best approach for the time being. Not sure when the best time is to remove it entirely, perhaps the next major version, 6.0. I would modify the warnings/notices to say that the extension will be removed in version 6.0 rather than just saying it will be removed in a future version.
But considering when 6.0 is bound to be released, I'd actually claim that saying "in 6.0" or in "future version" is in an equally unprecise and distant future.
But fear not: there's always a chance they won't port the mysql extension to quantum computers when those hit the market - and then we won't have to wait for 6.0 to stop supporting mysql_
But considering when 6.0 is bound to be released, I'd actually claim that saying "in 6.0" or in "future version" is in an equally unprecise and distant future.
The difference is though PHP versions are increasing, even if slowly. So while it is an imprecise future, version 6.0 is coming closer with every new release.
Bookmarks