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 2 of 2

Thread: how to access

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

    how to access

    im just wondering how can i access values from the select function in this class.


    <?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_NAME, USERNAME, PASSWORD, DB_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 setup a test table and i ran a script to echo query successful if the sizeof the result was greater or equal to one and it ran correctly but im not sure how to access the actual data now.

    i would go back to the original lecture but it would take me hours to find the right bit. hopefully someone here can help

    just to add, im not fully sure how it works, if the table has 3 columns, username pw and email but it only has one row is it returned as a 1d associative array? but if it has more than one row does it change the result to a 2d array?

  2.  

  3. #2
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    heres what ive tried


    <?php

    include 'view/header.php';
    include 'includes/connect.php';

    $db = new database();

    $sql = "SELECT `first_name`, `last_name` FROM `pratts_db`
    WHERE `first_name` = `clive`;";

    $result = $db->select($sql);

    if (sizeof($result) >= 1){
    echo "query successful";
    echo "<p>{$result[`first_name`]}</p>";
    }

    include 'view/footer.php';

    ?>
    i have tried using single quotes, double quotes and the backtick thing when writing out the first name bit but nothing works


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 03:57 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com