0

I have these lines of code:

$user = $_SESSION['name'];
$con = mysqli_connect("localhost","root","","db_shop");
$sql = mysqli_query($con,"SELECT * FROM tbl_cart WHERE `user` = '$user' AND `done` = '0'");

while( $result = mysqli_fetch_assoc( $sql ) ){

    $file = $result['items'];
    $res = explode(",",$file);
    $total = 0;
    $tmp = count( $res );
    for( $i = 0 ; $i <= $tmp; $i++ ){

        $sql = "SELECT * FROM `tbl_details` WHERE `file_name` = '".$res[$i]."'";
        echo $sql;
        $sql = mysqli_query( $con, "SELECT * FROM `tbl_details` WHERE `file_name` = '".$res[$i]."'");
        while( $res = mysqli_fetch_assoc( $sql ) ){
            $total += $res['price'];
        }
        echo "<script>alert('$total');</script>";
    }
}

where $res[1] must contain a value of 1000 from my database, but it just gives me a null value.

As you can see I tried to echo out $sql, $res[0] returned the right one but $res[1] returned:

SELECT * FROM `tbl_details` WHERE `file_name` = ''

Any help would be appreciated thanks.

5
  • what is your echo $sql; outputs? Commented Oct 20, 2015 at 22:30
  • SELECT * FROM tbl_details WHERE file_name = '1.jpg'SELECT * FROM tbl_details WHERE file_name = ''SELECT * FROM tbl_details WHERE file_name = '' this is the output Commented Oct 20, 2015 at 22:31
  • i think the looping is right my only problem is why my $res[1] returns nothing Commented Oct 20, 2015 at 22:32
  • If you were to add print_r($res); immediately after $res = explode(",",$file); what would you get in the array? Does this contain all the values you would expect? Commented Oct 20, 2015 at 22:33
  • yep it returns just what i expect. Array ( [0] => 1.jpg [1] => 10.jpg ) Commented Oct 20, 2015 at 22:35

2 Answers 2

2

I wonder whether it is because the inner sql statement references the same variable name $sql? Changed the inner variable names ....

$user = $_SESSION['name'];
$con = mysqli_connect("localhost","root","","db_shop");
$sql = mysqli_query($con,"SELECT * FROM tbl_cart WHERE `user` = '$user' AND `done` = '0'");

while( $result = mysqli_fetch_assoc( $sql ) ){

    $file = $result['items'];
    $res = explode(",",$file);
    $total = 0;
    $tmp = count( $res );

    for( $i = 0 ; $i < $tmp; $i++ ){

        $sql_inner = "SELECT * FROM `tbl_details` WHERE `file_name` = '".$res[$i]."'";
        #echo $sql_inner;

        $res_inner = mysqli_query( $con, "SELECT * FROM `tbl_details` WHERE `file_name` = '".$res[$i]."'");
        while( $row = mysqli_fetch_object( $res_inner ) ){
            $total += $row->price;
        }
        echo "<script>alert('$total');</script>";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Dude it worked like a miracle . :D thanks ^-^v i almost died here . xD
since you commented echo $sql_inner; you can comment echo "<script>alert('$total');</script>"; as well ;-)
0

You should change $i <= $tmp to $i < $tmp.

Because count $tmp = count( $res ); returns number of elements but not the max index of element.

for( $i = 0 ; $i < $tmp; $i++ ){

and here you should use another variable name not $res:

    while( $row = mysqli_fetch_assoc( $sql ) ){
        $total += $row['price'];
    }

2 Comments

tried it tho, didn't worked for me. $res[1] still returns nothing.
@YesSir you repeat $res[1] so many time in comments, but I don't see any code line that outputs that $res[1] somewhere in your code provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.