Jump to content

dude i suck, this freaken oop stuff stupid


Jarod

Recommended Posts

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'); ?>

Link to comment
Share on other sites

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;
   }
}
?>

Link to comment
Share on other sites

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'); ?>

Link to comment
Share on other sites

<?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);
   }
}
?>

Link to comment
Share on other sites

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  :-\.

Link to comment
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.