 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
07-02-2008, 06:15 PM
|
#1 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
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';
}
}
?>
|
|
|
|
07-02-2008, 06:31 PM
|
#2 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
You can't use ' around the variables in the mysql_connection
|
|
|
|
07-02-2008, 07:11 PM
|
#3 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
so how would i fix that instead ?
|
|
|
|
07-02-2008, 07:17 PM
|
#4 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
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
|
|
|
|
|
The Following User Says Thank You to delayedinsanity For This Useful Post:
|
|
07-02-2008, 07:44 PM
|
#5 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
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.
|
|
|
|
The Following User Says Thank You to drewbee For This Useful Post:
|
|
07-02-2008, 09:35 PM
|
#6 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
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!
|
|
|
|
07-02-2008, 09:53 PM
|
#7 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
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.
|
|
|
|
07-02-2008, 09:56 PM
|
#8 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
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.
|
|
|
|
The Following User Says Thank You to drewbee For This Useful Post:
|
|
07-02-2008, 10:04 PM
|
#9 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
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.
|
|
|
|
The Following User Says Thank You to drewbee For This Useful Post:
|
|
07-03-2008, 06:28 AM
|
#10 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
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.
|
|
|
|
07-03-2008, 07:12 AM
|
#11 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
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
|
|
|
|
|
The Following User Says Thank You to delayedinsanity For This Useful Post:
|
|
07-03-2008, 07:22 AM
|
#12 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
it is still not working...
.."No database selected"..
|
|
|
|
07-03-2008, 07:25 AM
|
#13 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Meaning: You didn't select a database.. ?  ? So select a database
|
|
|
|
07-03-2008, 08:09 AM
|
#14 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
so what you call this then ?
$this->showConnectionDetails('zone');
Read the code ;)
|
|
|
|
07-03-2008, 08:36 AM
|
#15 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by codefreek
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());
|
|
|
|
07-03-2008, 08:38 AM
|
#16 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
|
The Following User Says Thank You to sketchMedia For This Useful Post:
|
|
07-03-2008, 08:44 AM
|
#17 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
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 =?..
|
|
|
|
07-03-2008, 08:49 AM
|
#18 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
07-03-2008, 08:49 AM
|
#19 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by codefreek
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.
|
|
|
|
07-03-2008, 08:50 AM
|
#20 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|