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.
Related
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.
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>';
I'm trying to change the output of a variable from 0 to display user or from 1 to display superuser,
<?php
$result = mysql_query("SELECT * FROM users ") or trigger_error(mysql_error());
$tv = nl2br($row['type']);
if ($tv == '0') {
$tv = 'user';
} elseif ($tv == '3') {
$tv = 'superuser';
} else {
$tv = 'N/A';
}
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
echo "<tr>";
echo "<td>" . nl2br( $row['name'] . "</td>";
echo "<td>" . nl2br( $row['email']) . "</td>";
echo "<td>" . $tv . "</td>";
echo "</tr>";
}
?>
i know I'm validating the data before i do the mysql_fetch_array and that should do it after, but i wasn't able to put the if statement in the echo
any help pls ?
You can't access $row before it is set, you can alter your echo line for $row['type'] like this:
if ($row['type'] == 0) { echo 'user'; } else if ($row['type'] == 1) { echo 'superuser';} else {echo 'NA'; }
<?php
$result = mysql_query("SELECT * FROM users ") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
echo "<tr>";
echo "<td>" . nl2br( $row['name'] . "</td>";
echo "<td>" . nl2br( $row['email']) . "</td>";
echo "<td>"; if ($row['type'] == 0) { echo 'user'; } else if ($row['type'] == 1) { echo 'superuser';} else {echo 'NA'; } echo "</td>";
echo "</tr>";
}
?>
I'm sure you have a reason for using nl2br, but I totally don't get it.
As #Zarathuztra has mentionend, please change to mysqli_ or PDO, mysql_ is not the way to go as it is deprecated
I am trying to get this php code to run. I have made it output the table, however, I am getting this error:
Warning: Invalid argument supplied for foreach() in /path/time/processing/time/viewpunlist.php on line 54
I have been able to use the $row to get the values of it before and even reassigned it later to make sure that it wasn't only executing in WHILE. I have no clue what is going on there. Line 54 is the line:
foreach ( $row as $each)
Here is the file that I am using it in. Any help is appreciated on
a) how to make this file better and
b) getting the whole foreach statement working.
Thank you in advance!
<head>
<title>View My Punches</title>
<body bgcolor="#9966FF">
<link rel="icon" type="image/ico" href="http://example.com/time/favicon.ico"/>
</head>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASSWORD', 'pass');
define('DB_HOST', 'host');
$link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($link->connect_errno > 0){
die('Could not connect: ' .connect_error());
}
$userid_value = $_POST['userid'];
$table = "tc_".$userid_value;
$checkusersql = "SELECT * FROM tc_users WHERE userid = '$userid_value'";
$usercheck = $link->query($checkusersql);
$punchessql = "SELECT * FROM $table ORDER BY id";
$result = $link->query($punchessql);
$unixtime = time() + 60*60;
$time_value = date("h:i:s A", $unixtime);
$date_value = date("m/d/Y", $unixtime);
if ($usercheck->num_rows == 0) {
echo "Sorry, " . $userid_value . " is not a valid user ID. Please try again.";
}else {
echo "Punch Report for " . $userid_value . " | Generated at " . $time_value . " on " . $date_value;
echo "<p></p>";
if ($result->num_rows == 0) {
echo "<p></p>";
echo "No punches were found for " . $userid_value . ".";
}else{
echo "<table border=1>";
echo "<tr><th>Punch ID</th><th>Time</th><th>Punch Type</th><th>Group</th><th>Department</th><th>Notes</th></tr>";
while ($row = $result->fetch_array())
{
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['time'] . "</td><td>" . $row['punchtype'] . "</td><td>" . $row['groupname'] . "</td><td>" . $row['dept'] . "</td><td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
}
$differs = array();
$inout = array();
$inarray = array();
$outarray = array();
$current = array('in'=>$inarray,'out'=>$outarray,'length'=>'');
foreach ( $row as $each)
{
if ( $each['punchtype'] == 'in' )
{
if ( empty($current['in']) )
{ $current['in'] = $each; }
}
else if ( $each['punchtype'] == 'out' )
{
if ( empty($current['out']) )
{ $current['out'] = $each; }
}
if (( !empty($current['in']) && !empty($current['out'])))
{
$in = new DateTime($current['in']);
$out = new DateTime($current['out']);
$current['length'] = $in->diff($out);
$inout[] = $current;
$stamp = $inout['length'];
$stampformat = $stamp->format('%s');
$stampint = intval($stampformat);
$stampintval = $stampint/3600;
echo $stampintval;
}
}
?>
 
 
<form method="GET" action="http://example.com/time/panel.php">
<input type="submit" value="Go Home">
</form>
You need to check if the argument passed through foreach is an array.
This can be done by using the function is_array()
if (is_array($variable)) {
foreach ($variable as $item) {
}
}
Unless I am missing something, which I do a lot, it seems to me that you've already iterated through your SQL results here,
if ($usercheck->num_rows == 0) {
echo "Sorry, " . $userid_value . " is not a valid user ID. Please try again.";
}else {
echo "Punch Report for " . $userid_value . " | Generated at " . $time_value . " on " . $date_value;
echo "<p></p>";
if ($result->num_rows == 0) {
echo "<p></p>";
echo "No punches were found for " . $userid_value . ".";
}else{
echo "<table border=1>";
echo "<tr><th>Punch ID</th><th>Time</th><th>Punch Type</th><th>Group</th><th>Department</th><th>Notes</th></tr>";
while ($row = $result->fetch_array())
{
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['time'] . "</td><td>" . $row['punchtype'] . "</td><td>" . $row['groupname'] . "</td><td>" . $row['dept'] . "</td><td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
}
which means that the data is no longer available because you are not using a prepared statement in order to reuse it. You should be able to run another query for the foreach.
$punchessql = "SELECT * FROM $table ORDER BY id";
$result = $link->query($punchessql);
$row = $result->fetch_array();
foreach ( $row as $each) {
//your existing code.
}
I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this.
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}
?>
As you can see it is written again and again with a slight change of 2,4,6,8 in the while loop. It works but the problem is I cant rewrite it again and again as when the website will go live it will have more than 1000 entries. Could You guys help me out by suggesting another way to do this?
""** I need it to be like these dots (dots represent records in the database) **"""
. . . .
. . . .
. . . .
THANKS in Advance.
Ramzy
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
echo "</tr></table>";
}
?>
while($row = mysql_fetch_array($sql)) {
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
I don't really see what's the problem here.
By the way you should never call you're array like this $row[id] but you should quote the key instead $row['id']; Because if a constant id exists it will screw up your code and also for performance reason.
Just use
$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
++$i;
}
Also, that limit is unnecessary if all you want is to flush every record to the output. You could just do
while ($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
And it will stop as soon as there are no more records.
To print all database rows into an HTML-table, use:
echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
$i++;
}
echo '</tr></table>';
here is a general function I use:
function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
$table = array();
while ($tmp = mysql_fetch_assoc($res))
array_push($table, $tmp);
if (!count($table))
return false;
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
. ($table_id ? "id=\"$table_id\" " : "")
. ($table_class ? "class=\"$table_class\" " : "") . ">";
echo "<tr>";
foreach (array_keys($table[0]) as $field_name) {
echo "<th>$field_name";
}
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $col => $value) {
echo "<td>";
if ($value === NULL)
echo "NULL";
else
echo $value;
}
echo "\n";
}
echo "</table>\n";
return true;
}
I Got The Answer.. I wanted it to be like this. I made this and It Actually Works.
<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>
Thank You Guys For Your Support