Jump to content

[SOLVED] Include problems


Daegalus

Recommended Posts

ok well i fixed my old problem somehow, but now i cant get the fucntions file to get the proper mysql info from teh config file.

 

[phpBB Debug] PHP Notice: in file /home/vol2/manusmut/public_html/includes/functions.php on line 3: mysql_connect() [function.mysql-connect]: Access denied for user 'manusmut'@'localhost' (using password: NO)
Unable to connect to MySQL

Ive tried so many include combinations but none work.

here is the funcitons file:

<?php
function get_news() {
$dbh = mysql_connect($mysql_server, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db($database,$dbh) or die("Could not select " + $database);

$query = "SELECT * FROM news ORDER BY date LIMIT $maxnewsitems";
$result = mysql_query($query);

$newsarray = array();

while($row = mysql_fetch_array($result))
{
	$newsarray[] = array("id" => $row['id'],
			     "title" => $row['title'],
			     "author" => $row['author'],
			     "author_id" => $row['author_id'],
			     "body" => $row['body'],
			     "date" => $row['date']);
}

return $newsarray;
}

 

header file

 

<?php
define('IN_PHPBB', true);
define('PHPBB_ROOT_PATH', '../forum/');
$phpbb_root_path = PHPBB_ROOT_PATH;
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Begin site header
include_once '../config.php';
include_once '../includes/functions.php';
?>

 

and the config file jsut has the database info variables. and the $maxnewsitems variable.

Link to comment
https://forums.phpfreaks.com/topic/99781-solved-include-problems/
Share on other sites

your problem is variable scope.... you can't access global variables inside functions unless declaring them in the function or passing them to the function. so you could either do this....

 

(passing vars)

function get_news($mysql_server, $username, $password, $database) {...

 

and call the function like...

 

get_news($mysql_server, $username, $password, $database);

 

or this

 

(declaring globals)

function get_news() {

  global $mysql_server, $username, $password, $database;

...

 

Oh wow, i feel like a noob. Anyways all fixed up but now I get a mysql problem

 

[phpBB Debug] PHP Notice: in file /home/vol2/manusmut/public_html/includes/functions.php on line 12: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

 

I know what it means, just i dont get how my result is not a valid mysql result resource. Oo its how i do all my mysql connections and they worked before.

It's probably down to $maxnewsitems not returning a integer.  Could be another problem with variable scope.

 

Yup I noticed that like 3 min before checking back here. I forgot to add it to the global line along with the other variables. Thanks for the help guys it all works nicely now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.