AWOL TheSquatch
AWOL TheSquatch

Reputation: 63

PHP display text from MYSQL

I'm still vary new to PHP coding.

I have the code to request and show numbers from my database but I cant seem to figure out how to show text. I've Googled it with different phrases but cant seam to figure it out.

I have the following code: As you can see the second statement is in number format and works great, but the first statement I need to change to show the username rather then the numbers.

So instead of it saying " . number_format((float)$row['0']) . " it needs to say what? So it shows the username with the most tokens.


Edit: I added notation to the original code I posted so it is easier to see what I did in my final code.


/*Begin username search*/
$result = mysqli_query($con,"SELECT username FROM authorize WHERE MAX(tokens)");
/*End username search*/

/*Begin display of username*/
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td class='pslc'>" . number_format((float)$row['0']) . "</td>";
}
/*End display of username*/

/*Begin search of tokens for username*/
$result = mysqli_query($con,"SELECT MAX(tokens) FROM authorize");
/*End search of tokens for username*/

/*Begin display of tokens for username*/
while($row = mysqli_fetch_array($result)) {
  echo "<td class='psrc'>" . number_format((float)$row['0'], 5, '.', '') . "</td>";
  echo "</tr>";
}
/*End display of tokens for username*/



Edit: Here is an update of my final code so others can see what I did.

First I edited the " . number_format((float)$row['0'], 5, '.', '') . " to say " . $row['0'] . " this change alone did not fix my problem. So after much trial and error I discovered that I needed to make a code that would request the MAX(tokens). After requesting MAX(tokens) tell the next search to find a username related to the provided amount of tokens found. Then display the username and tokens on the page.

/*Begin username search*/
$result = mysqli_query($con,"SELECT MAX(tokens) FROM authorize");
while($row = mysqli_fetch_array($result)) {
$maxtokenusername=("SELECT username FROM authorize WHERE tokens = '" . $row['0'] . "'");
}
/*End username search*/

/*Begin display of username*/
$result = mysqli_query($con,$maxtokenusername);
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td class='pslc'><center>" . $row['0'] . "</center></td>";
}
/*End display of username*/

/*Begin search of tokens for username*/
$maxtokens=("SELECT MAX(tokens) FROM authorize");
/*End search of tokens for username*/

/*Begin display of tokens for username*/
$result = mysqli_query($con,$maxtokens);
while($row = mysqli_fetch_array($result)) {
  echo "<td class='psrc'>" . number_format((float)$row['0'], 5, '.', '') . "</td>";
  echo "</tr>";
}
/*End display of tokens for username*/

Upvotes: 2

Views: 1280

Answers (2)

channasmcs
channasmcs

Reputation: 1156

Please try like this: just use MySQL's FORMAT() function

select
    format(field, 2) as formatted
from
    table

if field = 100 will be formatted as 100.00.

For your query try

SELECT format(MAX(tokens), 2) as formatted 
FROM authorize

Run this query on you phpmyadmin

Use PHP number_format() if you use php

number_format($number, 2, '.', '');

if number= 100 will be formatted as 100.00

For your one try

number_format($row['0'], 2, '.', '');

Upvotes: 0

tyro
tyro

Reputation: 1428

Remove the part number_format((float)) from your code Simply put

 echo "<td class='pslc'>" .$row['0']. "</td>";

Upvotes: 2

Related Questions