1

I am using zend framework3 and using the Mysql driver for the database. I am using the below code to get the data:

        $con = $this->adapter;
        $select = $this->sql->select();
        $select->from('nav_menu');
        $selectString = $this->sql->getSqlStringForSqlObject($select);
        $results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);
        $resultSet = new ResultSet();
        $resultSet->initialize($results); 

when i var_dump this data i am getting the result as below:

   Zend\Db\ResultSet\ResultSet Object
   (
    [allowedReturnTypes:protected] => Array
    (
        [0] => arrayobject
        [1] => array
    )
    [arrayObjectPrototype:protected] => ArrayObject Object
    (
        [storage:ArrayObject:private] => Array
            (
            )
        )

[returnType:protected] => arrayobject
[buffer:protected] => 
[count:protected] => 
[dataSource:protected] => Zend\Db\ResultSet\ResultSet Object
    (
        [allowedReturnTypes:protected] => Array
            (
                [0] => arrayobject
                [1] => array
            )

        [arrayObjectPrototype:protected] => ArrayObject Object
            (
                [storage:ArrayObject:private] => Array
                    (
                    )

            )

        [returnType:protected] => arrayobject
        [buffer:protected] => -1
        [count:protected] => 
        [dataSource:protected] => Zend\Db\Adapter\Driver\Mysqli\Result Object
            (
                [resource:protected] => mysqli_result Object
                    (
                        [current_field] => 0
                        [field_count] => 8
                        [lengths] => 
                        [num_rows] => 15
                        [type] => 0
                    )

                [isBuffered:protected] => 1
                [position:protected] => 0
                [numberOfRows:protected] => -1
                [currentComplete:protected] => 
                [nextComplete:protected] => 
                [currentData:protected] => 
                [statementBindValues:protected] => Array
                    (
                        [keys] => 
                        [values] => Array
                            (
                            )

                    )

                [generatedValue:protected] => 0
            )

        [fieldCount:protected] => 8
        [position:protected] => 0
    )

[fieldCount:protected] => 
[position:protected] => 0
)

I am getting an error When i am iterating this to get the value as below:

       foreach ($resultSet as $key => $value) {
            $array[$i]['id'] = $value->id;
            $array[$i]['name'] = $value->name;
            $array[$i]['label'] = $value->label;
            $array[$i]['route'] = $value->route;
            $array[$i]['parent_id'] = $value->parent_id;
            $i++;
        }

I don't where i am wrong. While there are num_rows is 15. This is working in ZF2 fine. Any help from any one is appreciated.

I tried a lot and find that if replace the code

    $selectString = $this->sql->getSqlStringForSqlObject($select);
    $results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);

with the

    $statement = $this->sql->prepareStatementForSqlObject($select);
    $results = $statement->execute(); 

then i get the result.But my question why i am not getting the result through

    $selectString = $this->sql->getSqlStringForSqlObject($select);
    $results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);

1 Answer 1

1

getSqlStringForSqlObject is deprecated since version 2.4. However I don't know if that is the reason it doesn't work anymore.

According to the docs there are 2 ways to this. With a prepare statement, which you are using exactly the same:

$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();

With executing the query, which you are probably looking for:

$selectString = $sql->buildSqlString($select);
$results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
Sign up to request clarification or add additional context in comments.

2 Comments

do you have any idea how to call procedure. I am using this code $results = $con->query('CALL fetching_menu(?)', $spParams); $resultSet = new ResultSet; $resultSet->initialize($results); where $spParams=array('backend'); I am getting an error.
i solved this. I used $spParams=['backend']; $statement = $con->createStatement('CALL fetching_menu(?)', $spParams); $result = $statement->execute(); $resultSet = new ResultSet; $resultSet->initialize($result);

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.