Click here to Skip to main content
11,716,938 members (88,322 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
Hi I have an error when trying to register a member. The form is failing to connect to the database. Error:Fatal error: Function name must be a string in C:\wamp\www\smith\config.php on line 7
Line 7 has been highlighted in bold and I am also getting an error:
Access denied to locahost ("smith")
I will appreciate your assistance.

THE CODE FOR MY FORM IS BELOW:

<?php
    //Start session
    session_start();
 
    //Include database connection details
    require_once('config.php');
 
    //Array to store validation errors
    $errmsg_arr = array();
 
    //Validation error flag
    $errflag = false;
 
    //Connect to mysql server
    $link = mysql_connect($localhost, $username, $password);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }
 
    //Select database
    $db = mysql_select_db("smith");
    if(!$db) {
        die("Unable to select database");
    }
 
    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }
 
    //Sanitize the POST values
    $fname = clean($_POST['fname']);
    $lname = clean($_POST['lname']);
    $login = clean($_POST['login']);
    $password = clean($_POST['password']);
    $cpassword = clean($_POST['cpassword']);
 
    //Input Validations
    if($fname == '') {
        $errmsg_arr[] = 'First name missing';
        $errflag = true;
    }
    if($lname == '') {
        $errmsg_arr[] = 'Last name missing';
        $errflag = true;
    }
    if($login == '') {
        $errmsg_arr[] = 'Login ID missing';
        $errflag = true;
    }
    if($password == '') {
        $errmsg_arr[] = 'Password missing';
        $errflag = true;
    }
    if($cpassword == '') {
        $errmsg_arr[] = 'Confirm password missing';
        $errflag = true;
    }
    if( strcmp($password, $cpassword) != 0 ) {
        $errmsg_arr[] = 'Passwords do not match';
        $errflag = true;
    }
 
    //Check for duplicate login ID
    if($login != '') {
        $qry = "SELECT * FROM members WHERE login='$login'";
        $result = mysql_query($qry);
        if($result) {
            if(mysql_num_rows($result) > 0) {
                $errmsg_arr[] = 'Login ID already in use';
                $errflag = true;
            }
            @mysql_free_result($result);
        }
        else {
            die("Query failed");
        }
    }
 
    //If there are input validations, redirect back to the registration form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: register-form.php");
        exit();
    }
 
    //Create INSERT query
    $qry = "INSERT INTO members(firstname, lastname, login, passwd) VALUES('$fname','$lname','$login','".md5($_POST['password'])."')";
    $result = @mysql_query($qry);
 
    //Check whether the query was successful or not
    if($result) {
        header("location: register-success.php");
        exit();
    }else {
        die("Query failed");
    }
?>

TH DATABASE CONNECTION CODE:


<?php
        //Database connection
    $mysql_hostname="localhost";
    $mysql_username="root";
    $mysql_password="";
  $mysql_databse="smith";
  $db_mysql_connect($mysql_hostname, $mysql_username, $mysql_password)
  or die("Can not connect to the database");
 
  mysql_select_db("smith") or die(mysql_error());
?>
Posted 17-Nov-12 6:39am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi

The problem seems to be in the database connection code where you made a call to $db_mysql_connect

Though its been a while that I coded in PHP but I guess you cann't use $ sign in front of a function name. Also the function to connect to a database is mysql_connect not db_mysql_connect. Check this out http://php.net/manual/en/function.mysql-connect.php[^]

so instead of

$db_mysql_connect($mysql_hostname, $mysql_username, $mysql_password)
  or die("Can not connect to the database");

please do:

mysql_connect($mysql_hostname, $mysql_username, $mysql_password)
  or die("Can not connect to the database");



Regards
Pawan

Please mark the answer as accepted solution if it is relevant.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 599
1 OriginalGriff 255
2 F-ES Sitecore 210
3 Maciej Los 185
4 Richard MacCutchan 130
0 Sergey Alexandrovich Kryukov 1,144
1 Maciej Los 509
2 OriginalGriff 505
3 Richard MacCutchan 440
4 CHill60 415


Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100