madhatter301 Posted March 7, 2007 Share Posted March 7, 2007 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 More sharing options...
nloding Posted March 7, 2007 Share Posted March 7, 2007 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 More sharing options...
madhatter301 Posted March 7, 2007 Author Share Posted March 7, 2007 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 Link to comment https://forums.phpfreaks.com/topic/41614-solved-mysql-data/#findComment-201653 Share on other sites More sharing options...
nloding Posted March 7, 2007 Share Posted March 7, 2007 Hey, OOP is the way to go! Yeah, I see my typo now Link to comment https://forums.phpfreaks.com/topic/41614-solved-mysql-data/#findComment-201661 Share on other sites More sharing options...
madhatter301 Posted March 7, 2007 Author Share Posted March 7, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.