TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   whats wrong in this code ? [i am learning]! (http://www.talkphp.com/absolute-beginners/3056-whats-wrong-code-i-am-learning.html)

codefreek 07-02-2008 06:15 PM

whats wrong in this code ? [i am learning]!
 
PHP Code:

<?php


class db_connect {

private 
$dbn;
private 
$user;
private 
$pass;

public function 
specs($dbn$user$pass) {

$dbtestcon mysql_connect('$dbn''$user''$pass');

if (!
$dbtestcon) {
    die(
'Could not connect: ' mysql_error());

echo 
'Connected successfully';



     }



}

?>


Tanax 07-02-2008 06:31 PM

You can't use ' around the variables in the mysql_connection

codefreek 07-02-2008 07:11 PM

so how would i fix that instead ?

delayedinsanity 07-02-2008 07:17 PM

Just to expand on that, when you're passing arguments, you don't need any quotes at all around variables, ie

PHP Code:

mysql_connect($dbn$user$pass); 

...and in the case of strings, variables won't be parsed inside of single quotes anyways, only inside of double quotes;

PHP Code:

$var 'world!';

echo 
'Hello, $var'// will display Hello, $var
echo "Hello, $var"// will display Hello, world! 

-m

drewbee 07-02-2008 07:44 PM

to expand even further on this, your class variables are not being utilized.
These guys:
PHP Code:

private $dbn;
private 
$user;
private 
$pass

The variables inside of the function ($dbn, $user, and $pass) are all local to the function, and will no longer be usable outside of the function. You need to assign them to use them outside of it.

Also, constructers are very useful as it will execute as soon as the object is initialized. I ahve the constructor called below to call the two functions. The constructor can be either called __constructor or the exact name of the class in this case: db_connect

PHP Code:

<?php
 
 
class db_connect 
{
 
    private 
$dbn;
    private 
$user;
    private 
$pass;
    
    public function 
db_connect()
    {
        
$this->specs('localhost''username''password');
        
$this->showConnectionDetails();
    }
 
    public function 
specs($dbn$user$pass
    {
        
$this->dbn $dbn;
        
$this->user $user;
        
$this->pass $pass;
 
        
$dbtestcon mysql_connect($dbn$user$pass);
        if (!
$dbtestcon
        {
            die(
'Could not connect: ' mysql_error());
        }
        echo 
'Connected successfully';
    }
 
    
// Now this function will work
    
function showConnectionDetails()
    {
        echo 
'We connected on ' $this->dbn ' with username: ' $this->user ' and password ' $this->password';
    }
 
}
 
?>

PHP Code:

$database_connection = new db_connect();
// Will output:
// 'connected succesfully'
// We connected on 'localhost' with username: 'username' and password 'password' 


codefreek 07-02-2008 09:35 PM

Thank you i get it now ;) just because it's private i need to keep it on that same page, so it can read the values i get it all..

TY!

codefreek 07-02-2008 09:53 PM

PHP Code:

<?php
error_reporting
(E_ALL & ~E_NOTICE);
 
class 
db_connect 
{
 
    private 
$dbn;
    private 
$user;
    private 
$pass;
    private 
$db_selected;
    
    public function 
db_connect()
    {
        
$this->specs('localhost''orb''123123');
        
$this->showConnectionDetails();
    }
 
    public function 
specs($dbn$user$pass$db_selected
    {
    
$this->db_selected $db_selected;
        
$this->dbn $dbn;
        
$this->user $user;
        
$this->pass $pass;
 
        
$dbtestcon mysql_connect($dbn$user$pass);
        if (!
$dbtestcon
        {
            die(
'Could not connect: ' mysql_error());
        }
        echo 
'Connected successfully';
    }
 
    
// Now this function will work
    
function showConnectionDetails()
    {
     
        
$db_selected mysql_select_db('zone'$dbtestcon);
    if (!
$db_selected)
    die (
'Can\'t use workspace : ' mysql_error());

    }
 
}

?>

what am i doing wrong with this snippet of code??..

drewbee 07-02-2008 09:56 PM

Quote:

Thank you i get it now ;) just because it's private i need to keep it on that same page, so it can read the values i get it all..
TY!
Making it privage gives you the ability to access the variables inside of the class, IE $this->. However, you would not be able to get it outside of the class lets say after it has been initialized.

$this->dbn would return results inside of the class,

$database_connection = new db_connection();
echo $database_connection->dbn; Should throw some type of error.

drewbee 07-02-2008 10:04 PM

PHP Code:

// A few things... see comments in code
<?php
error_reporting
(E_ALL & ~E_NOTICE);
 
class 
db_connect 
{
 
    private 
$dbn;
    private 
$user;
    private 
$pass;
    private 
$db_selected;
    
    public function 
db_connect()
    {
        
// you added a 4th parameter to specs. you would need to pass it here
        // public function specs($dbn, $user, $pass, $db_selected)
        
$this->specs('localhost''orb''123123');
        
$this->showConnectionDetails();
    }
 
    public function 
specs($dbn$user$pass$db_selected
    {
        
$this->db_selected $db_selected;
        
$this->dbn $dbn;
        
$this->user $user;
        
$this->pass $pass;
 
        
$dbtestcon mysql_connect($dbn$user$pass);
        if (!
$dbtestcon
        {
            die(
'Could not connect: ' mysql_error());
        }
        echo 
'Connected successfully';
    }
 
    
// Now this function will work
    
function showConnectionDetails()
    {
         
// We have no idea what the variable 'dbtestcon' is. Remember scope. 
        //  $dbtestcon scope is limited to the function spec();
        // You can handle this two ways: 1) ommit the identifier (php is smarty enough to know which
        // connection you are referring to so you could use:
        // $db_selected = mysql_select_db('zone');
        // Option two is to create a class variable for $dbtestcon
        // 1) create variable:  private $dbtestcon;
        // 2) in function specs() when doing the initial mysql_connect(), assign it to the new var:
        //    $this->dbtestcon = mysql_connect($dbn, $user, $pass);
        // 3) Reference it below
        //    $db_selected = mysql_select_db('zone', $this->dbtestcon);
        
$db_selected mysql_select_db('zone'$dbtestcon);
        
        
// Also, one other note. Since you added the variable 'db_selected', you should use that as well IE
        // $db_selected = mysql_select_db($this->db_selected, $this->dbtestcon);
            
if (!$db_selected)
        {
            die (
'Can\'t use workspace : ' mysql_error());
        }

    }
 
}

?>


codefreek 07-03-2008 06:28 AM

What is wrong now?
 
whats wrong now :S?
Quote:

//i know i am outputting the value the wrong way i think..

PHP Code:

<?php
error_reporting
(E_ALL & ~E_NOTICE);

class 
db_connect 
{
 
    private 
$dbn;
    private 
$user;
    private 
$pass;
    private 
$db_selected;
    private 
$dbtestcon;

    
    public function 
db_connect()
    {

        
        
$this->specs('localhost''orb''123123');
        
$this->showConnectionDetails('zone');
    
    
    }
 
    public function 
specs ($dbn$user$pass
    {
        
        
$this->dbn $dbn;
        
$this->user $user;
        
$this->pass $pass;

        
        
$this->dbtestcon mysql_connect($dbn$user$pass);

        if ( ! 
$this->$dbtestcon
        {
            die(
'Could not connect: ' mysql_error());
        }

        echo 
'Connected successfully';
    } 


    
// Now this function will work
    
function showConnectionDetails($db_selected)
    {
    
$this->db_selected $db_selected;
    
    
$db_selected mysql_select_db($this->db_selected$this->dbtestcon);
            if (!
$db_selected)
        {
            die (
'Can\'t use workspace : ' mysql_error());
        }
        echo 
'db_selected';
    }
 
}

?>


delayedinsanity 07-03-2008 07:12 AM

This part here:

PHP Code:

    public function specs($dbn$user$pass
    {
        
        
$this->dbn $dbn;
        
$this->user $user;
        
$this->pass $pass;
     
$this->dbtestcon $dbtestcon;
     
    
        
$dbtestcon mysql_connect($dbn$user$pass);
    
        if (!
$dbtestcon
        {
            die(
'Could not connect: ' mysql_error());
        }
        echo 
'Connected successfully';
    } 

Should be written:

PHP Code:

    public function specs ($dbn$user$pass
    {
        
        
$this->dbn $dbn;
        
$this->user $user;
        
$this->pass $pass;

        
// You were originally trying to assign an empty variable, $dbtestcon to $this->dbtestcon
        
$this->dbtestcon mysql_connect($dbn$user$pass);

        
// You need to use $this to assign properties to a global reference inside your class
        // Any variables that are called within a function directly (such as $dbtestcon as
        // opposed to $this->dbtestcon) are local to that function and do not exist in the global
        // scope of a class.
        
if ( ! $this->$dbtestcon
        {
            die(
'Could not connect: ' mysql_error());
        }

        echo 
'Connected successfully';
    } 

-m

codefreek 07-03-2008 07:22 AM

it is still not working...
.."No database selected"..

Tanax 07-03-2008 07:25 AM

Meaning: You didn't select a database.. ? :-P? So select a database

codefreek 07-03-2008 08:09 AM

so what you call this then ?
$this->showConnectionDetails('zone');
Read the code ;)

Tanax 07-03-2008 08:36 AM

Quote:

Originally Posted by codefreek (Post 16498)
so what you call this then ?
$this->showConnectionDetails('zone');
Read the code ;)

Ahh, missed that part.
Well, maybe you haven't set private $db_selected ? :-P

Also, you can use this:
PHP Code:

$db_selected mysql_select_db($this->db_selected$this->dbtestcon) or die(mysql_error()); 


sketchMedia 07-03-2008 08:38 AM

PHP Code:

 if ( ! $this->$dbtestcon
 {
      die(
'Could not connect: ' mysql_error());
 } 

should be
PHP Code:

 if ( ! $this->dbtestcon
 {
      die(
'Could not connect: ' mysql_error());
 } 

When accessing properties of an object, you dont need the '$' unless its static:
PHP Code:

$this->varName;
self::$varName


codefreek 07-03-2008 08:44 AM

Tanax, please read the code before saying something.. ps, ty all for the help!
but it is still not working... anyone with a clue why =?..

sketchMedia 07-03-2008 08:49 AM

Another tip:
this could be shortened
PHP Code:

$db_selected mysql_select_db($this->db_selected$this->dbtestcon);
if (!
$db_selected)
{
        die (
'Can\'t use workspace : ' mysql_error());            


to:
PHP Code:

mysql_select_db($this->db_selected$this->dbtestcon) or die ('Can\'t use workspace : ' mysql_error()); 


Tanax 07-03-2008 08:49 AM

Quote:

Originally Posted by codefreek (Post 16503)
Tanax, please read the code before saying something.. ps, ty all for the help!
but it is still not working... anyone with a clue why =?..

Eh, I said I missed it, lol. So kill me.

sketchMedia 07-03-2008 08:50 AM

Quote:

but it is still not working... anyone with a clue why =?..
works for me, whats the error?


All times are GMT. The time now is 11:47 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0