Jump to content

[SQL QUERY] I don't know what I'm doing wrong with PHP 8.2.0


Chessica

Recommended Posts

Hello!!!

Well, here I am, 10 years after my last php script, trying to execute a simple SQL query in a DB... and I'm not able to know why it doesn't work. I'm very conscious that there are lots of errors, but I will be glad if you can tell me them and make a bit of explanation to understand them. Lots of years without upgrading my knowledge and I'm very lost.

As you can see, it's a single web that try to do the following:

Session is active when you can do a connection to my DB with a determinated user who has a few rights over certain tables (this works, but I can't detect when the connection fails, it gets stuck, seems that errno doesn't work as it should do).

After that, I want to retrieve every single register of a table with a certain flag (SQL query works OK directly in server, and in the future I'll do a more acurate version), and show some fields of each registry, while I want maintain hidden the id to pass it through a form button.

I cannot retrieve the data stored in the DB, NO ERROR APPEARS, it's very frustrating and I don't know why.

Can you help me, please?

Thanks.

<?php
session_start();    
    
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'myuser';
$DATABASE_PASS = 'hispassword';
$DATABASE_NAME = 'mydb';

// Try and connect using the info above.

$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);

if (mysqli_connect_errno()){
    // If there is an error with the connection, stop the script and display the error.
    //Doesn't work, why is the server unable to process it?
    echo('Failed to connect to MySQL: ' . mysqli_connect_errno());
    $con->close();
    session_destroy();
    exit();
}
else{
        session_regenerate_id();
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $DATABASE_USER;
        $_SESSION['id'] = $id;
        echo 'Bienvenido ' . $_SESSION['name'] . '!';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Listas</title>
        <link href="./CSS/basic_style.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <div class="titulo">
    <h1>Cursillos disponibles</h1>
    </div>
        <div class="content">

<form action="comprar.php" name="comprar" method="post"> 
<table name="contents" width="100%">
                <tr>
                    <th>Título</th>
                    <th>Subtítulo</th>
                    <th>Descripción</th>
                    <th>Importe (€)</th>
                    <th>Adquirir</th>
                </tr>
<?php
$resultado = $con->excute_query("SELECT * FROM cursillo where cursillo.activo='0'", MYSQLI_USE_RESULT);
foreach ($resultado as $fila) { 
    echo $fila[1];
?>
    </td>
    <td>
<?php
    echo $fila[2];
?>
    </td>
    <td>
<?php
    echo $fila[3];
?>
    </td>
    <td>
<?php
    echo $fila[4];
?>
    </td>
    <td>
        <input type="submit" name="pagar" value="<?php$fila[0]?>">
    </td>
    </tr>
<?php
}

mysql_free_result($resultado);
$con->close();

?>
            </table>
            </form>
        </div>
    </body>
</html>

 

 

Edited by Chessica
Typographic error
Link to comment
Share on other sites

It would be helpful if you explained what it does do, rather than just what it does not do. Like what kind of error messages you may be getting.

And you should be getting error messages because you're using mysqli::execute_query incorrectly. If you aren't seeing messages then fix that before you fix the code.

Link to comment
Share on other sites

Hello.

Can you explain me how I should use mysqli::execute_query in my case instead of only tell me that is wrong used, please? As I tell you in my initial post, no errors are shown, and this is why I have no clue of what's happening or what I'm doing wrong.

How I can fix it? I'm running a uniformserver (last version), so any useful help will be apreciated.

I can only see the HTML and the welcome user message displayed when I connect to DB. No data is returned.

Do you want a screen?

Thanks.

Link to comment
Share on other sites

Hi again!

First of all, I need help understanding what's wrong. I'm using a uniformserver, and some features maybe won't work, but I'm nos interested in make a installation of apache+php+mysqyl while I'm pending for the final hosting.

I have added the example code in my php webpage and still doesn't work. No errors are shown. I have put a screencapture, I hope you like colours...

Top left: Validation message when you connect ok to DB (Bienvenido cliente!).

The rest: HTML plain code.

image.thumb.png.d99bd810880352dd56fbd9f0979395b9.png

If I use the next sentece before my DB connection procedure, the webpage stops working... It get stuck ("esta pagina no funciona....).

mysqli_report(mysqli_report_error | mysqli_report_strict);

 

Otherwise, nothing from my query is returned... as I stated in my first post.

 If I use my query with (') instead of (""), it stops to work too:                   

Quote

image.png.0e252e0b4e0ca029c469f12a6742724e.png

Here, the modified php+html code. Any help about it with explanation will be good.

<?php
session_start();    
    
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'myuser';
$DATABASE_PASS = 'mypassword';
$DATABASE_NAME = 'mydb';

// Try and connect using the info above.
//mysqli_report(mysqli_report_error | mysqli_report_strict);
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);

if (mysqli_connect_errno()){
    // If there is an error with the connection, stop the script and display the error.
    //Doesn't work, why is the server unable to process it?
    echo('Failed to connect to MySQL: ' . mysqli_connect_errno());
    $con->close();
    session_destroy();
    exit();
}
else{
        session_regenerate_id();
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $DATABASE_USER;
        $_SESSION['id'] = $id;
        echo 'Bienvenido ' . $_SESSION['name'] . '!';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Listas</title>
        <link href="./CSS/basic_style.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <div class="titulo">
    <h1>Cursillos disponibles</h1>
    </div>
        <div class="content">

<form action="comprar.php" name="comprar" method="post"> 
<table name="contents" width="100%">
                <tr>
                    <th>Título</th>
                    <th>Subtítulo</th>
                    <th>Descripción</th>
                    <th>Importe (€)</th>
                    <th>Adquirir</th>
                </tr>
<?php
//$resultado = $con->excute_query("SELECT * FROM cursillo where cursillo.activo='0'", MYSQLI_USE_RESULT);
$query="SELECT * FROM cursillo where cursillo.activo='0'";
$resultado = $con->excute_query($query, ['DEU']);
foreach ($resultado as $fila) { 
    echo $fila[1];
?>
    </td>
    <td>
<?php
    echo $fila[2];
?>
    </td>
    <td>
<?php
    echo $fila[3];
?>
    </td>
    <td>
<?php
    echo $fila[4];
?>
    </td>
    <td>
        <input type="submit" name="pagar" value="<?php $fila[0] ?>">
    </td>
    </tr>
<?php
}

mysql_free_result($resultado);
$con->close();

?>
            </table>
            </form>
        </div>
    </body>
</html>

 

Link to comment
Share on other sites

The first thing to fix is the error page you see when there's a problem. "This page has a problem" is not useful for you to fix anything. What you need are error messages about specific problems.

Edit the PHP configuration file, put these two settings at the very bottom of the file, and restart Apache:

error_reporting = -1
display_errors = on

Then try running your PHP script and query again. I think you'll see at least one warning. Then put your mysqli_report line back, and/or try the query using ' quotes, and see what PHP says.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.