Page 1 of 1
Unexpected Parse Error
Posted: Mon Sep 29, 2003 7:20 am
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
Posted: Mon Sep 29, 2003 1:00 pm
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
Posted: Mon Sep 29, 2003 5:22 pm
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
Posted: Mon Sep 29, 2003 6:02 pm
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
Posted: Mon Sep 29, 2003 6:12 pm
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
Posted: Mon Sep 29, 2003 6:50 pm
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
Posted: Mon Sep 29, 2003 8:40 pm
by junky
by setter and getter i mean:
Code: Select all
function set_var_name($var) {
$this->var_name = $var;
}
and by getter:
Code: Select all
function get_var_name() {
return $this->var_name;
}
Posted: Mon Sep 29, 2003 9:27 pm
by volka
Nay: what does your code look like now?
Posted: Tue Sep 30, 2003 9:40 am
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
Posted: Tue Sep 30, 2003 3:12 pm
by volka
$connection and $database are local variables in function connect(). After the function returns they're gone.
Posted: Wed Oct 01, 2003 3:30 am
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
Posted: Wed Oct 01, 2003 7:03 am
by junky
ur function (method) must return the object ya wanna use outside this method.
Posted: Wed Oct 01, 2003 7:05 am
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
Posted: Wed Oct 01, 2003 7:13 am
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
Posted: Wed Oct 01, 2003 11:59 am
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.