Deathstorm
Deathstorm

Reputation: 857

Passing PHP variables through functions

So I've been trying out some raw PHP coding. By that I mean writing PHP without using a framework or some other help tools.

After creating some small startup projects I stumbled upon an issue. Do keep in mind that I'm still in the learning fase so if there is a better way to write my code then please do tell me.

The issue I'm having has to do with connecting and inserting into a database. The connection works and after some research I thought I found the way of passing variables through functions. (sadly I didn't)

My code is as follow:

Database class.

<?php
/**
 * Created by: PhpStorm.
 * Project: Learning_projects
 * File name: database.php.
 * User: Niels.
 * Date: 23-6-2017.
 * Time: 11:25.
 * File Description: ...
 */

namespace PHP_learning\Database\classes;


class database extends layout
{
    /**
     * @var $servername = Servername
     * @var $username = Username
     * @var $password = Password
     * @var $db = Database
     * @var $conn = Connection DB
     */
    protected $servername;

    protected $username;

    protected $password;

    protected $db;

    protected $conn;

    protected $content;

    public function connect() {
        $servername = "localhost";
        $username = "..";
        $password = "..";
        $db = "..";

        $conn = new \mysqli($servername, $username, $password, $db);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        echo "Connected successfully";

        return $this->conn;
    }

    public function insert() {
        $conn = $this->connect();
        $content = "123";
        $sql = mysqli_query($conn,"INSERT INTO content (`content`) VALUES ('$content')");
        var_dump($sql);

        if ($sql === TRUE)
        {
            echo "<script>alert('Topic 1 ingevoerd.')</script>";
        }

        else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

Index.php

/**
* Require the classes, includes, etc all is set right
**/
/**
* Starting connect and insert 
**/
    $connection->connect();
    $connection->insert();

As you can see I'm trying to require the function connect() for the insert function. Yes I could write the connect within the insert function and yes that does work, but it's not what I want.

So if anyone could tell me what I'm doing wrong I would be very happy (:

PS: Code styling tips and sorts are always welcome (:

Thanks in advance!

Upvotes: 2

Views: 54

Answers (1)

B. Desai
B. Desai

Reputation: 16446

use $this->conn instead of connect it again :

public function connect() {
        $servername = "localhost";
        $username = "..";
        $password = "..";
        $db = "..";

        $this->conn = new \mysqli($servername, $username, $password, $db);

        if ($this->conn->connect_error) {
            die("Connection failed: " . $this->conn->connect_error);
        }
        echo "Connected successfully";

        return $this->conn;
    }

    public function insert() {        
            $content = "123";
            $sql = mysqli_query($this->conn,"INSERT INTO content (`content`) VALUES ('$content')");
            var_dump($sql);

            if ($sql === TRUE)
            {
                echo "<script>alert('Topic 1 ingevoerd.')</script>";
            }

            else {
                echo "Error: " . $sql . "<br>" . $this->conn->error;
            }
        }

Upvotes: 1

Related Questions