Query on PDO with error when doing echo

1

It turns out that I was testing some scripts considered "safe" to do SQL queries, I was interested, but I can not understand where the error is. It turns out that this script includes the fetch, and all that, so that it directly does% $consult = $connection->query("SELECT * FROM Table") and then an echo or what I would like to do with the result, it is not necessary to include the execute (), this is my query

$connection = new db("localhost", "basededatos", "root", "");
$consult = $connection->query("SELECT algo FROM cosas");
echo $consult;

What gives me the following error: Notice: Array to string conversion in C: \ xampp \ htdocs \ learn2 \ php \ sqlscript.php on line 251

The script is the following: link ,

<?php
/**
 *  DB - A simple database class 
 *
 * @author      Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal)
 * @git         https://github.com/indieteq/PHP-MySQL-PDO-Database-Class
 * @version      0.2ab
 *
 */
namespace PDOWrapper;
use \PDO;
class DB
{
    # @var, MySQL Hostname
    private $hostname = 'localhost';
    # @var, MySQL Database
    private $database;
    # @var, MySQL Username
    private $username;
    # @var, MySQL Password
    private $password;
    # @object, The PDO object
    private $pdo;
    # @object, PDO statement object
    private $sQuery;
    # @array,  The database settings
    private $settings;
    # @bool ,  Connected to the database
    private $bConnected = false;
    # @object, Object for logging exceptions    
    private $log;
    # @array, The parameters of the SQL query
    private $parameters;
        
       /**
    *   Default Constructor 
    *
    *   1. Instantiate Log class.
    *   2. Connect to database.
    *   3. Creates the parameter array.
    */
        public function __construct($hostname, $database, $username, $password)
        {           
            $this->Connect($hostname, $database, $username, $password);
            $this->parameters = array();
        }
    
       /**
    *   This method makes connection to the database.
    *   
    *   1. Reads the database settings from a ini file. 
    *   2. Puts  the ini content into the settings array.
    *   3. Tries to connect to the database.
    *   4. If connection failed, exception is displayed and a log file gets created.
    */
        private function Connect($hostname, $database, $username, $password)
        {
            global $settings;
            $dsn = 'mysql:dbname='.$database.';host='.$hostname;
            try 
            {
                # Read settings from INI file, set UTF8
                $this->pdo = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
                
                # We can now log any exceptions on Fatal error. 
                $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                
                # Disable emulation of prepared statements, use REAL prepared statements instead.
                $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
                
                # Connection succeeded, set the boolean to true.
                $this->bConnected = true;
            }
            catch (PDOException $e) 
            {
                # Write into log
                echo $this->ExceptionLog($e->getMessage());
                die();
            }
        }
    /*
     *   You can use this little method if you want to close the PDO connection
     *
     */
        public function CloseConnection()
        {
            # Set the PDO object to null to close the connection
            # http://www.php.net/manual/en/pdo.connections.php
            $this->pdo = null;
        }
        
       /**
    *   Every method which needs to execute a SQL query uses this method.
    *   
    *   1. If not connected, connect to the database.
    *   2. Prepare Query.
    *   3. Parameterize Query.
    *   4. Execute Query.   
    *   5. On exception : Write Exception into the log + SQL query.
    *   6. Reset the Parameters.
    */  
        private function Init($query,$parameters = "")
        {
        # Connect to database
        if(!$this->bConnected) { $this->Connect(); }
        try {
                # Prepare query
                $this->sQuery = $this->pdo->prepare($query);
                
                # Add parameters to the parameter array 
                $this->bindMore($parameters);
                # Bind parameters
                if(!empty($this->parameters)) {
                    foreach($this->parameters as $param)
                    {
                        $parameters = explode("\x7F",$param);
                        $this->sQuery->bindParam($parameters[0],$parameters[1]);
                    }       
                }
                # Execute SQL 
                $this->success = $this->sQuery->execute();      
            }
            catch(PDOException $e)
            {
                    # Write into log and display Exception
                    $this->ExceptionLog($e->getMessage(), $query );
            }
            # Reset the parameters
            $this->parameters = array();
        }
        
