Jump to content

OOP mysql class


marcin_koss

Recommended Posts

Hello everybody.

 

I am starting learning OO PHP and after reading many articles about it I am trying to start writing my first class which would be a class that handles mysql connections and performs queries. Below is the code I wrote so far but I'm already stuck with this error:

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\www\mysql_connect_oop.php on line 36"

 

I would appreciete if somebody could take a look a the code and tell me where could be the problem.

 

<?php

// class mysql_con

class mysql_con{

 

var $name;

var $pass;

var $host;

var $db;

var $query;

var $result;

 

function __construct($n, $p, $h, $d) {

$this->name = $n;

$this->pass = $p;

$this->host = $h;

$this->db = $d;

}

function connect() {

mysql_connect($this->host, $this->name, $this->pass);

mysql_select_db($this->db);

}

function query($q) {

$this->query = $q;

}

function result() {

$this->result = mysql_query($this->query) or trigger_error("Query: $this->query\n<br />MySQL Error: " . mysql_error());

}

 

}

 

// script that opens connection and performs query

 

$db = new mysql_con('root','martek123','localhost','world');

$db->query('SELECT name FROM city WHERE name = "bialystok"');

 

while ($row = mysql_fetch_array($db->result)) {

echo $row['name'];

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/182097-oop-mysql-class/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.