How to fix the HTTP 500 error? [closed]

-1

I'm trying to run the code below but I'm getting the message HTTP ERROR 500 , how can I fix it?

<?php

//chama o arquivo de conexão com o banco
include("connect_db.php");

//consulta mysqli
$query = mysql_query("SELECT numero, orig, dest, eqpt, rota FROM voos ORDER BY RAND() LIMIT 5") or die(mysql_error());

//aqui ele gera looping e cria um array com os campos da consulta
$array = mysql_fetch_array($query)
foreach($array as $indice => $item)
{
    if(isset($array[$indice + 1])){
        echo $array[$indice]['numero'];
        echo $array[$indice]['orig'];
        echo $array[$indice]['dest'];
        echo $array[$indice]['eqpt'];
        echo $array[$indice]['rota'];
        echo "<br>";
        //Se for o item 2 primeiro inverta a parte de baixo com a de cima
        echo $array[$indice + 1]['numero'];
        echo $array[$indice + 1]['orig'];
        echo $array[$indice + 1]['dest'];
        echo $array[$indice + 1]['eqpt'];
        echo $array[$indice + 1]['rota'];
    }
?>
<br />
<?php
}
?>
    
asked by anonymous 28.09.2017 / 20:18

2 answers

4

A ; is missing at the end of:

$array = mysql_fetch_array($query)

Switch to:

$array = mysql_fetch_array($query);

I tested the rest and it looks normal.

Note:

Functions like mysql_query , mysql_connect and others that begin with the prefix mysql_ have been deprecated in PHP5.3 and removed in PHP7, ie if your server is PHP7 they do not will work, the modern APIs are mysqli and PDO, migrate as soon as possible to these new APIs, otherwise your script may fail in the future (or maybe it is already failing because of this).

I recommend that you read: How to convert a link from MYSQL to MYSQLI?

A simple example of using MySqli:

A "procedural" mysqli usage example:

<?php
//$link é a variavel da "conexão"
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* Verifica erros de conexão */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

$result = mysqli_query($link, 'SELECT * ....') or die(mysqli_error($link));


/* array associativa */
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
    ....
}

/* libera a memoria */
mysqli_free_result($result);

/* fecha a conexão */
mysqli_close($link);
    
28.09.2017 / 20:20
0

All changes from what I understand have been made. Sorry for the work this is just a hobby and a hobby site nothing professional, I'm trying to learn yet, thanks even for the help!

<?php

//chama o arquivo de conexão com o banco
//$link é a variavel da "conexão"
$link = mysqli_connect("meuhost.com.br", "usuario", "senha", "db");

//consulta mysqli
$result = mysqli_query($link, 'SELECT numero, orig, dest, eqpt, rota FROM voos ORDER BY RAND() LIMIT 5') or die(mysqli_error($link));

//aqui ele gera looping e cria um array com os campos da consulta
$array = mysqli_fetch_array($query);
foreach($array as $indice => $item)
{
    if(isset($array[$indice + 1])){
        echo $array[$indice]['numero'];
        echo $array[$indice]['orig'];
        echo $array[$indice]['dest'];
        echo $array[$indice]['eqpt'];
        echo $array[$indice]['rota'];
        echo "<br>";
        //Se for o item 2 primeiro inverta a parte de baixo com a de cima
        echo $array[$indice + 1]['numero'];
        echo $array[$indice + 1]['orig'];
        echo $array[$indice + 1]['dest'];
        echo $array[$indice + 1]['eqpt'];
        echo $array[$indice + 1]['rota'];
    }
}
?>
    
28.09.2017 / 20:49