Page 1 of 1

how to call this function?

Posted: Sat Nov 02, 2002 2:32 pm
by cooler75
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

Code: Select all

$conn = db_connect();
what should i do to correct this please..
thank you

Posted: Sat Nov 02, 2002 3:24 pm
by BDKR
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

Posted: Sat Nov 02, 2002 3:33 pm
by cooler75
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

Posted: Sat Nov 02, 2002 4:01 pm
by PaTTeR
Look at lines above line 6. May be you have something forgotten

DAMM SEMICOLONS

Posted: Sat Nov 02, 2002 4:20 pm
by phpScott
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 :twisted: as nothing is more silly or frustrating in my mind.

phpScott

Posted: Sat Nov 02, 2002 4:20 pm
by cooler75
hello,
ok my bad :oops:

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

Code: Select all

function db_connect()
thank you

Posted: Mon Nov 04, 2002 2:26 am
by twigletmac
Is there anything on lines 1-4?

Mac

Posted: Mon Nov 04, 2002 12:35 pm
by cooler75
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

try this

Posted: Mon Nov 04, 2002 1:50 pm
by phpScott
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

Posted: Mon Nov 04, 2002 3:21 pm
by cooler75
ok, i used yours to test and it worked!
:D

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!