Never Displays the first row - php

When I run the code in $Date within phpmyadmin it returns the correct number of rows but on the page it will never display the first row for some reason and I know it is not the numbering being off because my laptop Id is different for the two rows
function select_History_Date($link){
$status = '';
$i = 1;
$date = "SELECT Student_Id, Loaner_Laptop_Id, HDD_Id, "
. "Phone_Id, date_returned "
. "FROM equipment_history WHERE Loaner_Laptop_Id "
. "IS NOT NULL OR HDD_Id IS NOT NULL OR Phone_Id IS NOT NULL";
$ret = mysqli_query($link, $date);
$row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC);
if (!$ret) {
die('Could not execute select statement:' . mysqli_errno($link));
} else {
while ($row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC)) {
$status .= '<tr>';
$status .= '<td>' . $i . '</td>';
if (is_null($row1['date_returned']) && is_null($row1['HDD_Id']) && is_null($row1['Phone_Id'])){
$status .= '<td><a href="ReturnLaptop.php?laptopId=' .
$row1['Loaner_Laptop_Id'] . '&studentId='.
$row1['Student_Id']. '">'. 'Return'. '</td>';
} elseif (is_null($row1['date_returned']) && is_null($row1['HDD_Id']) && is_null($row1['Loaner_Laptop_Id'])){
$status .= '<td><a href="ReturnPhone.php?phoneId=' .
$row1['Phone_Id'] . '&studentId='.
$row1['Student_Id']. '">'. 'Return'. '</td>';
}elseif (is_null($row1['date_returned']) && is_null($row1['Phone_Id']) && is_null($row1['Loaner_Laptop_Id'])){
$status .= '<td><a href="ReturnHDD.php?hddId=' .
$row1['HDD_Id'] . '&studentId='.
$row1['Student_Id']. '">'. 'Return'. '</td>';
}else{
$status .= '<td>' . $row1['date_returned'] . '</td>';
}
$status .= '</tr>';
$i++;
return $status;
}
}
}

