0

I'm trying to get values from a regular table with regular way of mysql_query and mysql_fetch_array from call in

if ($comtype==3){
        $getsteps = mysql_query("SELECT id FROM steps WHERE id = $id");

        $row = mysql_fetch_array($getsteps);
        if($row != false){
            $steps_array = array();
            while ($row = mysql_fetch_array($getsteps, MYSQL_ASSOC)) 
            {
                array_push($steps_array,$row);
            }
            $countsteps = count($steps_array);

            error_log("count: ".$countsteps);
            error_log(print_r($steps_array));

            echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $steps_array)); 
        } else {
            //error
        }
    }

in error log :

[28-Aug-2017 11:25:32 UTC] count: 1 [28-Aug-2017 11:25:32 UTC] 1

while the count I know it is 2 rows!!!

and in

$.ajax({
            type:'POST',
            url:'app/check.php',
            data: {'ID':ID},
            dataType: 'json',
            success: function(value){
                if (value.comtype== "0") {
                        $("#newC").show();
                        $("#hadC").hide();
                    } else {
                        var method;
                        var com;
                        com = "<div class='clearfix'></div><br><h3>old.</h3>";
                        com += "By Stpes: <br>";

                            for (i = 0; i < value.countsteps; i++) { 
                                com += "Step " + Number(i)+1 + ": Greater Than " + value.steps_array['calfrom'] + " AND Less Than or Equal " + value.steps_array['calto'] + " Apply "  + value.steps_array['calapl'] + " % <br>";
                            }
                        }

                        $("#hadC").html(com);
                        $("#hadC").show();
                        $("#newC").hide();
                    }
                }   
            });

question is: 1. I don't know how to push data from while into array? 2. I don't know how to loop into this array in ajax to view it in div?

1
  • first of all stop using deprecated+removed version of mysql_*. turn to mysqli_* OR PDO(along with prepared statement to prevent your query from SQL Injection). for current code do only this:- if ($comtype==3){ $getsteps = mysql_query("SELECT id FROM steps WHERE id = $id"); $row = mysql_fetch_array($getsteps); $countsteps = count($row); echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $row)); } Commented Aug 28, 2017 at 11:48

1 Answer 1

1

The problem is that you're calling mysql_fetch_array() before the while loop. This fetches the first row, and the loop fetches the remaining rows.

If you want to test whether any rows were returned before looping, call mysql_num_rows(). You can also use this instead of count($steps_array).

if ($comtype==3){
    $getsteps = mysql_query("SELECT id FROM steps WHERE id = $id");

    $countsteps = mysql_num_rows($getsteps);
    if($countsteps > 0) {
        $steps_array = array();
        while ($row = mysql_fetch_array($getsteps, MYSQL_ASSOC)) 
        {
            array_push($steps_array,$row);
        }

        error_log("count: ".$countsteps);
        error_log(print_r($steps_array));

        echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $steps_array)); 
    } else {
        //error
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.