因此,我試圖在我最近在我的WAMP服務器2.5版上設置的網站上從我的數據庫中的一個數據庫中回覆每行用戶。我可能是錯的,但我已經谷歌搜索,也搜索這個網站的帖子......我不認爲有這樣的問題,像這個問題還沒有解決。無法將PHP變量分配給mysqli查詢
在我通過以下教程/練習使用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,並打印變量的值,我得到「注意:未定義的變量:user_rs在li上的C:\ wamp \ www \ index.php中ne 58.「
這是我的文件。
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的結果。主要的是,即使我似乎已經正確地將它分配給查詢,它似乎沒有任何作用。先謝謝您的幫助。
你正在提取$ reco rd作爲一個對象,但試圖顯示它,就好像它是一個數組:'$ record-> id'和'$ record-> username' –
正如@Mark所說 - 試試這個語法:'$ record-> id'和'$ record-> username'〜但爲什麼要運行查詢兩次? – RamRaider
@RamRaider爲什麼運行查詢兩次?我不確定我明白你的意思。 –