Jump to content

SOLVED: Need help!


Procode

Recommended Posts

Sorry if it's noobish but i am just learning classes and i just can't get this fixed.

Basicly i wrote a mysql class and now have hell of errors saying it does not let me connect to the database, is it me or am i doing something wrong. I use WAMP so i use root for username... no password, localhost and db name is correct as well.

Here is the MySQL class.(part of it)

[code]class mysql {

//MySQL var

var $mysql = array();

//MySQL settings.

function settings(){
//MySQL settings.
$this->mysql['host'] = $mysql['host'];
$this->mysql['user'] = $mysql['user'];
$this->mysql['pass'] = $mysql['pass'];
$this->mysql['dbname'] = $mysql['dbname'];
//MySQL errors.
$this -> mysql['con_error'] = "<b>MySQL encountered a severe error while trying to connect to the host</b>n";
        $this -> mysql['db_error'] = "<b>MySQL encountered a severe error while trying to use the database</b>n";
        $this -> mysql['gen_error'] = "<b>MySQL Error: <b>Due to a database error this page cannot be displayed</b>n";
}

//MySQL connect function.

function do_connect(){

$this->settings();

$connect = mysql_connect($this->mysql['host'], $this->mysql['user'], $this->mysql['pass']);
$db = mysql_select_db($this->mysql['dbname']);

if(!$connect){
echo "<br>";
echo $this->mysql['con_error'];
}elseif(!$db){
echo "<br>";
echo $this->mysql['db_error'];
}
}

//MySQL query function

function sql_run($sql){
$this->do_connect();
$res = mysql_query($sql);
if(!$res){
echo "<br>";
echo $this->mysql['db_error'];
}else{
return($res);
}
}

//MySQL mysq_num_rows function.

function num_rows($sql){
$this->do_connect();
$now = $this->sql_run($sql);
$res = mysql_num_rows($now);
if(!$res){
echo "<br>";
echo $this->mysql['db_error'];
}else{
return $res;
}
}[/code]

Please help, what am i doing wrong?
Link to comment
https://forums.phpfreaks.com/topic/28531-solved-need-help/
Share on other sites

Your settings method appears to be the problem:
[code]function settings(){
//MySQL settings.
$this->mysql['host'] = $mysql['host'];
$this->mysql['user'] = $mysql['user'];
$this->mysql['pass'] = $mysql['pass'];
$this->mysql['dbname'] = $mysql['dbname'];
//MySQL errors.
$this -> mysql['con_error'] = "<b>MySQL encountered a severe error while trying to connect to the host</b>n";
        $this -> mysql['db_error'] = "<b>MySQL encountered a severe error while trying to use the database</b>n";
        $this -> mysql['gen_error'] = "<b>MySQL Error: <b>Due to a database error this page cannot be displayed</b>n";
}[/code]
You are setting up the mysql class variable with another array called mysql which doesn't exist. Where does the mysql array come from ($mysql). What would be better is to use a constructor instead of the settings function.

The constructor is a function which will automatically be called by PHP when you initiate the class. What you'll want to do is pass the host, username, password and database name through to this function. You do this when initiating the class like so:
[code=php:0]$mysqlC = new mysql('host', 'username', 'password', 'database_name');[/code]

That will now setup the mysql class variable. Now all you need to do is call the do_connect function. Heres your improved class:
[code]class mysql {

//MySQL var

var $mysql = array();

    // The constuctor! YOu'll notice this has the same name as the class.
    function mysql($host, $user, $pass, $dbname)
    {
        //MySQL settings.
$this->mysql['host'] = $host;
$this->mysql['user'] = $user;
$this->mysql['pass'] = $pass;
$this->mysql['dbname'] = $dbname;

//MySQL errors.
$this -> mysql['con_error'] = "<b>MySQL encountered a severe error while trying to connect to the host</b>n";
        $this -> mysql['db_error'] = "<b>MySQL encountered a severe error while trying to use the database</b>n";
        $this -> mysql['gen_error'] = "<b>MySQL Error: <b>Due to a database error this page cannot be displayed</b>n";
    }

//MySQL connect function.

function do_connect()
    {
$connect = mysql_connect($this->mysql['host'], $this->mysql['user'], $this->mysql['pass']);
$db = mysql_select_db($this->mysql['dbname']);

if(!$connect){
echo "<br>";
echo $this->mysql['con_error'];
}elseif(!$db){
echo "<br>";
echo $this->mysql['db_error'];
}
}

//MySQL query function

function sql_run($sql)
        {
$this->do_connect();
$res = mysql_query($sql);
if(!$res){
echo "<br>";
echo $this->mysql['db_error'];
}else{
return($res);
}
}

//MySQL mysq_num_rows function.

function num_rows($sql)
        {
$this->do_connect();
$now = $this->sql_run($sql);
$res = mysql_num_rows($now);
if(!$res){
echo "<br>";
echo $this->mysql['db_error'];
}else{
return $res;
}
}
}

$mysqlC = new mysql('localhost', 'root', '', 'mysql');

$mysqlC->do_connect();[/code]
Link to comment
https://forums.phpfreaks.com/topic/28531-solved-need-help/#findComment-130564
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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