This line:
$row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC);
is reading the first row of data from your result. But then you are not doing anything with it before you do
while ($row1 = mysqli_fetch_array($ret, MYSQLI_ASSOC)) {
which overwrites the data with the values from the second row. You need to delete the first line.

Related

Why is my php script not showing all data from mysql? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a php script that should change the TeamSpeak group of specific users.
For getting a bit visual aspect I added a table, so I can see what for users should get the groups.
My whole php script:
<?php
date_default_timezone_set("Europe/Berlin");
require ("config.php");
require ("groups.php");
include ("datenbank.php");
require_once("libraries/TeamSpeak3/TeamSpeak3.php");
$ts3_VirtualServer = TeamSpeak3::factory("serverquery://" . $cfg["user"] . ":" . $cfg["pass"] . "#" . $cfg["host"] . ":" . $cfg["query"] . "/?server_port=9987");
$user_check_confirmed = "SELECT * FROM Users WHERE TSUUIDconfirmed = '1'";
$result = mysqli_query($db, $user_check_confirmed);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
if($row['MainSteamGroup'] == '1' || $row['vip'] == '1'){
echo '<table border="1">';
echo '<thead>';
echo '<tr class="table-head">';
echo '<th class="column1">Username</th>';
echo '<th class="column2">Rocket League</th>';
echo '<th class="column3">CS:GO</th>';
echo '<th class="column1">FACEIT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($fetch = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo '<td class="column1">' . $fetch['Username'] . '</td>';
echo '<td class="column2">' . $fetch['RLHighestRank'] . '</td>';
echo '<td class="column3">' . $fetch['CSGOMM'] . '</td>';
echo '<td class="column1">' . $fetch['FACEITLVL'] . '</td>';
echo "</tr>";
}
echo '</tbody>';
echo '</table>';
echo '<br><br><br>';
// Rocket League
if($row['RLHighestRank'] == 'none'){
} else {
if($row) {
if($row['RLHighestRank'] == $row['RLHighestRankSet']){
}
}
if($row) {
if($row['RLHighestRank'] !== $row['RLHighestRankSet'] && $row['RLHighestRankSet'] !== '0' && strpos($row['activeranks'], 'Rocket League') !== false){
try {
$client = $ts3_VirtualServer->clientFindDb($row['TeamSpeakUUID'], true);
if( $ts3_VirtualServer->serverGroupClientDel($rl[$row['RLHighestRankSet']], $client[0]));
if( $ts3_VirtualServer->serverGroupClientAdd($rl[$row['RLHighestRank']], $client[0]));
$sql = "UPDATE Users SET RLHighestRankSet='".$row['RLHighestRank']."' WHERE TeamSpeakUUID='".$row['TeamSpeakUUID']."'";
if ($db->query($sql) === TRUE) { }
} catch(Exception $e) {
echo "<br>" . $row['Username'] . " Fehler!<br/>ErrorID: <b>". $e->getCode() ."</b>; [RocketLeague1] Error Message: <b>". $e->getMessage() ."</b><br>;";
}
}
}
if($row) {
if($row['RLHighestRankSet'] == '0' && strpos($row['activeranks'], 'Rocket League') !== false ){
try {
$client = $ts3_VirtualServer->clientFindDb($row['TeamSpeakUUID'], true);
if( $ts3_VirtualServer->serverGroupClientAdd($rl[$row['RLHighestRank']], $client[0]));
$sql = "UPDATE Users SET RLHighestRankSet='".$row['RLHighestRank']."' WHERE TeamSpeakUUID='".$row['TeamSpeakUUID']."'";
if ($db->query($sql) === TRUE) { }
} catch(Exception $e) {
echo "<br>" . $row['Username'] . " Fehler!<br/>ErrorID: <b>". $e->getCode() ."</b>; [RocketLeague2] Error Message: <b>". $e->getMessage() ."</b><br>;";
}
}
}
}
}
}
For testing I set every user in the db as "vip". Overall I have 8 users, so all the users should get their rank.
If I open the php site now, I see 4 users in den table and the others are getting the rank updated, so why is my script not working how it should like?
I mean, every user have like the same database entry, the only things that differs are the names and their ranks...
The structure here is a little difficult to grasp. Can you show some example data?
You have a duplicate while loop which will likely cause an issue. I had a little cleanup which might be worth running:
<?php
date_default_timezone_set("Europe/Berlin");
require("config.php");
require("groups.php");
include("datenbank.php");
require_once("libraries/TeamSpeak3/TeamSpeak3.php");
$ts3_VirtualServer = TeamSpeak3::factory("serverquery://" . $cfg["user"] . ":" . $cfg["pass"] . "#" . $cfg["host"] . ":" . $cfg["query"] . "/?server_port=9987");
$user_check_confirmed = "SELECT * FROM Users WHERE TSUUIDconfirmed = '1'";
$result = mysqli_query($db, $user_check_confirmed);
if (mysqli_num_rows($result) > 0) {
echo '<table border="1">';
echo '<thead>';
echo '<tr class="table-head">';
echo '<th class="column1">Username</th>';
echo '<th class="column2">Rocket League</th>';
echo '<th class="column3">CS:GO</th>';
echo '<th class="column1">FACEIT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo '<td class="column1">' . $fetch['Username'] . '</td>';
echo '<td class="column2">' . $fetch['RLHighestRank'] . '</td>';
echo '<td class="column3">' . $fetch['CSGOMM'] . '</td>';
echo '<td class="column1">' . $fetch['FACEITLVL'] . '</td>';
echo "</tr>";
}
echo '</tbody>';
echo '</table>';
echo '<br><br><br>';
// Rocket League
if (isset($row['RLHighestRank']) && $row['RLHighestRank'] != 'none') {
if ($row['RLHighestRank'] !== $row['RLHighestRankSet'] && $row['RLHighestRankSet'] !== '0' && strpos($row['activeranks'], 'Rocket League') !== false) {
try {
$client = $ts3_VirtualServer->clientFindDb($row['TeamSpeakUUID'], true);
if ($ts3_VirtualServer->serverGroupClientDel($rl[$row['RLHighestRankSet']], $client[0])) ;
if ($ts3_VirtualServer->serverGroupClientAdd($rl[$row['RLHighestRank']], $client[0])) ;
$sql = "UPDATE Users SET RLHighestRankSet='" . $row['RLHighestRank'] . "' WHERE TeamSpeakUUID='" . $row['TeamSpeakUUID'] . "'";
if ($db->query($sql) === true) {
}
}
catch (Exception $e) {
echo "<br>" . $row['Username'] . " Fehler!<br/>ErrorID: <b>" . $e->getCode() . "</b>; [RocketLeague1] Error Message: <b>" . $e->getMessage() . "</b><br>;";
}
}
if ($row['RLHighestRankSet'] == '0' && strpos($row['activeranks'], 'Rocket League') !== false) {
try {
$client = $ts3_VirtualServer->clientFindDb($row['TeamSpeakUUID'], true);
if ($ts3_VirtualServer->serverGroupClientAdd($rl[$row['RLHighestRank']], $client[0])) ;
$sql = "UPDATE Users SET RLHighestRankSet='" . $row['RLHighestRank'] . "' WHERE TeamSpeakUUID='" . $row['TeamSpeakUUID'] . "'";
if ($db->query($sql) === true) {
}
}
catch (Exception $e) {
echo "<br>" . $row['Username'] . " Fehler!<br/>ErrorID: <b>" . $e->getCode() . "</b>; [RocketLeague2] Error Message: <b>" . $e->getMessage() . "</b><br>;";
}
}
}
}
That said, the conditions on this line in particular look quite complicated:
if ($row['RLHighestRank'] !== $row['RLHighestRankSet'] && $row['RLHighestRankSet'] !== '0' && strpos($row['activeranks'], 'Rocket League') !== false) {
Seeing the accompanying data with anything sensitive removed will help further.

Returning records from database in to the table with php

I have this script where I am for first time returning records in to the table. It works quite fine but, it is returning only last added record. Can you look at it, because I cant find what is wrong.
$query = 'SELECT character_id, alias, real_name, alignment
FROM comic_character
ORDER BY ' . $order[$o];
$result = mysql_query($query, $db) or die (mysql_error($db));
if (mysql_num_rows($result) > 0) {
echo '<table>';
echo '<tr><th>Alias</th>';
echo '<th>Real Name</th>';
echo '<th>Alignment</th>';
echo '<th>Powers</th>';
echo '<th>Enemies</th></tr>';
$odd = true;
while ($row = mysql_fetch_array($result)) {
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
echo '</td><td>' . $row['alias'] . '</td>';
echo '<td>' . $row['real_name'] . '</td>';
echo '<td>' . $row['alignment'] . '</td>';
$query2 = 'SELECT power FROM comic_power p
JOIN comic_character_power cp
ON p.power_id = cp.power_id
WHERE cp.character_id = ' . $row['character_id'] . '
ORDER BY power ASC';
$result2 = mysql_query($query2, $db) or die(mysql_error($db));
if (mysql_num_rows($result2) > 0) {
$powers = array();
while ($row2 = mysql_fetch_assoc($result2)) {
$powers[] = $row2['power'];
}
echo '<td>' . implode(',', $powers) . '</td>';
} else {
echo '<td>none</td>';

Sorting nested While() Loops

I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}

IF statement within WHILE not working

I am working on a basic messaging system. This is to get all the messages and to make the row of the table that has an unread message Green. In the table, there is a column called 'msgread'. this is set to '0' by default. Therefore it should make any row with the msgread = 0 -> green. this is only working for the first row of the table with the code i have - i verified that it is always getting a 0 value, however it only works the first time through in the while statement ..
require('./connect.php');
$getmessages = "SELECT * FROM messages WHERE toperson = '" . $userid . "'";
echo $getmessages;
$messages = mysql_query($getmessages);
if(mysql_num_rows($messages) != 0) {
$table = "<table><tr><th>From</th><th>Subject</th><th>Message</th></tr>";
while($results = mysql_fetch_array($messages)) {
if(strlen($results[message]) < 30){
$message = $results[message];
}
else {
$message = substr($results[message], 0 ,30) . "...";
}
if($results[msgread] == 0){
$table .= "<tr style='background:#9CFFB6'>";
$table .= "<td>" . $results[from] . "</td><td>" . $results[subject] . "</td><td><a href='viewmessage.php?id=" . $results[message_id] ."'>" . $message . "</a></td></tr>";
}
else {
$table .= "<tr>";
$table .= "<td>" . $results[from] . "</td><td>" . $results[subject] . "</td><td><a href='viewmessage.php?id=" . $results[message_id] ."'>" . $message . "</a></td></tr>";
}
}
echo $table ."</table>";
}
else {
echo "No Messages Found";
}
There's all the code, including grabbing the info from the database. Thanks.
if(strlen($results[message]) < 30){
the message probably should be quoted:
if(strlen($results['message']) < 30){
There are quite a few other similar issues
i tested your code an the only mistake i found was the lack of quoatation marks in the indices array $results. You are using this $result[message_id] when the most appropriate would be $result['message_id']. The rest works as expected, the records with msgread equal to 0 stayed with the green line.
Your code looks a little nasty and is not easy to read.
You should use mysqli_fetch_assoc().
Always end style with a ;
use quotation on associative array
more logic choice of var names
where does $userid come from? is the content safe?
Here is quickly cleaned version of your code :
$query = "SELECT * FROM messages WHERE toperson = '" . $userid . "'";
if($results = mysqli_query($query)) {
if(mysqli_num_rows($results) != 0) {
$table = "<table><tr><th>From</th><th>Subject</th><th>Message</th></tr>";
while($data = mysqli_fetch_assoc($results)) {
if(strlen($data['message']) > 30){
$data['message'] = substr($data['message'], 0 ,30) . "...";
}
$table .= "<tr";
if($data['msgread'] == 0){
$table .= " style='background:#9CFFB6;'";
}
$table .= ">";
$table .= "<td>" . $data['from'] . "</td><td>" . $data['subject'] . "</td><td><a href='viewmessage.php?id=" . $data['message_id'] ."'>" . $data['message'] . "</a></td></tr>";
}
echo $table ."</table>";
} else {
echo "No Messages Found";
}
}

Get the total from foreach loop + mysql while loop

I am getting the $_post array and running a query on each iteration, then trying to get the total points accummulated, it seems to be overwriting the total with the last point iteration. How do I fix this?
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$total_donations = $row['points'];
}
}
}
$full_total += $total_donations;
echo $full_total;
You have to insert the $full_total in the foreach loop like this
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$full_total += $row['points'];
}
}
}
echo $full_total;

Categories