我的当前脚本存在问题。我希望它回显变量$ _SESSION ['name'],结果应显示名称。相反,它只显示数字零,而不是名称。虽然我的数据库充满了几个用户和用户名,但它拒绝显示任何名称。
<?php
ob_start();
$host="127.0.0.1"; // Host name
$username="admin"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['id'];
$password=$_POST['password'];
$mypassword=md5($password);
$myname = mysql_query("SELECT username FROM $tbl_name WHERE id = $myusername");
if (!$myname) {
echo 'Could not run query: ' . mysql_error();
exit;
}
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE id='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "plaza.php"
session_start();
$_SESSION['username'] = $myusername;
$_SESSION['password'] = $mypassword;
$_SESSION['name'] = $myname;
header("location:plaza.php");
}
else {
echo "Wrong Username or Password, <a href='index.php'>click here</a> return";
}
ob_end_flush();
?>
//
<?php
session_start();
if(!isset($_SESSION['name'])){
header("location:index.php");
}
?>
<html>
<head>
<title>test</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
</div>
<div id="main_container">
<div id="module_0">
<h1>Welkom <?php
echo $_SESSION['name'];
?>
<form action="logout.php">
<input type="submit" value="Uitloggen"/>
</form>
</div>
<div id="module_1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque id lacus nec tortor euismod ultrices vehicula vitae tellus. Aliquam consectetur risus suscipit auctor placerat. Nunc nec imperdiet odio. Integer eu dui ut nisi dictum varius in vitae erat. Duis porta, lorem nec volutpat vulputate, nulla elit pretium tellus, vel iaculis purus lacus et lectus. Ut porttitor odio vel tempor varius. Nam id eros sed risus tincidunt pharetra. Ut ligula diam, elementum et facilisis ut, lobortis sit amet leo.
</div>
<div id="module_2">
</div>
<div id="module_3">
</div>
<div id="module_4">
</div>
<div id="module_5">
</div>
</div>
</body>
</html>
有谁知道脚本有什么问题,为什么它显示数字0而不是名字?
答案 0 :(得分:3)
$ myname是resource,而不是数据库中的数据。您应该使用mysql_fetch_row
答案 1 :(得分:1)
因为代码:
$myname = mysql_query("SELECT username FROM $tbl_name WHERE id = $myusername");
if (!$myname) {
echo 'Could not run query: ' . mysql_error();
exit;
}
您应将其更改为:
$res = mysql_query("SELECT username FROM $tbl_name WHERE id = $myusername");
$arr = mysql_fetch_array($res);
$myname = $arr['username'];
if (!$myname) {
echo 'Could not run query: ' . mysql_error();
exit;
}
答案 2 :(得分:-1)
$ myname是一个变量类型,您需要使用mysql_fetch_array()或mysql_fetch_row() 从数据库中获取数据然后以会话变量的形式存储为$ _SESSION [&#39; name&#39;] ..