clown[NOR] Posted April 13, 2007 Share Posted April 13, 2007 i have been fighting this problem for a while now, but with no luck.. i just cant see why it wont work... it might be the function... it's the first time i'm working with it... so.. this is the error message i get: Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in F:\root\oleaa.com\index.php on line 21 Unable to connect to DB this is the code: <?php include('config/db.php'); function freshNews() { if (!mysql_connect($dbHost, $dbUser, $dbPass)) { die("Unable to connect to DB"); } //This is line 21 if (!mysql_select_db($dbName)) { die("Unable to select DB"); } $query = "SELECT * FROM news"; $result = mysql_query($query); if (!$result) { die("Could not run query from DB"); } $dbNumRows = mysql_num_rows($result); if ($dbNumRows > 0) { echo $dbNumRows . " news found."; } else { echo "No news found."; } } Thanks In Advance - Clown Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/ Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Author Share Posted April 13, 2007 ok.. i figured it out... i had to move the include() inside the function... but... is there a way so I dont have to do that? Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/#findComment-228690 Share on other sites More sharing options...
taith Posted April 13, 2007 Share Posted April 13, 2007 not sure why it needs to be in the function for that... shouldnt need to be... but if you include_once(); you can put it in either/both spots... problem solved :-) Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/#findComment-228691 Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Author Share Posted April 13, 2007 hehe... oh well... i tried to add it as include_once() outside the function ... didnt work.. if that's what you ment tho... oh well.. it's jsut the db.php... so it's ok... but if anyone have an solution for this.. please let me know... Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/#findComment-228693 Share on other sites More sharing options...
Guest prozente Posted April 13, 2007 Share Posted April 13, 2007 http://us2.php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/#findComment-228702 Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Author Share Posted April 13, 2007 this is the db file.. tried to make the variables global... but it didn't work <?php global $dbHost, $dbUser, $dbPass, $dbName; $dbHost = "localhost"; $dbUser = "root"; $dbPass = ""; $dbName = "oleaa"; ?> Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/#findComment-228719 Share on other sites More sharing options...
Guest prozente Posted April 13, 2007 Share Posted April 13, 2007 put global $dbHost, $dbUser, $dbPass, $dbName; in your function before you use the variables Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/#findComment-228726 Share on other sites More sharing options...
clown[NOR] Posted April 13, 2007 Author Share Posted April 13, 2007 aaahhh... *feeling stupid* haha... thanks m8 Link to comment https://forums.phpfreaks.com/topic/46906-solved-include-and-function/#findComment-228746 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.