Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.