where is the error in my query

0

in my general database I have 5 database

  • information schema
  • mysql
  • performance schema
  • phpadmin
  • test
  • wordpress

the base I use is test, inside I have my table commenti that contains 3 fields which are

  • nome varchar (15)
  • varchar commentary (255)
  • date date

on my main page I have a text box and a comment box, this serves as a comment system, if someone wants to leave a comment it is enough to put the name and the comment, when this event occurs the data travels through the get method and then the data is stored in my commenti table up to this point there is no problem with my next code

<form method="get" action="conexion.php">
<input type="text" name="name">
<textarea rows="5" cols="5" name="commento"></textarea>
<input type="submit" value="enviar">
</form>
<?php
$conexion = mysql_connect('localhost', 'root', 'admin');
mysql_select_db('test', $conexion) or die (mysql_error());

$name = $_GET['name'];
$commento = $_GET['commento'];
$fecha= date("Y-m-d H:i:s");

$sql = "INSERT INTO commenti (nome, commento, fecha) VALUES ('$name', '$commento', '$fecha')" ;
mysql_query($sql);

echo $name;
echo $commento;

With this I get the name, the comment and the date printed on the page

However, what I want is to be able to visualize all the comments on my main page, for this purpose I have added a while loop that is recovering the data from my commenti table, however there is an error that I can not understand

<form method="get" action="conexion.php">
<input type="text" name="name">
<textarea rows="5" cols="5" name="commento"></textarea>
<input type="submit" value="enviar">
</form>

<?php
$conexion = mysql_connect('localhost', 'root', 'admin');
mysql_select_db('test', $conexion);

$query = "SELECT * FROM commenti";
while($fila = mysql_fetch_array($query)){
echo $fila['nome'];
echo $fila['commento'];
echo $fila['fecha'];
}
?>

Warning: mysql_fetch_array () expects parameter 1 to be resource, string given in F: \ xamp \ htdocs \ local \ basic \ demo.php on line 12

    
asked by steven 09.05.2017 в 01:56
source

2 answers

0

The error observed in your code PHP and for which it shows the error is that the function mysql_fetch_array expect a resource (result) from mysql_query and you send him a string simply.

Your code would be something like that.

<?php
  $conexion = mysql_connect('localhost', 'root', 'admin');
  mysql_select_db('test', $conexion);

  $query = "SELECT * FROM commenti";
  $resultado = mysql_query($query);/* Línea que falta*/
   while($fila = mysql_fetch_array($resultado, MYSQL_ASSOC)){
     echo $fila['nome'];
     echo $fila['commento'];
     echo $fila['fecha'];
   }
?>

As a recommendation, stop using mysql use Mysqli or PDO to somehow avoid SQL Injection

    
answered by 09.05.2017 / 02:26
source
0

I miss you to execute the query, replace

$query = "SELECT * FROM commenti";

for

$query = mysql_query("SELECT * FROM commenti");
    
answered by 09.05.2017 в 21:49