How to fix HTTP 500 error?

Asked

Viewed 216 times

-1

I’m trying to run the code below but I’m getting the message HTTP ERROR 500, how can I correct?

<?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
}
?>

2 answers

4

Missing one ; at the end of:

$array = mysql_fetch_array($query)

Change to:

$array = mysql_fetch_array($query);

I tested the rest and it looks normal.

Observing:

The functions as mysql_query, mysql_connect and others that begin with the prefix mysql_ were discontinued in PHP5.3 and removed in PHP7, meaning if your server is PHP7 they will not work, the modern Apis are mysqli and PDO, migrate as soon as possible to those new Apis, if not your script may fail in the future (or maybe I’m already failing because of this).

I recommend you read: How to convert a MYSQL connection to MYSQLI?

A simple example of using Mysqli:

A "procedural" example of using mysqli:

<?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);
  • I made and edit and thank you for the help, but it kept popping up the same message on my site > http://latamvirtual.net/core/gera_escala.php

  • @Joaquimalberico See the answer edition.

  • Guiherme thank you for your help, I made the changes that I can "understand" because I am not a programmer but the error persists.

  • @Joaquimalberico must be inside connect_db.php

  • But even if I call $link = mysqli_connect("localhost", "my_user", "my_password", "world"); from within the file the error persists, it may be some configuration from where I host the site ?

  • @Joaquimalberico you must have done something wrong when you switched to mysqli, you followed the example I posted? Understand thatmysqli_ is totally different from mysql_, the functions look like, but in fact they are not the same, so do not just exchange mysql_ for mysqli_, recommend that you redo the codes by following the example, and exchange my_user, my_password and world by your login, password and name of the database you use in your mysql database.

Show 1 more comment

0

All the changes I understood were made. Sorry the work this is just a hobby and a hobby site anything 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'];
    }
}
?>
  • Your code is totally different from what I taught you to use. Where is the mysqli_connect_errno? Repeat following exactly what I did: https://answall.com/a/241529/3635

  • I generated another code here, and the error persists, I host the site at Hostinger and I’m seeing other people with that same error :(

  • Joaquim generated another code, but did not inform how to remake it, without seeing the code I can not see where you failed, understand?

Browser other questions tagged

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