--------------- 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);
?>
出现错误是(((!)致命错误:在第6行的C:\ wamp \ www \ TechnologySociety \ logindata.php中调用未定义的函数conn() )) 当我调用函数
时答案 0 :(得分:1)
你需要在logindata.php中包含 conn.php
//logindata.php
<?php
....
include("conn.php");
session_start();
您正在调用session_start()两次,这样会产生另一个错误。
您还应该考虑使用mysqli而不是mysql
答案 1 :(得分:0)
你只需要开始会话 - session_start();
doc session_start()
根据通过GET或POST请求传递的会话标识符创建会话或恢复当前会话,或通过cookie传递。
也不要忘记包含连接功能..
include_once('conn.php');
答案 2 :(得分:0)
--------------- logindata.php ------------ 在logindata.php顶部的这一行
include_once("conn.php");
答案 3 :(得分:0)
更改
function conn{
为:
function conn(){
并在函数结束时返回连接
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;
}
你应该打电话给谁,
$link = conn();
$query = mysql_query($sql, $link);
答案 4 :(得分:0)
你错过了
required conn.php
前
conn();
同样在logindata.php
中,无需拨打电话
session_start();
因为它已在conn.php
中调用,并且在将其包含在required()
之后,它将被添加并调用
也在conn.php
中添加括号到函数定义
function conn(){
答案 5 :(得分:0)
尝试使用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);
?>