Mysql query returning empty

Asked

Viewed 66 times

0

I made the following selection

$selficha = mysqli_query($conexao,"SELECT  * FROM ficha WHERE nome='$user->username'");

$ficha = mysqli_fetch_row($selficha);

When I want to get a table result ficha nothing appears. I give print $ficha["deus"] and the result is blank.

Table structure ficha

CREATE TABLE `ficha` (
  `nome` varchar(25) NOT NULL DEFAULT '',
  `força1` smallint(7) NOT NULL DEFAULT '0',
  `força2` smallint(7) NOT NULL DEFAULT '0',
  `const` smallint(7) NOT NULL DEFAULT '0',
  `agil` smallint(7) NOT NULL DEFAULT '0',
  `Crenca` smallint(7) NOT NULL DEFAULT '0',
  `Dedi` smallint(7) NOT NULL DEFAULT '0',
  `hp` smallint(20) NOT NULL DEFAULT '0',
  `hpa` smallint(20) NOT NULL DEFAULT '0',
  `cosmo` smallint(20) NOT NULL DEFAULT '0',
  `cosmoa` smallint(20) NOT NULL DEFAULT '0',
  `golpe1` varchar(25) NOT NULL DEFAULT '',
  `golpe2` varchar(25) DEFAULT NULL,
  `golpe3` varchar(25) DEFAULT NULL,
  `golpe4` varchar(25) DEFAULT NULL,
  `regiao` varchar(20) NOT NULL DEFAULT '',
  `grupo` varchar(20) NOT NULL DEFAULT '',
  `convite` varchar(20) NOT NULL DEFAULT '',
  `deus` varchar(10) NOT NULL DEFAULT '',
  `cea` int(7) NOT NULL DEFAULT '0',
  `v` int(10) NOT NULL DEFAULT '0',
  `d` int(10) NOT NULL DEFAULT '0'
)
  • 1

    The return of mysqli_fetch_row is a array of numerical indices, not a array associative.

1 answer

3

I recommend that you at all times read the documentation of the functions before using them. Otherwise you will program on trial and error basis, getting frustrated and wasting time.

mysqli_fetch_row

mysqli_result::fetch_row -- mysqli_fetch_row - Get a row of the result as a numbered matrix

That is, the return will be a array of numerical indices, not a array associative. Thus, for you to access the column value "deus", will need to access $ficha[18], as the column is 19a in the table.

You can use the mysqli_fetch_assoc

mysqli_fetch_assoc

mysqli_result::fetch_assoc -- mysqli_fetch_assoc - Get a row of the result set as an associative matrix

And now you can do it $ficha['deus'], for the return will be a array associative.

You can use the mysqli_fetch_assoc

mysqli_fetch_object

mysqli_fetch_object -- result->fetch_object - Returns the current row of the result set as an object

So you can do $ficha->deus, because the return will be an object, instance of stdClass.

You can use the mysqli_fetch_array

mysqli_fetch_array

mysqli_fetch_array -- result->fetch_array - Get a row of the result as an associative, numeric, or both

So you can choose if you want one array of numerical indexes, a array associative or both:

  • mysqli_fetch_array($selficha, MYSQLI_NUM), even though mysqli_fetch_row;
  • mysqli_fetch_array($selficha, MYSQLI_ASSOC), even though mysqli_fetch_assoc;
  • mysqli_fetch_array($selficha, MYSQLI_BOTH), joins the two results above;
  • mysqli_fetch_array($selficha), even if defined MYSQLI_BOTH.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.