0

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

4
Contributors
6
Replies
12
Views
4 Years
Discussion Span
Last Post by broj1
0

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.

0

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

Edited by diafol

0

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?

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.