Jarod Posted November 23, 2009 Share Posted November 23, 2009 Okay this is how all this started, I literally thought to the best ways around this solution and they failed, so I was like I'mma just start from ground up, which is what I'm supposedly doing right now, but these freaken errors wont go way for anything in the world, wtf man ! I cant get this freaken oop project to work for a thing in the world wtf , last one I did it literally printed out like 1K queries of the same usernames, which like crashed my browser HEAVILY. So I rewrote the script again and messed up and then again and messed up AGAIN... So on this chance I rewrote it again in the best way I could this time and these fucking bizarre errors are like hunting me man wtf , im frustrating over here right now . I get this error: Warning: mysql_query() expects parameter 2 to be resource, null given in C:\wamp\www\inc\lib\class.dbConnector.php on line 16 Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\inc\lib\class.dbConnector.php on line 20 Okay here is the code I used to start myself off with, before I get to showing that I'll explain the purpose of these codes though. The first script you'll see is an object that sets up the connection for using the database, and the last one is where I make custom functions for how the site will be accessing future queries, obviously that is not working because of my gay vista laptop I guess :'(. The source where I implemented the 2 codes together is the last block of code you'll see. Please I reallllllyyyyyyyy neeeeed help! *pulls hear out* <?php class dbComponent { var $dbhost; var $dbusername; var $dbpassword; var $dbname; /** * Creates the database on construct */ function connectDB($host, $username, $password, $name) { $this->dbhost = $host; $this->dbusername = $username; $this->dbpassword = $password; $this->dbname = $name; mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword); mysql_select_db($this->dbname); } function get_host() { return $this->dbhost; } function get_username() { return $this->dbusername; } function get_password() { return $this->dbpassword; } function get_name() { return $this->dbname; } } ?> <?php require_once( dirname(__FILE__) . '/class.dbComponent.php'); class dbConnector extends dbComponent { var $query; var $thelink; function __construct() { $dbComponent = new dbComponent(); $this->thelink = $dbComponent->connectDB('localhost', 'root', NULL, 'jv_cms'); mysql_select_db($dbComponent->get_name()); } function query($q) { $this->query = $q; return mysql_query($this->query, $this->thelink); } function fetch_array($result) { return mysql_fetch_array($result); } function close() { mysql_close($this->thelink); } } ?> <?php include_once(dirname(__FILE__) . '/inc/lib/class.dbConnector.php'); $db = new dbConnector(); require_once('inc/header.php'); require_once('inc/sidebar.php'); ?> <div id="entries"> <div class="entry"> <!-- Entry #1 --> <div class="entry_name">This is an entry</div> <div class="entry_content"> <p> <?php $query = $db->query('SELECT users_username FROM jays_users'); $row = $db->fetch_array($query); echo $row['users_username']; ?></p> </div> </div> <div id="browse_entries"><a href="#" title="Browse news archive">Browse Archive</a></div> </div> <?php require_once('inc/footer.php'); ?> Quote Link to comment Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 Try using this for DB component class. <?php class dbComponent { var $dbhost; var $dbusername; var $dbpassword; var $dbname; /** * Creates the database on construct */ function connectDB($host, $username, $password, $name) { $this->dbhost = $host; $this->dbusername = $username; $this->dbpassword = $password; $this->dbname = $name; mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword) or die mysql_error(); mysql_select_db($this->dbname); } function get_host() { return $this->dbhost; } function get_username() { return $this->dbusername; } function get_password() { return $this->dbpassword; } function get_name() { return $this->dbname; } } ?> Quote Link to comment Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 might want to change this too. mysql_select_db($this->dbname); to mysql_select_db($this->dbname) or die mysql_error(); Quote Link to comment Share on other sites More sharing options...
Jarod Posted November 23, 2009 Author Share Posted November 23, 2009 I fixed it a little, it's displaying but the error still wont go away :'(, it says somethings wrong with connecting to the database when it's displaying data from the database, see what I mean? These errors are hunting me! <?php class dbComponent { var $dbhost; var $dbusername; var $dbpassword; var $dbname; /** * Creates the database on construct */ function connectDB($host, $username, $password, $name) { $this->dbhost = $host; $this->dbusername = $username; $this->dbpassword = $password; $this->dbname = $name; $connect = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword) or die('Details provided to connect to the database are invalid.' . mysql_error()); mysql_select_db($this->dbname, $connect) or die('Could not select the database. ' . mysql_error()); } function get_host() { return $this->dbhost; } function get_username() { return $this->dbusername; } function get_password() { return $this->dbpassword; } function get_name() { return $this->dbname; } } ?> <?php require_once( dirname(__FILE__) . '/class.dbComponent.php'); class dbConnector extends dbComponent { var $query; var $thelink; function __construct() { $dbComponent = new dbComponent(); $this->thelink = $dbComponent->connectDB('localhost', 'root', NULL, 'jv_cms') or die('Details provided to connect to the database are invalid.' . mysql_error()); mysql_select_db($dbComponent->get_name(), $this->thelink) or die('Could not select the database. ' . mysql_error()); } function query($q) { $this->query = $q; return mysql_query($this->query); } function fetch_array($result) { return mysql_fetch_array($result); } function close() { mysql_close($this->thelink); } } ?> <?php include_once(dirname(__FILE__) . '/inc/lib/class.dbConnector.php'); $db = new dbConnector(); require_once('inc/header.php'); require_once('inc/sidebar.php'); ?> <div id="entries"> <div class="entry"> <!-- Entry #1 --> <div class="entry_name">This is an entry</div> <div class="entry_content"> <p> <?php $query = $db->query('SELECT users_username FROM jays_users'); $row = $db->fetch_array($query); echo $row['users_username']; ?></p> </div> </div> <div id="browse_entries"><a href="#" title="Browse news archive">Browse Archive</a></div> </div> <?php require_once('inc/footer.php'); ?> Quote Link to comment Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 user "or die mysql_error()" with all your throughput from your database. Quote Link to comment Share on other sites More sharing options...
Jarod Posted November 23, 2009 Author Share Posted November 23, 2009 Say that again, i'm not understanding Quote Link to comment Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 <?php require_once( dirname(__FILE__) . '/class.dbComponent.php'); class dbConnector extends dbComponent { var $query; var $thelink; function __construct() { $dbComponent = new dbComponent(); $this->thelink = $dbComponent->connectDB('localhost', 'root', NULL, 'jv_cms') or die('Details provided to connect to the database are invalid.' . mysql_error()); mysql_select_db($dbComponent->get_name(), $this->thelink) or die('Could not select the database. ' . mysql_error()); } function query($q) { $this->query = $q; return mysql_query($this->query)or die mysql_error(); } function fetch_array($result) { return mysql_fetch_array($result) or die mysql_error(); } function close() { mysql_close($this->thelink); } } ?> Quote Link to comment Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 when things don't work out "or die mysql_error()" sheds a little light on why your not getting what you expect from your mysql database Quote Link to comment Share on other sites More sharing options...
Jarod Posted November 23, 2009 Author Share Posted November 23, 2009 It just prints out Details provided to connect to the database are invalid. now, it's still not working. I know for sure it's connecting BECAUSE when I didn't have the die() it was outputting all the users from the database, so I know for sure something else is wrong with the database. I'm just not sure "what" is wrong though :-\. Quote Link to comment Share on other sites More sharing options...
Jarod Posted November 23, 2009 Author Share Posted November 23, 2009 Anyone help please :'(? Quote Link to comment Share on other sites More sharing options...
Jarod Posted November 23, 2009 Author Share Posted November 23, 2009 Omg no one cant help me, ffs :'( Quote Link to comment Share on other sites More sharing options...
trq Posted November 23, 2009 Share Posted November 23, 2009 I guess the first question I have is what exactly is the point of your classes? they don't add any functionality to the already existing mysql* functions. Quote Link to comment Share on other sites More sharing options...
dbillings Posted November 23, 2009 Share Posted November 23, 2009 I agree you're just adding complexity Quote Link to comment 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.