Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3

Thread: php error

  1. #1
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times

    php error

    ok so ive created (with help) a class in php for connecting to and selecting info from a database. heres the code

    PHP Code:
    <?php
      
    /* this script is for creating a class to connect to the database */

      
    include "includes/config.php";

      class 
    database {
        protected static 
    $connection// variable to hold the mysqli connection


      
    protected function connect(){
          if (!isset(
    self::$connection)){ // if the connection variable is not set
              
    self::$connection = new mysqli(SERVER_NAMEUSERNAMEPASSWORDDB_NAME); // set the connection
          
    }
          if (
    self::$connection === false){ //in case connection failed return false
            
    return false;
          }
          else {
            return 
    self::$connection// returns the connection
          
    }
        }
        


        protected function 
    query($query){ // public function to take a sql query
          
    $connection $this->connect(); // calls the connect function to create a connection to the database
          
    $result $connection->query($query); // puts the query into the result variable
          
    return $result//returns the result
        
    }
        


        public function 
    select($query){
          
    $rows = new array();
          
    $result $this->query($query);
          if(
    $result === false){
            return 
    false;
          }
          while(
    $row $result->fetch_assoc()){
                
    $rows[] = $row
                }
            return 
    $rows
        }


        
        public function 
    error(){
          
    $connection this->connect();
          return 
    $connection->error;
        }
      }
    ?>
    i have included this in index.php. this page just has 3 includes, one including header, one including the php i posted up there and then a footer.

    when i run in the browser it displays the header then gives an error which is

    Parse error: syntax error, unexpected 'array' (T_ARRAY) in C:\wamp64\www\final\includes\connect.php on line 31

    i checked the file and line 31 is empty and right before the select function. also the footer doesnt show

    can anyone spot the error?

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    I don't think you can return an array from a query without processing it first.

    Code:
        protected function query($query){ // public function to take a sql query
          $connection = $this->connect(); // calls the connect function to create a connection to the database
          $result = $connection->query($query); // puts the query into the result variable
          return $result; //returns the result
        }
    So you are doing: return $result;
    But $result hasn't been processed yet.

    I would expect it to look more like this:
    Code:
            protected function query($query){ // public function to take a sql query
            $connection = $this->connect(); // calls the connect function to create a connection to the database
            $result = $connection->query($query); // puts the query into the result variable
                 while($row = $result->fetch_assoc()){
                $rows[] = $row; 
                }
            return $rows; 
            }


  4. #3
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    thanks mlsiem, i foubd the error with help from stack overflow, it was the defining if the array

    $rows = new array()

    where it should have been

    $rows array()

    apparently, and also in the error function i was missing a $ symbol.

    im not sure about what you posted though but i have seen this script in action and it does work when implimented correctly.

    now i dont understand how to access the results but ill post another topic on that


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:19 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com