Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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
<?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

1 solution

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

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


please do:

ASM
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.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900