How do I make a database file?

  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

Basically I'm learning CLASSES and OBJECT ORIENTED PROGRAMMING, I was just trying to make a database connecting file. It didn't work so I became crazy after not getting the file to work so now I'm reaching out for some help.

I want to be able to connect to my database like this (code below being index.php)

Code: [ Select ]
<?php
include db.php;
dbconnect = new dbconnect("name of somedatabase");
dbconnect->connect();
?>
  1. <?php
  2. include db.php;
  3. dbconnect = new dbconnect("name of somedatabase");
  4. dbconnect->connect();
  5. ?>


The db.php should contain a class, with the variables $host, $user, $password preset and do a mysql_connect($host, $user, $password); this seems impossible. Also I want the values of $host, $user, $password to be changeable. And the installation "dbconnect = new dbconnect("name of somedatabase");" should let me decide database.

Here is my db.php that did nothing:

Code: [ Select ]
<?php

class dbconnect {
var $host= "localhost";
var $user= "root";
var $psw= "myPASS";
var $database = false;

//function to change the default connection
function ChangeData($host, $user, $psw) {
$this->host = $host;
$this->user = $user;
$this->psw = $psw;

}
function connect($db) {
$this->database=$db;
$con= mysql_connect($host, $user, $database);
mysql_select_db("$database", $con);
}
?>
  1. <?php
  2. class dbconnect {
  3. var $host= "localhost";
  4. var $user= "root";
  5. var $psw= "myPASS";
  6. var $database = false;
  7. //function to change the default connection
  8. function ChangeData($host, $user, $psw) {
  9. $this->host = $host;
  10. $this->user = $user;
  11. $this->psw = $psw;
  12. }
  13. function connect($db) {
  14. $this->database=$db;
  15. $con= mysql_connect($host, $user, $database);
  16. mysql_select_db("$database", $con);
  17. }
  18. ?>


Trying to run these files result in a error saying "Parse error: parse error, expecting `T_FUNCTION' in C:\wamp\www\gtf\lib\db.php on line 21".
How can I make this work the OOP way? I'm probably over-complicating things. Please help me with this.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I see a few problems in your code. The first section of code you posted you are missing your $ headings for the variables. So this line:

PHP Code: [ Select ]
dbconnect = new dbconnect("name of somedatabase");
dbconnect->connect();
  1. dbconnect = new dbconnect("name of somedatabase");
  2. dbconnect->connect();


should be:

PHP Code: [ Select ]
$dbconnect = new dbconnect("name of somedatabase");
$dbconnect->connect();
  1. $dbconnect = new dbconnect("name of somedatabase");
  2. $dbconnect->connect();


The second problem I am seeing is that with your class file you are missing the final } on your class which is probably why you are getting a parse error on that line.

Third I am not seeing any constructor in your class since you are wanting to call the class like this:

PHP Code: [ Select ]
$dbconnect = new dbconnect("name of somedatabase");


Make sure that your dbconnect class has a constructor that can take the value you are passing there and store it in a private variable in your class. A default constructor would have the same function name as your class name, so in this case you would have a function in your dbconnect class like:

PHP Code: [ Select ]
function dbconnect($db) {
   $this->database = $db;
}
  1. function dbconnect($db) {
  2.    $this->database = $db;
  3. }


Make sure you modify your connect function appropriately, as you are not passing any variables through that function.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

Thanks for your input. But I has rewrote it and I think problem is solved.

(This) being the new index.php:

Code: [ Select ]
<?php
require "db.php";
$dbase = new dbcon("");
$dbase->go();
?>
  1. <?php
  2. require "db.php";
  3. $dbase = new dbcon("");
  4. $dbase->go();
  5. ?>


And this being db.php:

Code: [ Select ]
<?php
class dbcon{
    var $con = "";
    var $host = "localhost";
    var $user = "root";
    var $password = "myPASS";
    var $db_name = "test";
    
    //makes it possible to change the default "database"
    function __construct($whatDB) {
    if($whatDB!= "") {
    $this->db_name = $whatDB;
    }
    }

    //connects to the selected database
    function go() {
    $this->con = mysql_connect("$this->host", "$this->user", "$this->password") or die(mysql_error());
    mysql_select_db($this->db_name, $this->con);
    }
}
?>
  1. <?php
  2. class dbcon{
  3.     var $con = "";
  4.     var $host = "localhost";
  5.     var $user = "root";
  6.     var $password = "myPASS";
  7.     var $db_name = "test";
  8.     
  9.     //makes it possible to change the default "database"
  10.     function __construct($whatDB) {
  11.     if($whatDB!= "") {
  12.     $this->db_name = $whatDB;
  13.     }
  14.     }
  15.     //connects to the selected database
  16.     function go() {
  17.     $this->con = mysql_connect("$this->host", "$this->user", "$this->password") or die(mysql_error());
  18.     mysql_select_db($this->db_name, $this->con);
  19.     }
  20. }
  21. ?>


Not reviving any errors at least but I have yet to try it out further. If there are any suggestions on how to improve/optimize this code I be happy to listen.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

By the way I think I added more to my post while you were writing yours, so you may want to re-read the end of my last post. Also I just did a bit of research and my methods for the constructor seem to be a bit outdated as it would not longer work for PHP 5.3.3 or higher, the preferred way is the way you are doing it with the __construct method.

With that said the code you rewrote at first glance seems like it could work. Let us know if you encounter problems.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

Bigwebmaster wrote:
By the way I think I added more to my post while you were writing yours, so you may want to re-read the end of my last post. Also I just did a bit of research and my methods for the constructor seem to be a bit outdated as it would not longer work for PHP 5.3.3 or higher, the preferred way is the way you are doing it with the __construct method.

With that said the code you rewrote at first glance seems like it could work. Let us know if you encounter problems.


I read all of your post now. And I also did some testing. After using the connection made by db.php to my database called "test", I tried sending a mysql INSERT INTO query(it got added). And then I tried changing a row in my file index.php so it asked for a other database (built the same as "test").

$dbase = new dbcon("");
became

$dbase = new dbcon("test2");

And all of a sudden I could add stuff to the database "test2" instead! :D So far no bugs that I have spotted!
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

That is awesome! Great job, feels great when things actually work, huh ;)
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

Bigwebmaster wrote:
feels great when things actually work, huh ;)



Ofc, working code was the goal. :wink:

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 153 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.