无法将PHP变量分配给mysqli查询
因此,我正在尝试从我最近在WAMP服务器2.5版上建立的网站上的一个数据库中回显每一行用户。我可能错了,但我已经在谷歌上搜索了这个网站的帖子。。。我认为还没有解决过像这样的问题 在我通过以下教程/练习使用PHP的实践中,我当然发现MySQL现在更喜欢PDO或MySQLi,所以我决定尝试使用MySQLi 奇怪的是,我包含了配置MySQL连接的PHP文件,我知道连接变量被正确调用,因为我甚至打印了变量被分配到的查询结果:无法将PHP变量分配给mysqli查询,php,mysqli,Php,Mysqli,因此,我正在尝试从我最近在WAMP服务器2.5版上建立的网站上的一个数据库中回显每一行用户。我可能错了,但我已经在谷歌上搜索了这个网站的帖子。。。我认为还没有解决过像这样的问题 在我通过以下教程/练习使用PHP的实践中,我当然发现MySQL现在更喜欢PDO或MySQLi,所以我决定尝试使用MySQLi 奇怪的是,我包含了配置MySQL连接的PHP文件,我知道连接变量被正确调用,因为我甚至打印了变量被分配到的查询结果: mysqli_result Object ( [current_field]
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\r的结果。主要是,它似乎什么都没有,即使我似乎已将其正确分配给查询。提前感谢您的帮助。这显然是一个类似打字错误的错误 本质上,它询问如何将变量分配给表达式结果,答案显而易见:
$user_rs = $db_con->query("SELECT * FROM users");
这个问题可以归结为
为什么我从下面的代码中得到未定义的变量:用户错误
答案是这是不可能的
检查您正在运行的文件以及类似的内容。您的db查询将记录集作为对象获取,但您试图使用数组语法引用字段
<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;
var e;
var p;
x = document.getElementById('nameCheck').value;
y = document.getElementById('password').value;
e = document.getElementById('errorMsg');
p = document.getElementById('errorPass');
if(x !="" && y !=""){
return true;
} else if(x =="" && y == ""){
e.innerHTML='<font color="red">(required) Name:</font>';
p.innerHTML='<font color="red">(required) Password:</font>';
return false;
} else if(x =="" && y!=""){
e.innerHTML='<font color="red">(required) Name:</font>';
p.innerHTML='<font color="blue">Password:</font>';
return false;
} else if(y =="" && x!=""){
e.innerHTML='<font color="red">(required) Password:</font>';
p.innerHTML='<font color="blue">Name:</font>';
return false;
}
}
</script>
</head>
<body>
<?php
include("header.php");
if ( isset( $_SESSION['user'] ) ){
/* Use is logged in */
echo "
<p class ='welcome' id='greeting'>
Hi, ". $_SESSION['user'] . "! Welcome to the site!
</p>";
} else {
/* User is NOT logged in */
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>";
}
if ( isset( $_SESSION['user'] ) ){
/* Query the db, once! */
$user_rs = $db_con->query("SELECT * FROM `users`;");
/* Assume there is a recordset and create the table */
echo "
<center>
<h1> User List:</h1>
<table border='1'>
<tr>
<td><b>User</b></td>
<td><b>Login password</b></td>
</tr>";
/* loop through recordset - new row for each user */
while( $record = $user_rs->fetch_object() ){
echo "
<tr>
<td>" . $record->id . "</td>
<td>" . $record->username . "</td>
</tr>";
}
/* Close the table */
echo "
</table>
</center>";
}
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. I WILL use CSS to do my presentation in future '-)</p>
<?php
include("footer.php");
?>
</body>
</html>
非常感谢您就这个问题给我指点和指导 我真的很喜欢给出的答案,但不是因为如果我接受RamRaider的答案可能会有人认为的原因 所以我想我应该把它作为一个问题的实际答案。我的问题似乎是,我没有用花括号清楚地定义PHP的作用域。我在代码中漏掉了这些内容,因此,当我漏掉检查用户会话的if语句的括号时,它似乎变得混乱起来。可怜的PHP服务器:我真的滥用了它 如果If-else语句的作用域跨越一行或两行以上的代码,请始终找到一种方法,用正确的语法将作用域括起来。否则你将面临艰难的旅程 话虽如此,关于只查询一次数据库并调用属性就好像我在查询对象一样,这对我这个新手来说仍然很有帮助。在编写mysqli语句时也要注意这一点
非常感谢大家。您正在获取$record作为对象,但试图将其显示为数组:$record->id和$record->username,正如@Mark所说的那样-尝试以下语法:$record->id和$record->username~但为什么要运行两次查询?@RamRaider为什么要运行两次查询?我不确定我是否理解你的意思。我喜欢你关于使用mysqli对象语法获取所述对象的属性的观点,并且我相应地修改了代码。然而,这仍然不能回答百万美元的问题,为什么我不能把它赋值给一个变量。我还注意到,将语法更改为属性语法也会无限重复用户表中存在的一条记录。我错过了另一个细节吗?您使用print\r$db\u con->querySELECT*FROM users;在第一次运行查询之后,您对这种错误有一个很好的看法,据我所知,这种情况不应该发生。在经历了许多挫折之后,我尝试在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;
var e;
var p;
x = document.getElementById('nameCheck').value;
y = document.getElementById('password').value;
e = document.getElementById('errorMsg');
p = document.getElementById('errorPass');
if(x !="" && y !=""){
return true;
} else if(x =="" && y == ""){
e.innerHTML='<font color="red">(required) Name:</font>';
p.innerHTML='<font color="red">(required) Password:</font>';
return false;
} else if(x =="" && y!=""){
e.innerHTML='<font color="red">(required) Name:</font>';
p.innerHTML='<font color="blue">Password:</font>';
return false;
} else if(y =="" && x!=""){
e.innerHTML='<font color="red">(required) Password:</font>';
p.innerHTML='<font color="blue">Name:</font>';
return false;
}
}
</script>
</head>
<body>
<?php
include("header.php");
if ( isset( $_SESSION['user'] ) ){
/* Use is logged in */
echo "
<p class ='welcome' id='greeting'>
Hi, ". $_SESSION['user'] . "! Welcome to the site!
</p>";
} else {
/* User is NOT logged in */
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>";
}
if ( isset( $_SESSION['user'] ) ){
/* Query the db, once! */
$user_rs = $db_con->query("SELECT * FROM `users`;");
/* Assume there is a recordset and create the table */
echo "
<center>
<h1> User List:</h1>
<table border='1'>
<tr>
<td><b>User</b></td>
<td><b>Login password</b></td>
</tr>";
/* loop through recordset - new row for each user */
while( $record = $user_rs->fetch_object() ){
echo "
<tr>
<td>" . $record->id . "</td>
<td>" . $record->username . "</td>
</tr>";
}
/* Close the table */
echo "
</table>
</center>";
}
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. I WILL use CSS to do my presentation in future '-)</p>
<?php
include("footer.php");
?>
</body>
</html>