Empty array in PHP with MySql

php html mysql

29 просмотра

2 ответа

0 Репутация автора

I am trying to code it so users can type in their -- or others -- usernames which will be converted in their UUID then returns that user's stats from my MySql databse.

The code I attempted this with is

$username = $_POST['searchbox'];
$json = file_get_contents("https://api.mojang.com/users/profiles/minecraft/".$username);
$obj = json_decode($json);
$id = $obj->id;
$rank = Database::query("SELECT * FROM playerdata WHERE uuid=:uuid", array(':uuid'=>"".$id))[0]['rank'];
echo 'Showing results for '.$_POST['searchbox'].' '.$id.' Rank: '.$rank;

Except when I run this code it outputs:

"Showing results for kingbluesapphire 0d8d246d11c54cbbb197c6bc8ba01ee2 Rank:"

I know it's not a problem with the connection to the database because other queries are working

My goal right now is to get the field in the MySql Database thats called rank and I would like to display their rank.

Автор: user7305158 Источник Размещён: 08.04.2017 10:48

Ответы (2)

0 плюса

8099 Репутация автора

It's more like:

  $sb = $_POST['searchbox']);
  $json = json_decode(file_get_contents("https://api.mojang.com/users/profiles/minecraft/$sb"));
  if($queryRes = $connection->query("SELECT * FROM playerdata WHERE uuid={$json->id}")){
      $o = $queryRes->fetch_object(); // guessing there's only one row or you would put this in a while loop
      echo "Showing results for $sb {$o->uuid} Rank:{$o->rank}";

I'm assuming uuid is a number. Do tell.

Автор: StackSlave Размещён: 08.04.2017 11:13

0 плюса

18060 Репутация автора


Given the discussion in the comments, either you have to find out what is removing the dashes which I highly recomend to or change your query to:

$rank = Database::query("SELECT * 
                           FROM playerdata 
                          WHERE replace(uuid, '-','')=:uuid",

Databases need to be given the exact value you are looking for, any different character in a equals operation will not give you any data.

Автор: Jorge Campos Размещён: 08.04.2017 11:13
Вопросы из категории :