Why isn’t my consultation working?

Asked

Viewed 129 times

0

I have a query in a table that should return the amount of records found that meet a certain requirement.

The code:

<?php

    require('conn.php');

    $prevJan= "SELECT COUNT(ID) AS PrevJan FROM participantes WHERE PREVISTO = 'Previsto' and FORMACAO = 'Revues Technique' and MES = 'jan' and AREA = 'R&D'";
    $realJan= "SELECT COUNT(ID) AS RealJan FROM participantes WHERE REALIZADO = 'Realizado' and FORMACAO = 'Revues Technique' and MES = 'jan' and AREA = 'R&D'";

    $consultapj = mysqli_query($prevJan);
    $consultarj = mysqli_query($realJan);

    var_dump($consultapj);
?>

The connection:

<?php

    $connection = mysqli_connect("localhost", "root", "", "db_formacao");

    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

If I make a query like this by phpMyAdmin, great, returns the correct value, however, if when I try to print the value in my php page, printa NULL.

Also, I want to take this value and put as a list item, see:

data: [<?php echo($consultarj);?>]

Does anyone have any idea what might be going on?

  • Have you seen if in the bank there really is any record that obeys the conditions of your SELECT ?

  • I already checked. I created this bank myself and I know it from top to bottom. There is. :/

  • You can add the percentage symbol. PREVISTO LIKE '%Previsto%'

  • Also check if PHP is actually connecting to the database, there is your file conn.php .

  • So, I tried to var_dump the connection, returned: Object(mysqli)#1 (0) { }

  • If you are in your bank manager (Workbench, phpmyadmin and etc) and give the command SELECT COUNT(*) FROM participantes WHERE PREVISTO = 'Previsto' and FORMACAO = 'Revues Techniques' and MES = 'jan' and AREA = 'R&D he brings something ?

  • So by changing to SELECT COUNT(*) FROM participantsWHERE PREVISTO =PredictedAND REALIZADO =Performed`` He gives me a count of all the predicted only.

Show 2 more comments

1 answer

2


The mysqli_query returns an object and not the result query, to get the SELECT COUNT(*) first of a nickname uses AS (alias):

$prevJan= "SELECT COUNT(*) as TOTAL FROM participantes WHERE PREVISTO = 'Previsto' and FORMACAO = 'Revues Techniques' and MES = 'jan' and AREA = 'R&D'";
$consultapj = mysqli_query($prevJan);

And then use fetch_assoc, thus:

$consultapj = mysqli_query($prevJan);

if ($consultapj = mysqli_query($connection, $prevJan)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($consultapj)) {
        echo 'Total:', $row['TOTAL'];
    }

    /* free result set */
    mysqli_free_result($consultapj);
} else {
    die(mysqli_error($connection));
}

Note that I passed the $connection in the mysqli_query

If zero returns (0) is because or the TABLE is case-sensitive or you missed something in WHERE


[Editing]

After editing the question, the explanation of two querys is simple, fetch_array or fetch_assoc should preferably stay with your queries, in order and the variable row should not be reused, because we are using to take the results inside the while/if, mixing with other variables may confuse the results:

//Variaveis para pegar os resultados
$totaPJ = 0;
$totaRJ = 0;

//qUERYS
$prevJan = "SELECT COUNT(*) AS TOTAL FROM participantes WHERE PREVISTO = 'Previsto' and FORMACAO = 'Revues Technique' and MES = 'jan' and AREA = 'R&D'";

$realJan = "SELECT COUNT(*) AS TOTAL FROM participantes WHERE REALIZADO = 'Realizado' and FORMACAO = 'Revues Technique' and MES = 'jan' and AREA = 'R&D'";

//Pega o resultado de prevJan
if ($resultado = mysqli_query($connection, $prevJan)) {

    if ($row = mysqli_fetch_assoc($resultado)) {
        $totaPJ = $row['TOTAL']; //Seta o total
    }

    mysqli_free_result($resultado);
} else {
    die(mysqli_error($connection));
}

//Pega o resultado de realJan
if ($resultado = mysqli_query($connection, $realJan)) {

    if ($row = mysqli_fetch_assoc($resultado)) {
        $totaRJ = $row['TOTAL']; //Seta o total
    }

    mysqli_free_result($resultado);
} else {
    die(mysqli_error($connection));
}

And on the chart will probably do so:

{
    name: 'Realizado',
    type: 'column',
    yAxis: 1,
    data: [<?php echo $totalRJ; ?>],
    tooltip: {
        valueSuffix: ''
    }

}, {
    name: 'Previsto',
    type: 'spline',
    data: [<?php echo $totalPJ; ?>],
    tooltip: {
        valueSuffix: ''
    }
}
  • @Marianaferreira, see if you can understand the answer edition.

  • 1

    Yes, I got it all straight. It works now, thank you! :)

Browser other questions tagged

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