TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-02-2008, 06:15 PM   #1 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
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';



     }



}

?>
codefreek is offline  
Reply With Quote
Old 07-02-2008, 06:31 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
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)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

so how would i fix that instead ?
codefreek is offline  
Reply With Quote
Old 07-02-2008, 07:17 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
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)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
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!
codefreek is offline  
Reply With Quote
Old 07-02-2008, 09:53 PM   #7 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
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??..

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)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
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';
    }
 
}

?>

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)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
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)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

it is still not working...
.."No database selected"..
codefreek is offline  
Reply With Quote
Old 07-03-2008, 07:25 AM   #13 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
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)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

so what you call this then ?
$this->showConnectionDetails('zone');
Read the code ;)
codefreek is offline  
Reply With Quote
Old 07-03-2008, 08:36 AM   #15 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by codefreek View Post
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 ?

Also, you can use this:
PHP Code:
$db_selected mysql_select_db($this->db_selected$this->dbtestcon) or die(mysql_error()); 
Tanax is offline  
Reply With Quote
Old 07-03-2008, 08:38 AM   #16 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
codefreek (07-03-2008)
Old 07-03-2008, 08:44 AM   #17 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

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 =?..
codefreek is offline  
Reply With Quote
Old 07-03-2008, 08:49 AM   #18 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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()); 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 07-03-2008, 08:49 AM   #19 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by codefreek View Post
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.
Tanax is offline  
Reply With Quote
Old 07-03-2008, 08:50 AM   #20 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
but it is still not working... anyone with a clue why =?..
works for me, whats the error?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:28 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design