0

So briefly I have a framework class that I'm using that work perfect except with the issue I'm currently having. Problem is this, everything displays on the page as expected until the form on the page is submitted and the ISSET at the top is true:

if(isset($addAccount)){
	$SP = new SP_functions();
	echo $SP->addAccount($UID,$name,$type,$code,$balance,$website);
}

Once it passes through the isset and then its the code listed below I get this error message: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given. I'm not sure what this means so I don't understand the problem, this isset function should have nothing to do with what happens below that I would think.

The code that runs basically goes through this route:

$account = new AllAccountInfo();
$result = $account->loadByFields("UID",$UID);
foreach($result as $a){ 
   //display stuff
}

The loadByFields function looks like this:

function loadByFields($fields, $values, $options="") {
		$fields = explode(',',$fields);
		$values = explode(',',$values);
		$loops = count($fields);
		
		$selectedFields = "*";
		if($options!="") {
			foreach($options as $i => $v) {
				if($i=='fields') $selectedFields = $v;
			}
		}
		$query = "SELECT ".$selectedFields." FROM ". $this->tableName ." WHERE";
		for($i=0; $i<$loops; $i++) {
			if($i==0) $query .= " " .mysqli_real_escape_string($this->link,$fields[$i]). " = '" .mysqli_real_escape_string($this->link,$values[$i]). "'";
			else $query .= " AND " .mysqli_real_escape_string($this->link,$fields[$i]). " = '" .mysqli_real_escape_string($this->link,$values[$i]). "'";
		}
		
		if($options!="") {
			foreach($options as $i => $v) {
				if($i=='group') $groupByCond .= " GROUP BY ".mysqli_real_escape_string($this->link,$v)." ";
				if($i=='order') $orderByCond = " ORDER BY ".mysqli_real_escape_string($this->link,$v)." ";
			}
			$query .= $groupByCond.$orderByCond;
		}
// $query = SELECT * FROM allAccountInfo WHERE UID = '8' every time
		return $this->loadArrayFromQuery($query);
	}

which then calls this method, loadArrayFromQuery:

function loadArrayFromQuery($query) {
		$result = $this->executeQuery($query);
		$return = array();
		while ($rows = mysqli_fetch_array($result))  //THIS IS THE LINE THAT IS FAILING
		{	
			$return[] = $this->loadFromArray($rows);
		}
		return $return;
	}

which calls this:

function executeQuery($query) {
		mysqli_select_db($this->link, $this->db_name);
		$this->counter();
		$result = mysqli_query($this->link, $query);
		return $result;
	}

Thanks in advance to the person who helps to resolve this.

2
Contributors
5
Replies
12
Views
6 Years
Discussion Span
Last Post by Xtremefaith
0

add $this->link as first parameter, change that line to following

while ($rows = mysqli_fetch_array($this->link,$result))
0

When I do that I get this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in /home2/andreawo/public_html/myibudget/sandbox/nick/mysqlFramework/MysqlTable.php on line 232

But meanwhile I've been researching more and I think I found the answer through using this updated version of executeQuery

function executeQuery($query) {
		mysqli_select_db($this->link, $this->db_name);
		$result = mysqli_query($this->link, $query);
		//mysqli_store_result(); NOT SURE HOW TO USE THIS JUST YET...
		if(!$result){
			echo $result;
			echo mysqli_error($this->link);
		}
		return $result;
	}

Resulted in this: Commands out of sync; you can't run this command now
Which google then led me to:

This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result()

This seems to be the issue at the moment, now I just need to learn how to use that function and I shall see.

0

You may try this part, I think your query is failing. do not change executeQuery now.

function loadArrayFromQuery($query) {
		$result = $this->executeQuery($query);
                if($result)
                {
		   $return = array();
   		   while ($rows = mysqli_fetch_array($result))  //THIS IS THE LINE THAT IS FAILING
		   {	
			   $return[] = $this->loadFromArray($rows);
		   }
		   return $return;
                 }
                 else
                 {
                     echo "some error occured ".mysqli_error($this->link);;
                     die('');
                  }
	}

Edited by urtrivedi: n/a

0

It again very cleanly states "Commands out of sync; you can't run this command now"
Closer I think the issue is not trying to figure out how to use mysql_free_result()

0

The ANSWER:

function executeQuery($query) {
		mysqli_select_db($this->link, $this->db_name);
		$result = mysqli_query($this->link, $query);
		$result->close();
		mysqli_next_result($this->link); //Without this you will get "Commands out of sync; you can't run this command now" error because of the following queries being executed.
		if(!$result){
			echo $result;
			//echo mysqli_error($this->link);
		}
		return $result;
	}

Thanks to this tidbit: http://php.net/manual/en/mysqli.query.php

When calling multiple stored procedures, you can run into the following error: "Commands out of sync; you can't run this command now".
This can happen even when using the close() function on the result object between calls.
To fix the problem, remember to call the next_result() function on the mysqli object after each stored procedure call. See example below:

// 1st Query
$result = $db->query("call getUsers()");
if($result){
     // Cycle through results
    while ($row = $result->fetch_object()){
        $user_arr[] = $row;
    }
    // Free result set
    $result->close();
    $db->next_result();
}

Making this the answer:

Procedural style

bool mysqli_next_result ( mysqli $link )

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.