0

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);
?>
19
  • show your html code as well. Commented Apr 8, 2017 at 19:12
  • Sorry I have update the post. Read please. Commented Apr 8, 2017 at 19:16
  • 2
    you need echo $rows['column_name']; Commented Apr 8, 2017 at 19:17
  • 1st of all, you should really think about prepared statements, then, add error_reporting(E_ALL); ini_set('display_errors', 1); on top of the page, and see what error shows up. Then, echo $rows['my_column_name_whatever_is_name_is'] and see what it returns Commented Apr 8, 2017 at 19:19
  • @OldPadawan he is not searching for an error, he is looking for a solution. Commented Apr 8, 2017 at 19:21

4 Answers 4

1

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 ]"; }
?>
Sign up to request clarification or add additional context in comments.

Comments

1

(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.

Comments

0

In your html you have to do this

<a href="editBook.php?bookid=<?php echo $rows['book_id']; ?>&book_name=<?php echo $rows['book_name']; ?>">Edit</a>

Comments

0

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.

3 Comments

your code lacks a session_start, and all links will be stuck together, not knowing which one to edit, just a link... :/
No, my code does not lack a session start. I modified the code above which does not have a session_start either. Most likely, the session is started in connect.php. You're right that the links should have a more descriptive name, I corrected it.
we now lack an answer to his last comment :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.