TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2008, 06:15 PM   #1 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 279
Thanks: 166
codefreek is on a distinguished road
Application Error 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';



     }



}

?>
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 07-02-2008, 06:31 PM   #2 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 536
Thanks: 64
Tanax is on a distinguished road
Default

You can't use ' around the variables in the mysql_connection
Tanax is offline  
Reply With Quote
Old 07-02-2008, 07:11 PM   #3 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 279
Thanks: 166
codefreek is on a distinguished road
Default

so how would i fix that instead ?
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 07-02-2008, 07:17 PM   #4 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas
Posts: 651
Thanks: 24
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
codefreek (07-02-2008)
Old 07-02-2008, 07:44 PM   #5 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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' 
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
The Following User Says Thank You to drewbee For This Useful Post:
codefreek (07-02-2008)
Old 07-02-2008, 09:35 PM   #6 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 279
Thanks: 166
codefreek is on a distinguished road
Default

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!
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 07-02-2008, 09:53 PM   #7 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 279
Thanks: 166
codefreek is on a distinguished road
Default

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??..
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.

Last edited by codefreek : 07-03-2008 at 06:30 AM.
codefreek is offline  
Reply With Quote
Old 07-02-2008, 09:56 PM   #8 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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.
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
The Following User Says Thank You to drewbee For This Useful Post:
codefreek (07-02-2008)
Old 07-02-2008, 10:04 PM   #9 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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

    }
 
}

?>
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
The Following User Says Thank You to drewbee For This Useful Post:
codefreek (07-02-2008)
Old 07-03-2008, 06:28 AM   #10 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 279
Thanks: 166
codefreek is on a distinguished road
Help 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';
    }
 
}

?>
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.

Last edited by codefreek : 07-03-2008 at 07:23 AM. Reason: CODE EDIT. 4th Time.
codefreek is offline  
Reply With Quote
Old 07-03-2008, 07:12 AM   #11 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas
Posts: 651
Thanks: 24
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
codefreek (07-03-2008)
Old 07-03-2008, 07:22 AM   #12 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 279
Thanks: 166
codefreek is on a distinguished road
Default

it is still not working...
.."No database selected"..
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 07-03-2008, 07:25 AM   #13 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 536
Thanks: 64
Tanax is on a distinguished road
Default

Meaning: You didn't select a database.. ? ? So select a database
Tanax is offline  
Reply With Quote
Old 07-03-2008, 08:09 AM   #14 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 279
Thanks: 166
codefreek is on a distinguished road
Default

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