Reputation: 3
---------------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
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
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
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
Reputation: 9351
--------------- logindata.php ------------ at this line at the top of logindata.php
include_once("conn.php");
Upvotes: 0
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
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