【发布时间】:2015-10-15 07:04:34
【问题描述】:
所以我试图在我最近在我的 WAMP 服务器版本 2.5 上设置的网站上从我的一个数据库中回显每一行用户。我可能是错的,但我已经用 Google 搜索过并搜索过这个网站的帖子......我认为还没有像这个问题那样解决过的问题。
在我按照教程/练习使用 PHP 的实践中,我当然发现 MySQL 现在更喜欢 PDO 或 MySQLi,所以我决定尝试使用 MySQLi。
奇怪的是,我已经包含了配置 MySQL 连接的 PHP 文件,并且我知道连接变量被正确调用,因为我什至打印出查询的结果,即变量被分配到:
mysqli_result Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 1 [type] => 0 )
以上是我使用时的输出:
print_r($db_con->query("SELECT * FROM users"));
然而,当我尝试将结果分配给变量(如 $user_rs)并打印该变量的值时,我在第 58 行得到“注意:未定义变量:C:\wamp\www\index.php 中的 user_rs 。”
这是我的文件。
dbconfig.php:
<?php
$db_con= new mysqli("localhost","root","abc123", "OTonGadgetHub");
if($db_con->connect_errno){
echo $db_con->connect_error;
}
?>
index.php:
<?php session_start();
include ("dbconfig.php"); ?>
<html>
<head>
<title>My Webpage</title>
<link rel = "stylesheet" type = "text/css" href = "site.css" />
<link rel="shortcut icon" href="index.html?img=favicon" type="image/ico" />
<script>
function validatingForm(){
var x;
var y;
x = document.getElementById('nameCheck').value;
y = document.getElementById('password').value;
if(x !="" && y !=""){
return true;
}
else if(x =="" && y == ""){
document.getElementById('errorMsg').innerHTML='<font color="red">(required) Name:</font>';
document.getElementById('errorPass').innerHTML='<font color="red">(required) Password:</font>';
return false;
}
else if(x =="" && y!=""){
document.getElementById('errorMsg').innerHTML='<font color="red">(required) Name:</font>';
document.getElementById('errorPass').innerHTML='<font color="blue">Password:</font>';
return false;
}
else if(y =="" && x!=""){
document.getElementById('errorPass').innerHTML='<font color="red">(required) Password:</font>';
document.getElementById('errorMsg').innerHTML='<font color="blue">Name:</font>';
return false;
}
}
</script>
</head>
<body>
<?php include("header.php"); ?>
<?php
if (isset($_SESSION['user']))
echo "<p class ='welcome' id='greeting'> Hi, ". $_SESSION['user'] . "! Welcome to the site!</p>";
else
echo "<p class ='welcome' id='greeting'> Please Login:</p>
<form action='welcome.php' method='post'>
<center><b id = 'errorMsg'>Name:</b>
<input type='text' id='nameCheck' name = 'username' /></center>
<br />
<center><b id='errorPass'>Password:</b> <input type='text' id ='password' name = 'password'/></center>
<br /><br />
<center><input type='submit' value='Log In' onClick='return validatingForm()'/></center>
</form>";
?>
<?php
if (isset($_SESSION['user']))
$user_rs = $db_con->query("SELECT * FROM users");
print_r($db_con->query("SELECT * FROM users"));
print_r($user_rs);
echo "<center><h1> User List:</h1>
<table border='1'>
<tr>
<td><b>User</b></td>
<td><b>Login password</b></td>
</tr>
<tr>";
while($record = $user_rs->fetch_object()){
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['username'] . "</td>";
echo "</tr>";
}
echo"</table></center>";
?>
<?php if (isset($_SESSION['user']))
echo "<center><a href='logout.php'>Logout</a></center>";?>
<p class = "content"> This is a page that is a scrap work in progress. </p>
<?php include("footer.php"); ?>
</body>
</html>
我还有其他文件,但相信这些文件超出了问题的范围。但是,如果需要,我很乐意提供其他文件。
提醒:我担心的是我基本上不能使用应该分配给 $user_rs 的结果。主要是,即使我似乎已将其正确分配给查询,它似乎也没什么。提前感谢您的帮助。
【问题讨论】:
-
您正在获取 $record 作为一个对象,但试图将其显示为一个数组:
$record->id和$record->username -
正如@Mark 所说 - 试试这个语法:
$record->id和$record->username~ 但是为什么要运行两次查询? -
@RamRaider 为什么要运行两次查询?我不确定我明白你的意思。
-
我喜欢你关于使用 mysqli 对象语法来获取所述对象的属性的观点,并且我已经相应地更改了我的代码。但是,这仍然不能回答为什么我不能将其分配给变量的百万美元问题。我还注意到,将语法更改为属性语法也会无限重复用户表中存在的一条记录。我错过了另一个细节吗?
-
第一次运行查询后直接使用
print_r($db_con->query("SELECT * FROM users"));