Home > Back-end >  This sql command make error but is write correct [duplicate]
This sql command make error but is write correct [duplicate]

Time:09-22

I would like to make a small portfolio for my employment in the field of web development and unfortunately for two days I don't know where the error from the sql command below comes from.

SELECT SUM(nr_points) FROM tb_transactions WHERE id_shop = 'user_shop' ;

For use I use XAMPP and below I additionally attach the command lines in the project

HTML

        <div class="formular">
          <p class="titlu_formular">Wallet</p>
          <form action="includes/wallet_system.php" method="post">
            <input type="text" name="user_shop" class="input_formular" id="user_shop" placeholder="Insert Shop ID">
            <button class="submit_btn" type="submit">Find Wallet</button>
          </form>
        </div>

PHP

    <?php

include_once 'dbh.inc.php';

$user_shop = mysqli_real_escape_string($conn, $_POST['user_shop']);

$wallet = "SELECT SUM(nr_points) FROM tb_transactions WHERE id_shop = '$user_shop' ;";

mysqli_query($conn, $wallet);

if ($conn->query($wallet) === TRUE) {
    header("Location: ../wallet.php?operation=succes");
} else {
    echo "Error: " . $wallet . "<br>" . $conn->error;
};

I know I'm still in the beginning but I need help

The last "if" I put it to know if everything works properly and before I can do more.

Have nice day

CodePudding user response:

Please consult the documentation of mysqli_query:

Returns false on failure. For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return true.

Aka: The SELECT statement does not return true, hence a check === TRUE will fail (it's a resource object, not boolean)

CodePudding user response:

Kindly accept what I have to say. We've all been here, whether we want it or not, but we tend to forget :D So here goes:

  • when at the beginning such simple problems should be taken to a more direct source of help, like a chat, like say, irc or discord with specialized dedicated help for php or mysql. And yes, you'll find yourself bombarded with answers like "your approach is not ok" which seems to deviate from your "real" problem. But hang in there and knock on it like a Woodpecker.

Coming back to your "problem". Do look at https://www.php.net/manual/en/mysqli.query.php, but not only look, also play with the code, trial and error it out, run it like you never ran code before and so-forth :D

Such a simple thing for it not to work, is basically not possible.

For example, running this:

$mysqli = new mysqli("localhost", "dorian", "asd123", "test");
    
/* Select queries return a resultset */
$user_shop = 1;
$result = $mysqli->query("SELECT SUM(nr_points) FROM tb_transactions WHERE id_shop = '$user_shop' ;");
printf("Select returned %d rows.\n", $result->num_rows);

will output the number of rows, making your "problem" inexistent.

Good luck at learning! PS: do read https://www.php.net/manual/en/mysqli.prepare.php and bind your params

  • Related