I have this in my index.php

include ('php/config.php');
$connection = new db(); 
$connection->connect();

the class (in a file called config.php) looks like this:

<?php
//CONECTION CLASS
class db
{
    var $host="localhost";
    var $username="root";
    Var $password="";
    var $database="training";

public function connect() 
{
    mysql_connect($this->host,$this->username,$this->password);
    mysql_select_db($this->database)or die(mysql_error());
    $sql = "SHOW TABLES";  
    $result = mysql_query($sql);
    return true;
}}

It doesnt show the tables? if I do normal procedural code its fine and shows the 10 tables in my database?
how can I make this work please?

Many Thanks

The code you posted doesn't print anything. It contains no echo statement and the result of the query, $result = mysql_query($sql);, doesn't get stored in a class property that could be printed by another method.

PS: A connect() method likely shouldn't perform a query or output anything. If you're doing that just as a test, that's cool.

Member Avatar for diafol

If you're using OOP, I'd suggest moving over to something like PDO or even mysqli. mysql is legs up.

I guess I have misunderstood what a class can do, I thought I could have a simple SQL statement and call it every time I need it in a new instance?

my code does have an echo statement, for some reason it was deleted from this thread, is it best to just use procedural code then?

and mysqli is replacing mysql?

Mad coder, how do I store it in the class property? that is what I want....thanks

is it best to just use procedural code then?

Whether to use procedural or object oriented style is a a question of a debate. They both have pros and cons. I personally use OOP style since I found it easier to maintain and scale code. It took me some time though to get experience to design classes correctly so they do not have to be redesigned too often. I personally recommend OOP style.

and mysqli is replacing mysql?

mysqli is newer version (mysql improved) and is recommended. It supports newer mysql db servers (4.1.3 and above) and has more features (prepared statements, transactions...). mysqli supports bot procedural style and OOP style of using it's functions.

http://www.php.net/manual/en/book.mysqli.php

There are many examples of how to use it on php.net site like this one:

http://php.net/manual/en/mysqli.query.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.