       /**
    *   @void 
    *
    *   Add the parameter to the parameter array
    *   @param string $para  
    *   @param string $value 
    */  
        public function bind($para, $value)
        {   
            $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . utf8_encode($value);
        }
       /**
    *   @void
    *   
    *   Add more parameters to the parameter array
    *   @param array $parray
    */  
        public function bindMore($parray)
        {
            if(empty($this->parameters) && is_array($parray)) {
                $columns = array_keys($parray);
                foreach($columns as $i => &$column) {
                    $this->bind($column, $parray[$column]);
                }
            }
        }
       /**
    *       If the SQL query  contains a SELECT or SHOW statement it returns an array containing all of the result set row
    *   If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows
    *
    *       @param  string $query
    *   @param  array  $params
    *   @param  int    $fetchmode
    *   @return mixed
    */          
        public function query($query,$params = null, $fetchmode = PDO::FETCH_ASSOC)
        {
            $query = trim($query);
            $this->Init($query,$params);
            $rawStatement = explode(" ", $query);
            
            # Which SQL statement is used 
            $statement = strtolower($rawStatement[0]);
            
            if ($statement === 'select' || $statement === 'show') {
                return $this->sQuery->fetchAll($fetchmode);
            }
            elseif ( $statement === 'insert' ||  $statement === 'update' || $statement === 'delete' ) {
                return $this->sQuery->rowCount();   
            }   
            else {
                return NULL;
            }
        }
        
      /**
       *  Returns the last inserted id.
       *  @return string
       */   
        public function lastInsertId() {
            return $this->pdo->lastInsertId();
        }   
        
       /**
    *   Returns an array which represents a column from the result set 
    *
    *   @param  string $query
    *   @param  array  $params
    *   @return array
    */  
        public function column($query,$params = null)
        {
            $this->Init($query,$params);
            $Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);     
            
            $column = null;
            foreach($Columns as $cells) {
                $column[] = $cells[0];
            }
            return $column;
            
        }   
       /**
    *   Returns an array which represents a row from the result set 
    *
    *   @param  string $query
    *   @param  array  $params
    *       @param  int    $fetchmode
    *   @return array
    */  
        public function row($query,$params = null,$fetchmode = PDO::FETCH_ASSOC)
        {               
            $this->Init($query,$params);
            return $this->sQuery->fetch($fetchmode);            
        }
       /**
    *   Returns the value of one single field/column
    *
    *   @param  string $query
    *   @param  array  $params
    *   @return string
    */  
        public function single($query,$params = null)
        {
            $this->Init($query,$params);
            return $this->sQuery->fetchColumn();
        }
       /**  
    * Writes the log and returns the exception
    *
    * @param  string $message
    * @param  string $sql
    * @return string
    */
}

$connection = new db("localhost", "basededatos", "root", "");
$consult = $connection->query("SELECT algo FROM cosas");
    
asked by Esteban Fernández 29.05.2018 в 01:53
source

1 answer

3

The message:

  

Notice: Array to string conversion in ...

occurs when you try to read an array as if it were a string.

In the code you show, the call to the query method returns an associative array, where the key of each element is the name of the column used in the SELECT and the value is the data stored in that column.

To present the data that is in the array you can open a for loop in the following way:

foreach ($consult as $row){
    echo $row["algo"].PHP_EOL;
}

If your query was like this:

SELECT algo, alguito FROM cosas;

You would read each value in $row like this:

foreach ($consult as $row){
    echo $row["algo"]." - ".$row["alguito"].PHP_EOL;
}

To see what's in the array you can do:

print_r($consult);

This is usually done for debugging purposes, rather than to present the data in a useful way to the user.

    
answered by 29.05.2018 / 02:13
source