Unexpected Parse Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Unexpected Parse Error

Post by Nay »

Okay, I'm starting to play around with Classes a little. I tried:

functions.php

Code: Select all

<?

// start session

session_start();

// include files

require "includes/sql.php";

// get action to do

$id = $_GET['id'];

// if ID is LOGIN

if($id=="login") {

$user = $_POST['username'];
$pass = $_POST['password'];

$open = new Connection;
$open->connect();
$c = $open->$c;
$db = $open->$db;

$q = "select * from 'members' where rank = 'admin' and username = '$user' and password = '$pass'";
$r = mysql_query($q, $c);

$rows = mysql_num_rows($r);

if($r>1) {
echo "Sorry, there was more than one result returned";    
} else {
session_register("user");
header("Location: admin.php");
}

}

?>
sql.php

Code: Select all

<?

class Variables {

var $host = "localhost";
var $db = "newezone_site";
var $user = "newezone_xxxx";
var $pass = "xxxx";

}

class Connection extends Variables {

    function connect() {

var $c = mysql_connect("$this->host", "$this->user", "$this->pass");
var $db = mysql_select_db("$this->db", "$this->$c");

    }

    function Close() {
        mysql_close("$this->c");
    }
}

?>
And I get:
Parse error: parse error in /home/newezone/public_html/admin/includes/sql.php on line 16

Fatal error: Cannot instantiate non-existent class: connection in /home/newezone/public_html/admin/functions.php on line 22
-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

function connect() {

var $c = mysql_connect("$this->host", "$this->user", "$this->pass");
var $db = mysql_select_db("$this->db", "$this->$c");
you're using var within a method, that's not allowed.
Anyway $db is already a member of the parent class.
"$this->host"
there's no need to quote here
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

nay: the rule about quotes (" ") with function is the next one:
ya need to enquote a var when its a string.
if ya're passing a variable (even if its a string), ya must not to enquote the var.

Code: Select all

var $c = mysql_connect($this->host, $this->user, $this->pass);
so like ya can guess if you're not using ur class you would write it like:

Code: Select all

var $c = mysql_connect("localhost","junky", "junky_pass");
i hope this gave ya more details about using function.

and in OOP, a function inside a classe are called a method.
function is related to procedural programming, and method is related to OOP (Object-Oriented Programming).

later
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Hey, thanks for the help. I'm getting a new error now it's a mysql_error actually. The link is not a valid resource - the one you get when you're connection is something wrong - well, most of the time.

Code: Select all

$open = new Connection;
$open->connect();
$connection = $open->connection;

mysql_query("do this", $connection);
Is there something wrong with that?

-Nay
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

Connection is a class that ya made, right?
ya've to write it like:

Code: Select all

$open = new Connection(); 
$open->connect(); //if connect is a method of ur class, of course
$connection = $open->connection;
//you should do a setter for the attribute connection of ur class Connection 

mysql_query("select password('blah')", $connection); 
//cause "do this" isnt a valid mysql command.

later
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

i don't think the propper form is $class->$variable
change that into $class->variable for every one u have, and u don't need to put quotes when printing these kinds of variables
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

by setter and getter i mean:

Code: Select all

function set_var_name($var) &#123;
   $this->var_name = $var;
&#125;
and by getter:

Code: Select all

function get_var_name() &#123;
  return $this->var_name;
&#125;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Nay: what does your code look like now?
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

It looks like (I put them into 1 file, for easier testing):

Code: Select all

<?

Class Variables {

var $host = "localhost";
var $user = "Nay";
var $pass = "nay77";
var $db = "news";

}

Class Connection extends Variables {

function connect() {
$connection = mysql_connect($this->host, $this->user, $this->pass) or die(mysql_error());
$database = mysql_select_db($this->db, $connection) or die(mysql_error());
}

function close() {
mysql_close($connection);
}

}

$open = new Connection;
$connection = $open->connection;
$open->connect();

$q = "select * from a_reviews order by title desc";
$r = mysql_query($q, $connection);

while($rows=mysql_fetch_array($r)) {
echo $rows['title'] . "<br />";
}

?>
I get:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\appserv\www\newezone\includes\sql.php on line 30

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\appserv\www\newezone\includes\sql.php on line 32
Help! I can't figure out what's wrong. I mean the variables and functions are set correctly. :(

-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$connection and $database are local variables in function connect(). After the function returns they're gone.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

So does it mean I can't use Methods to do this? I've seen new Connection or something like that in some people's scripts around here.

-Nay
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

ur function (method) must return the object ya wanna use outside this method.
junky
Forum Commoner
Posts: 30
Joined: Wed Sep 03, 2003 3:58 pm

Post by junky »

if tonight ive time (after job), im gonna build a class for that task.

i know it could be long, but im pretty busy, and ive a lot of work to do.

later
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

You mean:

Code: Select all

function connect() { 
$connection = mysql_connect($this->host, $this->user, $this->pass) or die(mysql_error()); 
$database = mysql_select_db($this->db, $connection) or die(mysql_error()); 
return $connection;
return $database;
}
Like that?

-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

no, you can't return more than one value.

Code: Select all

function connect() {
return $connection;  // <- functions returns here
return $database;  // <- never reached
}
I could give you the answer but then what's the learning effect?
Try this one

Code: Select all

<pre>
<?php
Class Variables {
	var $host = "localhost";
	var $user = "Nay";
	var $pass = "nay77";
	var $db = "news";
} 

class VariablesEx extends Variables
{
	var $my_counter = 0;
	
	function incrementA()
	{
		$counter = $counter + 1;
	}
	
	function incrementB()
	{
		$my_counter = $my_counter + 1;
	}
	
	function incrementC()
	{
		$this->my_counter = $this->my_counter + 1;
	}
}

$vex = new VariablesEx;
echo "initial state:\n";
print_r($vex);


$vex->incrementA();
echo "after incrementA:\n";
print_r($vex);


$vex->incrementB();
echo "after incrementB:\n";
print_r($vex);


$vex->incrementC();
echo "after incrementC:\n";
print_r($vex);
?> 
</pre>
and take a close look at the output.
Post Reply