How to name ALIAS SQL in PHP?

1

The problem lies with me when I want to call it by the name of "alias + column" from PHP.

   $sqlPersonaje = 'SELECT p.nombre,
                           p.nivel,
                           p.fuerza,
                           p.destreza,
                           p.vitalidad,
                           p.inteligencia,
                           p.agilidad,
                           p.suerte,
                           cl.nombre
                           FROM personaje p
                           INNER JOIN clase cl ON cl.ID_Clase = p.ID_Clase';

$respuestaPersonaje = mysqli_query($conn,$sqlPersonaje);
$personaje = mysqli_fetch_assoc($respuestaPersonaje);
mysqli_close($conn);

<li>Nombre: <span id="nombre"><?php echo $personaje['nombre'];?></span></li>
<li>Clase: <span id="clase"><?php echo $personaje['nombre']; ?></span></li>

If I put the character name "$ character ['p.name']" it returns an error, if I put "$ character ['name']" it returns the name of the class to which it belongs (Note that it returns the last field with the same name).

I pass the error that returns me, but I doubt it serves as something, since the rest of the values are returned to me in a perfect way, I would like to know how to be more exact when defining what I want to return to me PHP :

Notice: Undefined index: p.name in B: \ xampp \ htdocs \ Game \ app \ module \ game \ character.php on line 29

If anyone knows how to do in these cases I would appreciate it too much. I hope the question was clear.

Delete the previous post because I had made a mistake in the query, sorry!

    
asked by Alfacoy 29.11.2017 в 04:21
source

2 answers

1

To eliminate the ambiguity with the columns nombre in your query, you can assign them an alias using the clause AS in the following way:

SELECT p.nombre AS personaje_nombre,
       p.nivel,
       p.fuerza,
       p.destreza,
       p.vitalidad,
       p.inteligencia,
       p.agilidad,
       p.suerte,
       cl.nombre AS clase_nombre
       FROM personaje p
       INNER JOIN clase cl ON cl.ID_Clase = p.ID_Clase

Now you can retrieve both names without confusion using personaje_nombre and clase_nombre respectively.

    
answered by 29.11.2017 / 04:25
source
-1

USE THE AS TO DIFFERENTIATE THE NAME OF COLUMNS AND WHEN PRINTING IT IS ALWAYS IN CAPITAL BULLS BECAUSE THE COLUMN TITLES IN SQL ALWAYS ARE IN CAPITAL STATUS

$ sqlPersonality = 'SELECT p.name AS PNOMBRE,                            p.level,                            Strength                            p.destreza,                            p.vitality,                            p.intelligence,                            p.agility,                            p.suerte,                            cl.name                            FROM character p                            INNER JOIN class cl ON cl.ID_Class = p.ID_Class';

$ responseCharacter = mysqli_query ($ conn, $ sqlCharacter); $ character = mysqli_fetch_assoc ($ responsePersonaje); mysqli_close ($ conn);

  • Name:
  • Class:
  •     
    answered by 29.11.2017 в 04:33