我試圖使用一個查詢來查找與在文本輸入框中輸入的用戶名相關的用戶標識。然後,我嘗試使用我在上一個查詢中找到的用戶標識從同一數據庫的另一個表中選擇詳細信息。然而,當我嘗試這個,我得到的錯誤:如何在另一個查詢(PHP)中使用一個SQL查詢的結果
Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/jack/public_html/viewfriendlogbook.php on line 22
我認爲,這是因爲我想使用這個變量實際上是一個對象,而不是一個整數,但我不知道如何解決這個問題。任何幫助將不勝感激。
我的PHP:
<?php
SESSION_START();
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSWORD";
$dbname = "MY DATABASE NAME";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user= $_POST['textinput'];
$finduserid = "SELECT `user-id` FROM `users` WHERE `username` = $user";
$requesteduserid = mysqli_query($conn,$finduserid);
echo $requesteduserid ;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '$requesteduserid'";
$result = mysqli_query($conn,$sql);
echo '<style>
table, th, td {
border: 1px solid black;
}
</style>';
if ($result->num_rows > 0) {
echo "<table><tr><th>Climb</th><th>Crag</th><th>Grade</th> <th>Description</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climbname"]. "</td><td>" . $row["cragname"]. "</td><td> " . $row["grade"]. "</td><td>" . $row["description"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
此主題可能對您有幫助嗎? http://stackoverflow.com/questions/21722375/object-of-class-mysqli-result-could-not-be-converted-to-string-in – Tina