2

I have sql query that saved on table.

tbl_query

SELECT SUM(simpanan_wajib) AS TOTAL FROM tb_simpanan WHERE badgeid_fk = '$getBadgeID'

Then on PHP code:

$query = mysqli_query($con, "SELECT * FROM tbl_query");
while($data = mysqli_fetch_array($query))
{
    //SELECT SUM(simpanan_wajib) AS TOTAL FROM tb_simpanan WHERE badgeid_fk = '$getBadgeID'
    $getQuery = $data['sql_query'];

    $qTotal = mysqli_query($con, $getQuery);
    $dTotal = mysqli_fetch_array($qTotal);

    echo $dTotal['TOTAL'];
}

When I tried to run that code, it show me result of total is 0. But if I remove this WHERE badgeid_fk = '$getBadgeID' on query data, the result is OK not 0.

How to keep execute the query even if there is an variable '$getBadgeID'

HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

2 Answers2

3

PHP treating this variable as a string, thats why result generating this query

SELECT SUM(simpanan_wajib) AS TOTAL FROM tb_simpanan WHERE badgeid_fk = '$getBadgeID' // its not converting your variable with 150502

Here you can use alternate name or you can use with delimiter like:

Your current query is:

SELECT SUM(simpanan_wajib) AS TOTAL FROM tb_simpanan WHERE badgeid_fk = '$getBadgeID'

Change your query with:

SELECT SUM(simpanan_wajib) AS TOTAL FROM tb_simpanan WHERE badgeid_fk = ':getBadgeID'

Now, you need to use str_replace to replace delimiter with your variable like:

while($data = mysqli_fetch_array($query))
{
    $getQuery = str_replace(":getBadgeID", $getBadgeID , $data['sql_query']);    
}

Why i am using delimiter here, because your variable $getBadgeID having defined value inside your php script and its not dynamic.

In our chat conversation, @executable suggest an another solution to use prepared statement.

Edit:

As per discussion with @Bananaapple, i am adding this comment for future visitors, Prepared Statement is an another solution which is more secure, if you want to avoid SQL injection, then choose prepared statement.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • This makes the code vulnerable to SQL injection attacks. Instead of `str_replace` check out this https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Bananaapple Mar 19 '19 at 09:06
  • 1
    @Bananaapple: yes, i already shared this suggestion to OP, check our long conversation, about the possible solution, OP ask as to share example with delimeter. he ask us which one is the best one, i suggest him, prepared statement , check conversation.. and i also suggest to other person to post your answer with prepared statement. conversation https://chat.stackoverflow.com/rooms/190285/discussion-between-hidayurie-dave-and-devpro – devpro Mar 19 '19 at 09:09
  • Yeah I saw that but you should mention it in your answer as well as anyone else that comes across this question with a similar problem will not be reading the chat :-) – Bananaapple Mar 19 '19 at 09:17
  • @Bananaapple: thank you for this, i have added in answer, please check – devpro Mar 19 '19 at 09:34
2

The recommend way is to use the prepared statements to sanitize the query and protect you from SQL injection. The following comic give an example of what is SQL injection.

enter image description here

For answering the question we discover that in your query the variable $getBadgeID was read as text and not as variable. I recommend you to use this code which use the prepared statements :

<?php
$conn = new mysqli("HOST", "USER", "SECRET", "DATABASE");
if($stmt = $conn->prepare("SELECT SUM(simpanan_wajib) AS TOTAL FROM tb_simpanan WHERE badgeid_fk = ?")) {
    $stmt->bind_param("s", $getBadgeID);
    $stmt->execute(); 
    $result = $stmt->get_result();
    while($row = $result->fetch_assoc()) {
        $total = $row['TOTAL'];
    }
    $stmt->close();
}
$conn->close();
var_dump($total);

If you want more debugging :

<?php
if(isset($getBadgeID) and $getBadgeID != ""){
    $conn = new mysqli("HOST", "USER", "SECRET", "DATABASE");
    if($stmt = $conn->prepare("SELECT SUM(simpanan_wajib) AS TOTAL FROM tb_simpanan WHERE badgeid_fk = ?")) {
        $stmt->bind_param("s", $getBadgeID);
        $stmt->execute(); 
        $result = $stmt->get_result();
        while($row = $result->fetch_assoc()) {
            $total = $row['TOTAL'];
        }
        $stmt->close();
    }else{
        echo "Query is wrong";
    }
    $conn->close();
    var_dump($total);
}else{
    echo 'Variable $getBadgeID is empty';
}
executable
  • 3,365
  • 6
  • 24
  • 52