mysql_connect 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
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

mysql_connect error

Post by ast3r3x »

The error:
Parse error: parse error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/16/d107262476/htdocs/swigg/sampler/index.php on line 5
From the start of the script to line 5:

Code: Select all

<?php

class DBase
{
	$link = mysql_connect('address', 'user', 'pass');
I'm really confused, because I've used this before, but it seems like having it in my class causes problems. My first attempt at making a class too! ;) :roll:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

First of all, you never close your class..
secondly, you need to have your $link bit inside a function, you'll get errors if not.

Code: Select all

class NAME {

   function NAME1() {

     //do stuff here
   }

}
is the proper format..


feyd | forgot the parens ;)
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

*sigh*

Way to easy to fix.

I did close my class I just only showed up to the 5th line because that is where it said the error was.

Thanks, I was under the impression you could put any code inside of a class you wanted to run when it was created.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, you can.. inside the constructor. Unless the code is static return data, it won't fly, from what I remember.
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

Thanks guys! (gals?)

I got my first class working, and it will save me a little code for when I make calls to my database. I have to say I wish my main webserver was running PHP5, it's so nice to develop on my system, but then when I upload, I have to change stuff around to get it working...so annoying.

Anwyays, again thanks, you got me started on simple OO...McGruff would be so proud ;)
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Well if you want a database class, I highly suggest you check out ADOdb!
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

Thanks I'll check that out. Would ADOdb be overkill considering I just connect to my mysql database, perform simple queries?
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

No. They have some pretty useful functions and i think it is easier to use than php's mysql functions.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

The debate of the need for database abstraction layers, such as ADOdb has been discussed before.If you aare new to database handling in PHP I would recommend you learn how to do it using core PHP before you look at database abstraction layers. Who knows you may get a job where ADOdb is not allowed or they have their own abstraction layer. Know the basics and implementing ADOdb becomes easier.
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

Well by no means am I a pro, but I know how to work with mysql from php. My book actually taught me with pear, but I changed to just using mysql_functions.

I really only use connect, select_db, query, and fetch_array.

I guess I'll check out ADOdb then, if nothing else, can't hurt to learn something new.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

I don't think i have ever heard of anyone who went straight into ADOdb without first learning php's mysql module.

I just find it easier to work with, in fact, if you ever want to change databases, no need to screw with your code. I think most companies would admire that bennefit don't you? (I know mine does).
Post Reply