Here is the error it gives me

Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\bootstrap\tickets_info.php on line 27
db problem.
Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\bootstrap\tickets_info.php on line 31

Fatal error: Wrong SQL: SELECT `Uplata`, `Dobivka`, `Date`, `sharedTime`, `match1`, `match2`, `match3`, `match4`, `match5`, `match6`, `match7`, `match8`, `match9`, `match10`, `match11`, `match12`, `match13`, `match14`, `match15`, `match16`,`Odds`, `tip1`, `tip2`, `tip3`, `tip4`, `tip5`, `tip6`, `tip7`, `tip8`, `tip9`, `tip10`, `tip11`, `tip12`, `tip13`, `tip14`, `tip15`, `tip16`, `isActive`, `ticket_id`, `isShared`, `Username`, `Liked`, `is_wined` FROM `kladilnica` WHERE Username='s' Error: in C:\xampp\htdocs\bootstrap\tickets_info.php on line 31

And here is the code:

$user = getUserData('users', 'UserUsername');
$ticket = getUserData('kladilnica', 'ticket_id');
$sql = "
    SELECT
        `Uplata`, `Dobivka`, `Date`, `sharedTime`, `match1`, `match2`,
        `match3`, `match4`, `match5`, `match6`, `match7`, `match8`, `match9`,
        `match10`, `match11`, `match12`, `match13`, `match14`, `match15`,
        `match16`,`Odds`, `tip1`, `tip2`, `tip3`, `tip4`, `tip5`, `tip6`,
        `tip7`, `tip8`, `tip9`, `tip10`, `tip11`, `tip12`, `tip13`, `tip14`,
        `tip15`, `tip16`, `isActive`, `ticket_id`, `isShared`, `Username`,
        `Liked`, `is_wined`
    FROM
        `kladilnica`
    WHERE
        Username='{$user}'
    ";

    $result = $conn->query($sql);

    if(!$result) {
        echo "db problem.";
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
    } else {
        if($result->num_rows == 0){
            echo "/";
        } else if($result->num_rows >= 1) {
            while($row = $result->fetch_assoc()) {
                $is_wined = $row["is_wined"];
                $uplata = $row["Uplata"];
                $dobivka = $row["Dobivka"];

                $uplata_total = $uplata_total + $uplata;

                if($is_wined == 1){
                    $dobivka_total = $dobivka_total + $dobivka;
                }

            }
            ?>

I have executed successfully in phpMyAdmin

Recommended Answers

All 12 Replies

What's the error number? Do: echo $conn->errno;

Line 27: $result = $conn->query($sql);
Line 31: trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

No, I was referring to the MySQL error code that you can display by using $conn->errno:

Replace line 21 with this:

echo "db problem: " . conn->errno;

Currently with line 22 you are only displaying the error message.

It gives me this error again

Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\bootstrap\tickets_info.php on line 27

Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\bootstrap\tickets_info.php on line 30
db problem: 
Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\bootstrap\tickets_info.php on line 31

Fatal error: Wrong SQL: SELECT `Uplata`, `Dobivka`, `Date`, `sharedTime`, `match1`, `match2`, `match3`, `match4`, `match5`, `match6`, `match7`, `match8`, `match9`, `match10`, `match11`, `match12`, `match13`, `match14`, `match15`, `match16`,`Odds`, `tip1`, `tip2`, `tip3`, `tip4`, `tip5`, `tip6`, `tip7`, `tip8`, `tip9`, `tip10`, `tip11`, `tip12`, `tip13`, `tip14`, `tip15`, `tip16`, `isActive`, `ticket_id`, `isShared`, `Username`, `Liked`, `is_wined` FROM `kladilnica` WHERE Username='s' Error: in C:\xampp\htdocs\bootstrap\tickets_info.php on line 31

By commenting line 22 or by setting the trigger on the error number, can you get the MySQL error code? The message refers to the query, but it seems fine, so the error code should give the correct information. Alternatively check the MySQL server error log.

Does this can happen because connection file is wrong ? Here is the code i have in the connect.php

<?php
    $DBServer = 'localhost';
    $DBUser   = 'root';
    $DBPass   = '';
    $DBName   = 'upstrey';

    $conn = mysqli_connect($DBServer, $DBUser, $DBPass, $DBName);

    // Check connection
    if ($conn->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    }
?>

By commenting line 22 i get the information tables of the other file thats hitory.php and im getting just this error: Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\bootstrap\tickets_info.php on line 27 db problem:

In that case you would get the error message from the die() construct. Check the error log of the database or

I have executed successfully in phpMyAdmin

try to play the query returned by the error message directly into a MySQL client and see if you get some warnings SHOW WARNINGS;

Also you could try to replicate the query into a separated script:

$mysqli = @new mysqli('localhost', $dbuser, $dbpass, $dbname);

if($mysqli->connect_errno)
    die(sprintf("CONNECT ERROR: %s: %s", $mysqli->connect_errno, $mysqli->connect_error));

$result = $mysqli->query("YOUR QUERY");

if($mysqli->errno)
    die(sprintf("ERROR: %s [%s]: %s", $mysqli->errno, $mysqli->sqlstate, $mysqli->error));

while($row = $result->fetch_row())
    print $row[0];

And see what you get.

It gives me ERROR: 1046 [3D000]: No database selected i have do it in other script.
test.php

<?php
    $DBServer = 'localhost';
    $DBUser   = 'root';
    $DBPass   = '';
    $DBName   = 'upstrey';
    $mysqli = @new mysqli($DBServer, $dbuser, $dbpass, $dbname);

    if($mysqli->connect_errno)
        die(sprintf("CONNECT ERROR: %s: %s", $mysqli->connect_errno, $mysqli->connect_error));

    $result = $mysqli->query("
    SELECT
        `Uplata`, `Dobivka`, `Date`, `sharedTime`, `match1`, `match2`,
        `match3`, `match4`, `match5`, `match6`, `match7`, `match8`, `match9`,
        `match10`, `match11`, `match12`, `match13`, `match14`, `match15`,
        `match16`,`Odds`, `tip1`, `tip2`, `tip3`, `tip4`, `tip5`, `tip6`,
        `tip7`, `tip8`, `tip9`, `tip10`, `tip11`, `tip12`, `tip13`, `tip14`,
        `tip15`, `tip16`, `isActive`, `ticket_id`, `isShared`, `Username`,
        `Liked`, `is_wined`
    FROM
        `kladilnica`
    WHERE
        Username='{s}'
    ");

    if($mysqli->errno)
        die(sprintf("ERROR: %s [%s]: %s", $mysqli->errno, $mysqli->sqlstate, $mysqli->error));

    while($row = $result->fetch_row())
        print $row[0];

?>

This happens for a typo, you're using partially uppercase variables:

$DBUser   = 'root';
$DBPass   = '';
$DBName   = 'upstrey';

While, in the connection, those variables are lowercase:

$mysqli = @new mysqli($DBServer, $dbuser, $dbpass, $dbname);

So for the mysqli class the database was not selected. Fix it and repeat.

Opps my misstake it happens... :D I get nothing with the right variable names, it loads with out errors but nothing shows on the screen

Probably because of Username='{s}', by setting an existent username you should get the row, which means the query executes fine, which implies there is something else wrong in the code. Is $user defined?

$user = getUserData('users', 'UserUsername');

I have made a huge misstake i have closed connection after getUserData i have commented the $conn->close(); and now works good :D

function getUserData($table, $field) {
        global $conn;
        $DBServer = 'localhost';
        $DBUser   = 'root';
        $DBPass   = '';
        $DBName   = 'upstrey';

        $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT `$field` FROM `$table` WHERE `ID`='".$_SESSION['user_id']."'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            if($row = $result->fetch_assoc()) {
                return $row["$field"];
            }
        }
        //$conn->close();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.