This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP not be able to echo data
I'm trying to get data from database, and echo them on the page using their unique id.. below is my code
<?php
session_start();
require_once('config.php');
//create query statement
$query = 'SELECT * FROM player_info';
//make the query from db
$myData = mysql_query($query, $conn)
OR exit('Unable to select data from table');
while($row = mysql_fetch_array($myData, MYSQL_ASSOC)){
$playerID = $row['id'];
$player_bio = $row['player_bio'];
$achievements = $row['player_achts'];
}
?>
and here is how i code for echo the data
<?php
if ($playerID == 1)
{
echo '<p class="playerAchievesL">' . $achievements . '</p><p class="playerInfoL">' . $player_bio . '</p>';
}
?>
I don't get any error return from php, but the data does not display anything... help please ... thank you so much
If I understand correctly, you need to place the echo inside the while() loop:
while($row = mysql_fetch_array($myData, MYSQL_ASSOC)){
$playerID = $row['id'];
$player_bio = $row['player_bio'];
$achievements = $row['player_achts'];
echo '<p class="playerAchievesL">' . $achievements . '</p><p class="playerInfoL">' . $player_bio . '</p>';
}
You fetch all players from the table, and for each player, overwrite $playerID, $player_bio and $achievements. After the while loop, $playerID contains the identifier of the last fetched player.
If you have more than one player, this most likely is not $playerID with 1.
problem is in here. you're overwriting the values on every iteration
while($row = mysql_fetch_array($myData, MYSQL_ASSOC)){
$playerID = $row['id'];
$player_bio = $row['player_bio'];
$achievements = $row['player_achts'];
}
Just echo them directly in the loop:
while($row = mysql_fetch_array($myData, MYSQL_ASSOC)){
echo $row['id'];
echo $row['player_bio'];
echo $row['player_achts'];
}
Your while loop may be overwriting $pkayerID, so if the last playerID isn't 1 the it won't echo anything.
Try putting the echo line after the $achievemets line to see if this is the case.
Related
I have an query that shows data from db. I use while loop to display data. Problem is that I can call (echo) result only once.
Here is my code:
$ime_ = "SELECT * FROM `users` WHERE '" . ($_COOKIE['username']) . "' = user_username";
$ime_result = $mysqli->query($ime_);
and later in my html I use this result as:
<?php
if ($ime_result->num_rows > 0)
while($row = $ime_result->fetch_assoc()) {
echo "<h2>" . $row["Ime"] . "</h2>";
}
?>
This work ok, but I want to use this result to display many times in my html. And when copy while loop again later in html no result is given.
Store the string with data from while() into a variable, than apply echo to that variable as many times as you like..
$re_out ='';
if($ime_result->num_rows > 0){
while($row = $ime_result->fetch_assoc()) {
$re_out .="<h2>". $row["Ime"] ."</h2>";
}
}
echo $re_out;
//etc..
echo $re_out;
<?php
if ($ime_result->num_rows > 0)
$ime_result->data_seek(0); // seek to row no. 1
while($row = $ime_result->fetch_assoc()) {
echo "<h2>" . $row["Ime"] . "</h2>";
}
?>
Haven't run script. Try if it works.
I have problem with my code.
I use nested while in my code and the nested while doesn't work, only the outer while is work
I don't know where is the bug of my code.
This is the piece of my code :
$database = new mysqli('127.0.0.1', 'user', 'user', 'mini_email');
$query = 'SELECT * FROM mails';
$query2 = 'SELECT * FROM users';
$result_set = $database->query($query);
$result2 = $database->query($query2);
while ($row2 = $result2->fetch_assoc()) {
if ($row2['username'] == $_SESSION['user']) {
$id_email = $row2['id'];
}
}
if (isset($_SESSION['is_logged_in'])) {
echo('<center><font size="10">Mailing List</font></center><br><br>');
echo('<center><table border="3" bgcolor="#f0cdfa">');
echo('<tr>');
echo ('<td>No</td>');
echo ('<td>ID</td>');
echo ('<td>From</td>');
echo('<td>Subject</td>');
echo ('<td>Message</td>');
echo ('<td>Action</td>');
echo ('</tr>');
$i = 1;
while ($row = $result_set->fetch_assoc()) {
if ($row['to_user_id'] == $id_email) {
echo('<tr>');
echo ('<td>' . $i . '</td>');
echo ('<td>' . $row['id'] . '</td>');
while($row2 = $result2->fetch_assoc()){
if($row2['id']== $row['from_user_id']){
echo ('<td>' . $row2['username'] . '</td>');
}
}
echo ('<td>' . $row['subject'] . '</td>');
echo ('<td>' . $row['message'] . '</td>');
echo ('<td>View</td>');
echo ('</tr>');
$i++;
}
}
Your initial
while ($row2 = $result2->fetch_assoc()) {
is looping through all the records to the end of the set,
so looping a second time won't retrieve any further records because you're already at the end of the resultset
Resultset pointers aren't automatically reset when you initiate a new loop, but you can reset them manually using
$result2->data_seek(0);
before each subsequent loop
But iterating the point made in the comments by #Sirko you'd be better using a JOIN to make a single query
If I may help, I think you shouldn't even perform the queries when your if statement with the session returns false.
In the first lines or your script you should define a function with all this code, and write it like this :
function YourFunction()
{
if (isset($_SESSION['is_logged_in'])) {
return false;
}
// Database connection and queries
// your business
}
This will be more clean.
Next, your query doesn't seem to be correct. You get all the mails of your website, and then in a loop you check if the mail user id is the user id you have in session. Imagine the lack of performance if you have 35 000 mails in your database.
Improve your query with a WHERE statement : http://www.w3schools.com/sql/sql_where.asp
then, are you sure that $_SESSION['user'] contains an id ? I think your second if statement never returns true because of that.
I have this code
$result = mysql_query("SELECT name from room");
$data = mysql_fetch_array($result);
$firstValue="";
while($data = mysql_fetch_array($result)){
if($firstValue==""){
$firstValue=$data['name'];
}
if(isset($_POST["occupant"])and trim($_POST["choice"])==$data['name']){
echo '<option selected="selected" value="'.$data['name'].'" >'.$data['name'];
echo '</option>';
}else
{
echo '<option value="'.$data['name'].'" >'.$data['name'];
echo '</option>';
}
}
What i want here is to show all data from my database but when i open it in browser shows only the second data down to the last data. And i wonder why the first data is missing.
Can anybody knows my mistake here?
Get rid of the first:
$data = mysql_fetch_array($result);
It is popping the first record off of your result set.
$result = mysql_query("SELECT name from room");
$firstValue="";
while($data = mysql_fetch_array($result)){
I'm having trouble with a couple of pages: I want to list all my images from a database (urls stored) and then have the user click the image which takes them to another page which then gives the rest of the information from the database.
I'm using a Session variable to send the ID of the image which is then used on the other page to select information from the database before viewing. The problem I have is that it always seems to show the last entered detail on the database and not the actual information from the variable of the image clicked.
I'm not sure where this is going wrong, I've since added plenty of session_destroy() commands which I thought was the initial problem but have not solved the issue and appear only to clog my code.
Any help or direction would be greatly appreciated.
Here's my code:
Page 1 - Sending variable through Sessions
<?php
//code to access database
$query = "SELECT * FROM releases";
$result = mysql_query($query);
if(!$result) die ("Database access failed: " .mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo '<img src="images/releases/' . $row[1] .'" id="' . $row [0] . '"/>';
}
$id = $row [0]; //"row 0" is the auto increment of the database id.
if (isset($_SESSION ['id'])){
session_destroy();
}
session_start();
$_SESSION ['id']= $id;
mysql_close($db_server);
?>
Page 2 - Receiving the variable and then using it to access the database.
<?php
//code to access database
if (isset($_SESSION ['id'])){
session_destroy();
}
session_start();
$id = $_SESSION['id'];
$query = "SELECT * FROM releases WHERE release_id = $id";
$result = mysql_query($query);
if(!$result) die ("Database access failed: " .mysql_error());
$row = mysql_fetch_row($result);
echo "<img src='images/events/" .$row[1] . "' width= '150' height= '150' /> <br />";
echo "track title: " .$row[2] . "<br />";
echo "artist: " .$row[3] . "<br />";
session_destroy();
mysql_close($db_server);
?>
Look at what your code is doing:
for ($j ...) {
$row = mysql_fetch_row($result);
... do stuff with $row;
}
$id = $row[0];
you set $id AFTER the loop has terminated, meaning you will only EVER get the LAST item fetch from the query results.
Even if you were setting $id inside the loop, you'd still be only setting a SINGLE id in $_SESSION anyways:
for ($j ...) {
$id = $row[0];
$_SESSION['id'] = $id;
}
when the loop finishes, $_SESSION will be the last id, yet again. What you're doing does NOT need a session, just a query variable:
for ($j ...) {
$id = $row[0];
echo "<a href='test.php?id=$id'>click me</a>";
^^^^^^^---note the query string....
}
and then in your test.php page:
$id = $_GET['id'];
I have used urldecode to receive a member ID from a previous site. The correct ID is being displayed in the URL but I can't fetch information from the database.
members.php:
<?php
$query = "SELECT name, memberID FROM members";
if(!$result = $db->query($query)){
die('There was an error running your query[' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
printf ('<li>' . $row['name'] . '</li>');
}
?>
profiles.php:
<?php
$id = isset($_GET['memberID']);
$query = "SELECT * FROM members WHERE memberID = '".$id."'";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()){
printf("%s (%s)\n", $row["memberID"], $row['name']);
}
}
var_dump($query);
?>
All I get is a blank screen.
I found couple of problems in the code:
members.php
while($row = $result->fetch_assoc()){
printf ('<li>' . $row['name'] . '</li>');
}
Here you are using printf function which have 1st argument for format of string.
Correct that with echo statement as below:
while($row = $result->fetch_assoc()){
echo '<li>' . $row['name'] . '</li>';
}
profiles.php
$id = isset($_GET['memberID']);
Here you are setting the $id with isset() function return value.
You should instead set the value from GET parameter as below:
if(isset($_GET['memberID'])) $id = $_GET['memberID'];
See now if it's working.
Make sure that you use the correct capitalization of memberId vs. memberID. This is very important.
Do not pass values retrieved from GET/POST through urldecode. They already are.
Please try the following based on your code and let us know the results:
<?php
$id = isset($_GET['memberID']) ? $_GET['memberID'] : 0;
if($id > 0){
$query = "SELECT * FROM members WHERE memberID = '".$id."'";
$result = $db->query($query);
if($result){
echo "Rows found: " + $result->num_rows;
} else {
echo "No rows found";
}
} else {
echo "memberID is 0";
}
?>
Is memberID an int field in the database or a string field? If it is an int field then remove the single quotes in your query on profiles.php.