user3136540
user3136540

Reputation: 3

error when i call function that contain mysql connection

---------------conn.php-----------------

 <?php 

    session_start(); 

    function conn{

    $hostname = "localhost";
    $userDB = "root";
    $password = "";
    $databaseName = "forum";
$con = mysql_connect($hostname, $userDB, $password) or
            die("failed to connect");
    mysql_select_db($databaseName, $con) or
            die("failed to connect with database");

        }

            ?>

---------------------logindata.php------------

<?php 

session_start();


conn();

$myusername = mysql_real_escape_string($_POST['username']);
$mypassword = mysql_real_escape_string($_POST['password']);
$query = mysql_query("select * from users where username = '" . $myusername . "'  and password = '" . $mypassword . "' ");
if (mysql_num_rows($query) < 1) {

    echo "wrong";
} else {

    $_SESSION['username'] = $myusername;


    $query = mysql_query("select * from users  where username = '" . $myusername . "'  and password = '" . $mypassword . "' ");
    $row = mysql_fetch_assoc($query);
    $_SESSION['id'] = $row['id'];
    $_SESSION['name'] = $row['name'];
    $_SESSION['email'] = $row['email'];

    echo '<meta http-equiv="Refresh" content="0; URL=posts.php" />';
}



mysql_close($con);
?>

The error appears is (( ( ! ) Fatal error: Call to undefined function conn() in C:\wamp\www\TechnologySociety\logindata.php on line 6 )) when i call the function

Upvotes: 0

Views: 139

Answers (6)

rray
rray

Reputation: 2556

change

function conn{

to:

function conn(){

and return the connection at end of function

function conn(){

  $hostname = "localhost";
  $userDB = "root";
  $password = "";
  $databaseName = "forum";
  $con = mysql_connect($hostname, $userDB, $password) or
        die("failed to connect");
  mysql_select_db($databaseName, $con) or
        die("failed to connect with database");
  return $con;
}

who you should call,

$link = conn();
$query = mysql_query($sql, $link);

Upvotes: 0

Loopo
Loopo

Reputation: 2195

You need to include conn.php in logindata.php

 //logindata.php
    <?php 
    ....
    include("conn.php");
    session_start();

You are calling session_start() twice, so that will generate another error.

You should also look at using mysqli instead of mysql

Upvotes: 1

zzlalani
zzlalani

Reputation: 24374

you are missing

required conn.php

before

conn();

Also in logindata.php there is no need to call

session_start();

since it is already called in conn.php and after including it with required() it will be added and called

also in conn.php add parentheses to the function definition

function conn(){

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

--------------- logindata.php ------------ at this line at the top of logindata.php

 include_once("conn.php");

Upvotes: 0

Anil Meena
Anil Meena

Reputation: 993

Try this code for logindata.php

<?php 
require('conn.php');
session_start();


conn();

$myusername = mysql_real_escape_string($_POST['username']);
$mypassword = mysql_real_escape_string($_POST['password']);
$query = mysql_query("select * from users where username = '" . $myusername . "'  and password = '" . $mypassword . "' ");
if (mysql_num_rows($query) < 1) {

    echo "wrong";
} else {

    $_SESSION['username'] = $myusername;


    $query = mysql_query("select * from users  where username = '" . $myusername . "'  and password = '" . $mypassword . "' ");
    $row = mysql_fetch_assoc($query);
    $_SESSION['id'] = $row['id'];
    $_SESSION['name'] = $row['name'];
    $_SESSION['email'] = $row['email'];

    echo '<meta http-equiv="Refresh" content="0; URL=posts.php" />';
}



mysql_close($con);
?>

Upvotes: 0

Jorge Y. C. Rodriguez
Jorge Y. C. Rodriguez

Reputation: 3449

you only need to start the session ones -- session_start(); you doc session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

also don't forget to include the connection function.. include_once('conn.php');

Upvotes: 0

Related Questions