My php While loop run the query, but the results must be print inside html. In this moment I unknow the way to make this:
My php while loop
<?php
include "connect.php";
$username=$_SESSION['Username'];
$result = mysqli_query($dbconn,"
SELECT *
FROM books
WHERE username = '$Username'
");
while($rows = mysqli_fetch_array($result));
?>
After this code there is a Html code where I want print the variables:
<a href="editBook.php?bookid=<?php echo $book_id; ?>&book_name=<?php echo $book_name; ?>">Edit</a>
In this moment the variable is empty
How to fix this?
[Resolved] Update I have resolve my problem. This is the correct php script. Work fine:
<?php
include "connect.php";
$username=$_SESSION['Username'];
$result = mysqli_query($dbconn,"
SELECT *
FROM books
WHERE username = '$Username'
");
global $book_id, $book_name
while($row = mysqli_fetch_array($result)) {
$book_id = row['book_id'];
$book_name = row['book_name'];
?>
Outside while loop. Print variable inside Html:
<?php echo $row['book_id']; ?> <br>
<?php echo $row['book_name']; ?>
Close while loop and connection:
<?php
}
mysqli_close($dbconn);
?>
with prepared statements :
<?php
session_start();
$username = $_SESSION['Username'];
error_reporting(E_ALL);
ini_set('display_errors', 1);
include"config.inc.php";
/* connect to DB */
$mysqli = mysqli_connect("$host", "$user", "$mdp", "$db");
if (mysqli_connect_errno()) { echo "Error connecting : " . mysqli_connect_error($mysqli); }
$query = " SELECT * FROM books WHERE username=? ";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s", $username);
$results = $stmt->execute();
$stmt->bind_result($book_id, $book_name);
$stmt->store_result();
if ($stmt->num_rows > 0) {
while($stmt->fetch()){
?>
<p><?php echo"$book_name"; ?> > <a href="editBook.php?bookid=<?php echo"$book_id"; ?>&book_name=<?php echo"$book_name"; ?>">Edit</a></p>
<?php
}
}
else
{ echo"[ no data ]"; }
?>
(Rewriting)
The real issue is:
while ($rows = ...) ;
This loops until $rows
is NULL
. So there is nothing to display afterwards.
Since you are fetching the entire array in a single function call, there is no need for the loop!
$rows = ...;
But then you need to reference the first(?) row to get the desired data:
$row = $rows[0];
So, another approach is to just fetch one row, then close the fetch process.
在你的HTML中,你必须这样做
<a href="editBook.php?bookid=<?php echo $rows['book_id']; ?>&book_name=<?php echo $rows['book_name']; ?>">Edit</a>
As there might be multiple books for each user, you have to print the link inside the while
loop, or store it in a string:
<?php
include "connect.php";
$username = $_SESSION['Username'];
$result = mysqli_query($dbconn,"
SELECT * FROM books
WHERE username = '$Username'
");
$links = ""; // all links are stored in this string
while($rows = mysqli_fetch_array($result)) {
// I assume that the columns are called `id` and `name`
$links .= '<a href="editBook.php?bookid='. $rows["id"] .'&book_name='. $rows["name"] .'">Edit '. $rows["name"] .'</a>';
}
?>
In the html code simply write
<?php echo $links ?>
Note that you should use prepared statements instead. You should also take a look at the object oriented way to use mysqli using the mysqli
class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.