Jump to content

[SOLVED] Mysql Data


madhatter301

Recommended Posts

Hi,

 

I'm trying to do some php but have hit a few problems. I'm new to it all so bear with me  ???

 

Basically I am trying to create a website that takes reservations for different events each year. I have set up my sql tables and am trying to query and insert data into them. I found some helpful tutorials on php classes and so opted for that route to help keep my code tidy. My problem is that I am not sure how to access some of the data once i have retrieved it from my DB.

 

This is what the "event" table I am accessing looks like. Not valid sql i know but just to show you the basic idea.

 

(event_ID VARCHAR(30))
PRIMARY KEY ('event_ID)
(event_day1 DATE)
(event_day2 DATE)
(event_day3 DATE)
(event_day4 DATE)

 

My php class to connect to the database looks like this.

 

class dbConnection {

var $server;
var $db_name;
var $data;
var $q;
var $result;
var $connection;
var $username;
var $password;

function dbConnection() {

	$this->server = "localhost";
	$this->db_name = "events_database";
	$this->username = "myusername";
	$this->password = "mypassword";
        }

function connect() {

	if(!$this->connection = mysql_connect($this->server, $this->username, $this->password)) {
		echo "MYSQL connection problem";
		mysql_error();
		return false;
	}
	mysql_select_db($this->db_name, $this->connection) or die(mysql_error());
}

function query() {

	$this->result = mysql_query($this->q,$this->connection);
		if(!$this->result || (mysql_numrows($this->result) < 1)) {

			return 0;
		}
	$this->data = mysql_fetch_array($this->result);
}

} //end class

 

 

So then i include this php file into my code and use something like:

 

$dbConn = new dbConnection();
$dbConn->connect();
$dbConn->q = "SELECT * FROM event";
$dbConn->query();

 

What I am not sure about is how to access mutiple rows of the data and exactly how the data is stored. I know i can access by using $dbConn->data but like i say i'm not sure about getting access to all the rows.

 

 

Any help would be greatly appreciated, Thanks......

Link to comment
https://forums.phpfreaks.com/topic/41614-solved-mysql-data/
Share on other sites

If you're new to PHP, starting right off with a class is somewhat difficult.

 

With using "mysql_fetch_array" you are already fetching a single row.  You want that in a separate function, say you call it "fetch".

 

Then you would use:

 

while ($row = $dbConn->fetch) {
  // do whatever with your results here
}

 

You fetch method would be like this:

 

funtion fetch() {
  return mysql_fetch_array($this->data);
}

 

There is a chance I'm wrong and that won't function as I want it to, but I think it will be fine.

Link to comment
https://forums.phpfreaks.com/topic/41614-solved-mysql-data/#findComment-201643
Share on other sites

Thanks for your reply,

 

I have programmed with other OO languages so thought this would be the best way to go?

 

I see what you mean and I have just changed my code around and got it working thanks

 

Did you mean

 

funtion fetch() {
  return mysql_fetch_array($this->[b]result[/b]);
}

 

Anyway thanks for your swift help I really appreciate it  ;D

Link to comment
https://forums.phpfreaks.com/topic/41614-solved-mysql-data/#findComment-201653
Share on other sites

Well thanks anyway m8, you saved me a lot of  ??? time i'm sure.

 

Dont suppose you could also advise me on something else?

 

The event table i described above has an "active" column, which is an enum 'y' or 'n'. Basically i only want 1 event active at any given time. So therefore only 1 of the events in the table will have 'y', the rest 'n'. Do you think this is the best way to go about it or is there an easier way?

 

Thanks AGAIN....

Link to comment
https://forums.phpfreaks.com/topic/41614-solved-mysql-data/#findComment-201667
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.