doddsey_65 Posted December 18, 2010 Share Posted December 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/ Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 Can you post up the connect file (init.php). Just replace your username and password with something generic if you want. Denno Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148941 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 $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(); } Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148942 Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148943 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148944 Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 Fair enough then.. Have you tried the procedural type though, just to try and cross off the connection as the problem? Denno Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148945 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148947 Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 which is line 239? Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148948 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 233 now(changed code) $query = mysqli_query($db, " // this is the line in question SELECT $select FROM ".TBL_PREFIX."$table $where $order $limit ") or die(mysqli_error($db)); Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148949 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148950 Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148951 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 same error with raw values Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148952 Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148954 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 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)); Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148956 Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 Do you still get that Fatal error then? Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148957 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 yes Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148958 Share on other sites More sharing options...
denno020 Posted December 18, 2010 Share Posted December 18, 2010 I don't know then sorry.. Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148961 Share on other sites More sharing options...
doddsey_65 Posted December 18, 2010 Author Share Posted December 18, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/222048-functions/#findComment-1148967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.