3 Replies - 154 Views - Last Post: 12 March 2014 - 12:48 PM Rate Topic: -----

#1 RawrItsAdam  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 72
  • Joined: 10-November 08

Getting syntax error

Posted 12 March 2014 - 09:13 AM

I'm I'm trying to connect to a database grab some information and then print it out on a page, except I'm not having much luck this is the error I keep receiving:
Parse error: syntax error, unexpected T_PUBLIC in /home/adamspar/public_html/dev/_db.php on line 32


My code is as followed:
index.php
<?php

include_once('_db.php');


$news = $connect->Fetch('News');

if ($news && mysql_num_rows($news) > 0) {
     echo "Current news:<BR>";
     while ($record = mysql_fetch_array($news)) {
         echo $record[0];
     }
} else {
     echo "No posts!";
}

?>


_db.php
<?php

class connection {
	private $host;
	private $username;
	private $password;
	private $database;
	private $con;

	public function __Construct($host, $username, $password){
		$this->host = $host;
		$this->username = $username;
		$this->password = $password;

		$this->con = mysql_connect($this->host, $this->username, $this->password) 
			or die("Couldn't connect with database details provided"); 
	}


	public function Database($database) {
		$this->database = $database;
		mysql_select_db($this->database, $this->con)
			or die("Couldn't select the database in question");
	}

}

$connect = new connection ('localhost' , 'xx' , 'xx');
$connect->Database('xx');


	public function Fetch($table_name) {
	    return mysql_query("SELECT * FROM ".$table_name, $this->con);         
	  }

?>



Any pointers in the right direction would be great!
Thanks

This post has been edited by RawrItsAdam: 12 March 2014 - 09:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting syntax error

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Getting syntax error

Posted 12 March 2014 - 09:54 AM

You're declaring the function outside of your class definition.
Was This Post Helpful? 0
  • +
  • -

#3 RawrItsAdam  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 72
  • Joined: 10-November 08

Re: Getting syntax error

Posted 12 March 2014 - 10:00 AM

View PostJackOfAllTrades, on 12 March 2014 - 04:54 PM, said:

You're declaring the function outside of your class definition.


As soon as you said it it made sense, I think I had looked at it so much I was just seeing jumbled up letters. Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Getting syntax error

Posted 12 March 2014 - 12:48 PM

if you want your DB code to be used via a class, donít use the outdated and deprecated mysql_* functions. instead use either PDO or MySQLi in the object-oriented style. those are already OO.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1