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
cooler75
Forum Commoner
Posts: 40 Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland
Post
by cooler75 » Sat Nov 02, 2002 2:32 pm
hello,
i created a database function as follow called db.php:
Code: Select all
//CONNECT TO DATABASE
function db_connect()
{
$link = mysql_connect ($server, $user, $password);
if (! $link)
{
die ("Couldn't connect to mySQL server");
}
if (!mysql_select_db ($db, $link) )
{
die ("Coldn't open $db: ".mysql_error() );
}
}
//END DATABASE
When i tried to call it using test.php:
Code: Select all
<?
include "common.php";
include "db.php"
$conn = db_connect();
i got an error message:
Parse error: parse error, unexpected T_VARIABLE in /home/virtual/site56/fst/var/www/html/testing/test.php on line 6
line 6 is
what should i do to correct this please..
thank you
BDKR
DevNet Resident
Posts: 1207 Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:
Post
by BDKR » Sat Nov 02, 2002 3:24 pm
Something that I've taken to doing as of late is running the include files and seeing if they come up with errors. Try running db.php in your browser or from the command line. See what it tells ya.
Cheers,
BDKR
cooler75
Forum Commoner
Posts: 40 Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland
Post
by cooler75 » Sat Nov 02, 2002 3:33 pm
hello,
when i ran commom.php and db.php
i see nothing showed on the page, so i guess there is nothing wrong with those 2 files.
please help
PaTTeR
Forum Commoner
Posts: 56 Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:
Post
by PaTTeR » Sat Nov 02, 2002 4:01 pm
Look at lines above line 6. May be you have something forgotten
phpScott
DevNet Resident
Posts: 1206 Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.
Post
by phpScott » Sat Nov 02, 2002 4:20 pm
DAMM THOSE SEMICOLONS
If you are displaying the exact code that you are using then you have forgotten a semicolon at the end of line
include "db.php"
in your test.php file.
Missing semicolons are the work of the
as nothing is more silly or frustrating in my mind.
phpScott
cooler75
Forum Commoner
Posts: 40 Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland
Post
by cooler75 » Sat Nov 02, 2002 4:20 pm
hello,
ok my bad
thanks
however after i had the semicolon there, i get these error now:
Parse error: parse error, unexpected T_FUNCTION in /home/virtual/site56/fst/var/www/html/testing/db.php on line 5
Fatal error: Call to undefined function: db_connect() in /home/virtual/site56/fst/var/www/html/testing/test.php on line 5
what is wrong with my function?
line 5 on db.php is
thank you
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Mon Nov 04, 2002 2:26 am
Is there anything on lines 1-4?
Mac
cooler75
Forum Commoner
Posts: 40 Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland
Post
by cooler75 » Mon Nov 04, 2002 12:35 pm
hello,
thanks for replying,
well, here is the script:
Code: Select all
<?
include "common.php";
include "db.php";
$conn = db_connect();
line 4 is empty, got any idea?
could you maybe check my function?
thank you
phpScott
DevNet Resident
Posts: 1206 Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.
Post
by phpScott » Mon Nov 04, 2002 1:50 pm
I reworked your code a bit so try this.
for test.php
Code: Select all
<?php
include "db.php";
$conn = db_connect();
if(!$conn)
echo "no connection";
else
echo "connection made";
?>
and for db.php
Code: Select all
<?php
//CONNECT TO DATABASE
function db_connect()
{
include "common.php";
$link = mysql_connect ($server, $user, $password);
if (! $link)
{
die ("Couldn't connect to mySQL server");
}
if (!mysql_select_db ($db, $link) )
{
die ("Couldn't open $db: ".mysql_error() );
}
return true;
}
//END DATABASE
?>
use your own common.php of course.
I got it working locally on my machine so hopefully it will transfer well.
phpScott
cooler75
Forum Commoner
Posts: 40 Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland
Post
by cooler75 » Mon Nov 04, 2002 3:21 pm
ok, i used yours to test and
it worked!
so what did i do wrong with my own? the only thing i didnt do was to check $conn
hmm..because i didnt include commom.php in my function? stupid me
THANK YOU VERY MUCH!