2

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!

4
  • You never assign anything to $this->conn, you just set a local variable $con that is destroyed as soon as the connect() method finishes. Commented Jun 23, 2017 at 11:39
  • @jeroen Was thinking of that already, but is there another way, is there a way to achieve it in this way instead of setting a global variable and passing that through. That is my question Commented Jun 23, 2017 at 11:42
  • 1
    I'm not sure what you mean, are you looking for Dependency Injection? You can find plenty about that here. Commented Jun 23, 2017 at 11:44
  • @jeroen, Thanks (: It is what i'm looking for yes. Commented Jun 23, 2017 at 11:48

1 Answer 1

1

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;
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

2 errors occur: 1. ( ! ) Notice: Trying to get property of non-object in F:\wamp64\www\Learning_projects\PHP\PHP_learning\3_Database\classes\database.php on line 62 :::: 2. Warning: mysqli_query() expects parameter 1 to be mysqli, null given in F:\wamp64\www\Learning_projects\PHP\PHP_learning\3_Database\classes\database.php on line 53
Have you change connect() method?
no, it is still as stated in my question, except for the connect credentials ofcourse.
In connect also change $this->conn. See my answer

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.