MySQL does not collect data

1

I am trying to collect data to show on my page comments but not the table ...

Can you help me?

I have attached code and database capture.

<?php
$host_name = 'dbxxxxxx.db.1and1.com';
$database = 'dbxxxxx';
$user_name = 'dboxxxx';
$password = '<xxxx>';
$connect = mysqli_connect($host_name, $user_name, $password, $database);

if (mysqli_connect_errno()) {
    die('<p>Error al conectar con servidor MySQL: '.mysqli_connect_error().'</p>');
} else {
    echo '<p>Se ha establecido la conexión al servidor MySQL con éxito.</p >';
}
?>

<HTML>
<HEAD>
<TITLE>Deja un mensaje</TITLE>
</HEAD>
<BODY>

<FORM ACTION="procesar_mensaje.php" METHOD=POST>
<B>Nombre de usuario:</B>
<INPUT TYPE=text SIZE=20 NAME="usuario">
<BR>
<B>Escribe tu mensaje:</B>
<BR>
<TEXTAREA ROWS=10 COLS=70 NAME="mensaje"></TEXTAREA>
<BR>
<INPUT TYPE=submit VALUE="Enviar mensaje">
</FORM>

<HR>

<?PHP

$host_name = 'dbxxxxx.db.1and1.com';
$database = 'dbxxxxx';
$user_name = 'dboxxxxx';
$password = '<xxxxx>';
$connect = mysqli_connect($host_name, $user_name, $password, $database);

if (mysqli_connect_errno()) {
    die('<p>Error al conectar con servidor MySQL: '.mysqli_connect_error().'</p>');
} else {
    echo '<p>Se ha establecido la conexión al servidor MySQL con éxito.</p >';
}
if ($conexion)
{
    $resultado = mysqli_query("SELECT id, usuario, fecha, mensaje FROM comentarios ORDER BY id DESC", $conexion);
    while ($fila = mysqli_fetch_row($resultado))
    {
        echo "<B>Mensaje</B> #" . $fila[0] . "; ";
        echo "<B>Escrito por:</B> " . $fila[1] . "; ";
        echo "<B>Fecha:</B> " . $fila[2] . "; ";
        echo "<BR>";
        echo $fila[3];
        echo "<HR>";
    }
}


?>

</BODY>
</HTML>

    
asked by rolmo 15.01.2018 в 08:30
source

1 answer

3

You have at least two well-marked errors in your code.

  • Your variable or connection link is $connect and not $conexion , you must modify a single name. (connection for my answer)
  • mysqli_query wait as the first parameter the connection and then the query to execute, its error? is sending you the parameters wrongly.

Possible final code

//variable conexion
$conexion = mysqli_connect($host_name, $user_name, $password, $database);

if (mysqli_connect_errno()) {
    die('<p>Error al conectar con servidor MySQL: '.mysqli_connect_error().'</p>');
} else {
    echo '<p>Se ha establecido la conexión al servidor MySQL con éxito.</p >';
}
if ($conexion)
{
    $resultado = mysqli_query($conexion,"SELECT id, usuario, fecha, mensaje FROM comentarios ORDER BY id DESC");
    while ($fila = mysqli_fetch_row($resultado))
    {
        echo "<B>Mensaje</B> #" . $fila[0] . "; ";
        echo "<B>Escrito por:</B> " . $fila[1] . "; ";
        echo "<B>Fecha:</B> " . $fila[2] . "; ";
        echo "<BR>";
        echo $fila[3];
        echo "<HR>";
    }
}
    
answered by 15.01.2018 / 08:46
source