Jump to content

Recommended Posts

I have a function to get some data from the database. However it always comes back with:

 

Notice: Undefined variable: db in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 233

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 233

 

here is the code:

 

require_once(ASF_ROOT."includes/init.php"); // this is where the db connection is

function get_db_info($type, $select, $table, $where="", $order="DESC", $limit="")
{
if (empty($type) || empty($select) || empty($table))
{
	try
	{
		throw new emptyArgs();
	}
	catch (emptyArgs $invalid)
	{
		echo "<div style=\"border:1px solid #f00;
							padding:5px;
							width:350px;
							line-height:10px;\">";

		echo "<b>$invalid</b>";
		echo "<p>get_info() needs atleast 3 arguments (Type, Select and Table) - none supplied</p>";
		echo "</div>";
	}
}
else
{
	if ($type == "num")
	{
		$query = $db->query("
			SELECT $select 
			FROM ".TBL_PREFIX."$table
			$where
			$order
			$limit
			") or die($db->error());
		$result = $db->num_rows(MYSQLI_NUM);
		return $result;
	}
	else
	{
		$query = $db->query("
			SELECT $select 
			FROM ".TBL_PREFIX."$table
			$where
			$order
			$limit
			");
		$result = $query->fetch_array(MYSQL_ASSOC);
		return $result;
	}

}
}

 

I have a function for mysqli_real_escape_string above this function and it works with no errors.

 

Have i done something wrong? as you can see i am including the file which holds the connection but it doesnt seem to use it. every other file that uses the connection works fine.

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/
Share on other sites

$db = new mysqli($config['database']['hostname'], $config['database']['username'], $config['database']['password'], $config['database']['database']);
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148942
Share on other sites

I'm not sure what sort of format you're using to connect to your database there?

 

To connect to my database I use this format:

$myConnection = mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name");

 

But also, if you want to use mysqli, I have used:

$myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");

 

Try one of those formats instead?

 

Denno

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148943
Share on other sites

$config[][] refers to values in a config file which holds the connection info(user, pass etc)

 

I am using the OO mysqli connection. the one you have listed is the procedural connection type.

 

they do the same thing

 

either way this function isnt recognising $db and i dont know why since i have included the file.

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148944
Share on other sites

tried the procedural:

 

$db = mysqli_connect($config['database']['hostname'], $config['database']['username'], $config['database']['password'], $config['database']['database']);

 

and the query:

 

$query = mysqli_query("
			SELECT $select 
			FROM ".TBL_PREFIX."$table
			$where
			$order
			$limit
			", $db) or die(mysqli_error($db));

 

but it comes up with the same error:

 

Notice: Undefined variable: db in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 239

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148947
Share on other sites

when i add a connection in that function if tells me $config isnt recognized which means the files in require_once arent being used in this function. Why not?

 

this function (in the same file) works fine:

 

function asf_escape($string)
{
if ($string === null)
{
	return "";
}

$string = mysqli_real_escape_string($string);
return $string;
}

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148950
Share on other sites

So I guess you've settled on

$db = mysqli_connect($config['database']['hostname'], $config['database']['username'], $config['database']['password'], $config['database']['database']);

being the problem?

 

Can you take out the whole config thing, and just put in the raw values?

 

Other than that, I would be pretty much out of ideas..

 

Denno

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148951
Share on other sites

$query = $db->query("
			SELECT $select (etc etc)

 

Doesn't this '->' apply to objects? Is your database connection an object? Your second error:

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 233

Appears to be hinting at this, I think..

 

Denno

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148954
Share on other sites

that was the way the query looked when i was using the object oriented mysqli connection. now i am using procedural it looks like this:

 

$query = mysqli_query($db, " // this is the line in question
SELECT $select 
FROM ".TBL_PREFIX."$table
$where	
$order
$limit
") or die(mysqli_error($db));

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148956
Share on other sites

it has to be something to do with the require_once as i am getting it everywhere now.

 

index.php requires header.php which requires config.php

 

config.php has the variables for connection.(host, user, pass etc)

header.php sets up the connection and connects

and index.php runs a query

 

but i am getting:

 

Notice: Undefined variable: link

 

everywhere i run a query.

 

config.php:

 

$config['database']['database'] = 'database';
$config['database']['hostname'] = 'localhost';
$config['database']['username'] = 'user';
$config['database']['password'] = 'password';

 

header.php

 

require_once("config/config.php");

$link = mysqli_connect($config['database']['hostname'], $config['database']['username'], $config['database']['password'], $config['database']['database']);

if (mysqli_connect_errno()) 
{
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

 

index.php:

 

require_once("includes/header.php");

$query = mysqli_query($link, "SELECT
              f.forum_id, f.forum_name, f.forum_description, 
		  f.forum_topics, f.forum_posts,
              f.forum_last_poster, f.forum_last_post_time, f.forum_last_post,
		  f.forum_cat_name, f.forum_cat_id, f.forum_image,
              c.cat_id, c.cat_name, 
              m.user_id, m.user_username, m.user_group,
		  m.user_date_format, m.user_description_display,
              t.topic_id, t.topic_name
		  
		FROM ".TBL_PREFIX."categories as c

		JOIN ".TBL_PREFIX."forums as f
			ON f.forum_cat_id = c.cat_id

		LEFT JOIN ".TBL_PREFIX."members as m
			ON f.forum_last_poster = m.user_username

		LEFT JOIN ".TBL_PREFIX."topics as t
			ON t.topic_name = f.forum_last_post

		ORDER BY c.cat_id, f.forum_id ASC")
		or trigger_error("SQL", E_USER_ERROR);

// Loop through records to process data
$catData = array('cat_id' => false);

while ($forum_info = mysqli_fetch_object($query))

 

this query fails for no apparent reason!

Link to comment
https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148967
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.