Results 1 to 5 of 5
Thread: Problems With Querying the DB
-
06-16-2009, 05:27 PM #1New Member
- Join Date
- Jun 2009
- Posts
- 3
Problems With Querying the DB
I keep getting this error when trying to submit a form and query to add to the db:
Code:mysqli_query() expects parameter 1 to be mysqli, null given in
Code:require_once('../includes/dbconnect.php'); // Check for form submission if($_POST){ // Connect to db $connector = new dbconnect(); // Form validation goes here later // Run the query to add the content info to the db $insertQuery = "INSERT INTO content (title,section,content) VALUES (". "'".$_POST['title']."', ". $_POST['section'].", ". "'".$_POST['content']."')"; echo $insertQuery; if ($result = $connector->query($insertQuery)){ // Success echo '<p class="success">Content has been successfully added to the database.</p>'; }else{ // It hasn't worked so stop. Better error handling code would be good here! echo '<p class="error">MySQL Error: '.mysqli_error($dbc).'<br /><br />Query: '.$query.'</p>'; } } ?> <h1 class="legend">Add Content</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="content_data" id="content_data"> <fieldset class="fieldb"> <legend class="legend">Content Information</legend> <div class="row"> <label for="title">Title:</label> <input type="text" name="title" id="title" size="30" value="<?php if (isset($title)) echo $title; ?>" /> </div> <div class="row"> <label for="section">Section:</label> <input type="text" name="section" id="section" size="20" value="<?php if (isset($section)) echo $section; ?>"/> </div> <div class="row"> <label for="content">Content:</label> <textarea name="content" id="content" rows="30" cols="70"> </textarea> </div> <div class="legend"> <input type="submit" name="fm_submit" value="Submit" /> <input type="reset" name="fm_reset" value="Reset" /> </div> <input type="hidden" name="submitted" value="TRUE" /> </fieldset> </form> <?php require('../includes/footer.inc.php'); ?>
Code:<?php //////////////////////////////////////////////////////////////////////////////////////// // dbconnect.php: Setup and connection to the database /////////////////////////////////////////////////////////////////////////////////////// require_once 'dbvars.php'; class dbconnect extends dbvars { var $theQuery; var $dbc; function dbconnect(){ // Load settings from dbvars $settings = dbvars::getSettings(); $host = $settings['dbhost']; $db = $settings['dbname']; $user = $settings['dbusername']; $pass = $settings['dbpassword']; // Make the connection, using improved extension $dbc = mysqli_connect($host, $user, $pass) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); mysqli_select_db($dbc, $db); register_shutdown_function(array(&$this, 'close')); } // Execute a db query function query($query) { $theQuery = $query; return mysqli_query($dbc, $theQuery); } // Array the results function fetchArray($result) { return mysqli_fetch_array($result); } // Terminate the connection function close() { mysqli_free_result($result); mysqli_close($dbc); } } ?>
-
06-17-2009, 08:31 AM #2Newbie
- Join Date
- Apr 2009
- Posts
- 22
If you're trying to set class member variables, you need to use $this->my_var_name to set them.
i.e. this will not work:
<?php
class tester
{
protected $_var;
public function method1()
{
$_var = 2;
}
public function method2()
{
print('var is: ' . $_var);
}
}
$tester = new tester();
$tester->method1();
$tester->method2();
?>
But this will:
class tester
{
protected $_var;
public function method1()
{
$this->_var = 2;
}
public function method2()
{
print('var is: ' . $this->_var);
}
}
$tester = new tester();
$tester->method1();
$tester->method2();
Your $dbc variable should be $this->dbc and not just $dbc.
-
06-17-2009, 01:06 PM #3New Member
- Join Date
- Jun 2009
- Posts
- 3
Thanks for the reply.
Sadly, that's what I had before but I thought I screwed something up, so I changed it. I still had the same problem before and after, so the cause of the problem is something else.
Here's the new dbconnect:
PHP Code:<?php
////////////////////////////////////////////////////////////////////////////////////////
// dbconnect.php: Setup and connection to the database
///////////////////////////////////////////////////////////////////////////////////////
require_once 'dbvars.php';
class dbconnect extends dbvars {
var $theQuery;
var $dbc;
function dbconnect(){
// Load settings from dbvars
$settings = dbvars::getSettings();
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Make the connection, using improved extension
$this->dbc = mysqli_connect($host, $user, $pass) OR die
('Could not connect to MySQL: ' . mysqli_connect_error() );
mysqli_select_db($this->dbc, $db);
register_shutdown_function(array(&$this, 'close'));
}
// Execute a db query
function query($query) {
$this->theQuery = $query;
return mysqli_query($query, $this->dbc);
}
// Array the results
function fetchArray($result) {
return mysqli_fetch_array($result);
}
// Terminate the connection
function close() {
mysqli_free_result($result);
mysqli_close($this->dbc);
}
}
?>
Code:return mysqli_query($query, $this->dbc);
-
06-17-2009, 03:36 PM #4Junior Guru Wannabe
- Join Date
- May 2008
- Location
- Rutherford, NJ
- Posts
- 68
I noticed your error code just calls $dbc, not $connector->dbc. could cause errors.
also, hints:
1. take the stuff in ::close() and put it in the destructor. then get rid of register_shutdown_function.
2. the object property $theQuery seems to be entirely unused. it's assigned, but never referenced. get rid of it?Database design and performance optimization, custom PHP scripts, and publicly available resources for developers!
http://www.zeropride.com
-
06-17-2009, 06:08 PM #5New Member
- Join Date
- Jun 2009
- Posts
- 3
Thanks, those changes patched everything up. It seems I also somehow reversed the connection and query info for the mysqli_query function, so reversing them back after I made the changes you suggested made everything work like a charm.