www.webdeveloper.com
Results 1 to 2 of 2

Thread: Commands out of sync; you can't run this command now

  1. #1
    Join Date
    Apr 2012
    Posts
    19

    Commands out of sync; you can't run this command now

    Hello All,

    I have the following code that gets log entries and compiles them into an array for viewing later:

    Code:
    function get_risk_logs($risk_no, $group_id) {
            // get dates first
            $stmt = mysqli_prepare($this->db_conn, "select date from risk_logs where risk_no = ? and group_id = ? order by date desc");
            if (!$stmt) {
                echo("Error preparing the 'get_risk_logs: 1' statement: " . mysqli_error($this->db_conn));
                exit();
            }
            mysqli_stmt_bind_param($stmt, "ii", $risk_no, $group_id);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $date);
            while (mysqli_stmt_fetch($stmt)) {
                $dates[] = $date;
            }
            mysqli_stmt_close($stmt);
            
            $dates = array_unique($dates);
            $dates = array_values($dates);
    
            /*
             * for each of the dates in the array above,
             * put all log information in an array corresponding to that date
             */
            $logs = array();
            for ($i = 0; $i < sizeof($dates); $i++) {
                $tmp_date = strval($dates[$i]);
                $logs[$i] = array('date'=>$tmp_date, 'info'=>array());
                $stmt = mysqli_prepare($this->db_conn, "select risk_no, date, time, user, details from risk_logs where date = ? and risk_no = ? and group_id = ?");
                if (!$stmt) {
                    echo("Error preparing the 'get_risk_logs: 2' statement: " . mysqli_error($this->db_conn));
                    exit();
                }
                mysqli_stmt_bind_param($stmt, "sii", $tmp_date, $risk_no, $group_id);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_bind_result($stmt, $risk_no, $date, $time, $user_number, $details);
                while (mysqli_stmt_fetch($stmt)) {
                    $count = 0;
                    $user_name = $this->get_user_name($user_number);
                    $string = "<b>User:</b>&nbsp;$user_name<p/><b>Time:</b>&nbsp;$time<p/><b>Details:</b>&nbsp;$details<p/>";
                    $logs[$i]['info'][$count] = $string;
                    $count++;
                }
                mysqli_stmt_close($stmt);
            }
            return $logs;
        }
    When the above function is called, I get an error message that says "Error preparing the 'prepare mysql' statement: Commands out of sync; you can't run this command now". I have been trying to figure out why I am getting this error message but I am coming up short. Can someone help me out? Is this a PHP problem or a MySQL problem?

    Thanks!

  2. #2
    Join Date
    Apr 2012
    Posts
    19
    Update:

    I changed the second sql statement line to

    Code:
    $stmt = mysqli_prepare($this->db_conn, "select risk_no, date, time, user, details from risk_logs where date = '?' and risk_no = ? and group_id = ?");
    Notice the 's surrounding the question mark after "date = ".

    By doing this, I manage to eliminate the error message, but my array is not populating properly. Any idea why?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles