1

I have been struggling for days to figure how to follow OOP in the following procedure.

Here is my connection class which handles my connection to the database.

<?php
class Connection{
    public $con = null;
    public function __construct(){
        return $this->con = new mysqli("127.0.0.1:3306", "root", "root", "tester");
    }

}
?>

And here is my Helpers Class, this class contains all the common methods that will be used in my webapp like Insert data , delete and update.

<?php 
class Helpers{
    public $con = null;
    public function __construct($connection){
        $this->con = $connection;
    }

    public function register($name){
        $con = $this->con;
        $sql = "insert into name(Name)value($name);";
        $con->query($sql);
    }
}
?>

Now I call them like this in my register.php

<?php
require "Connection.php";
require "Helpers.php";

$connection = new Connection();
$Helpers = new Helpers($connection);

$Helpers->register("Keannu");
?>

But I am getting the following error:

Call to undefined method Connection::query().

What did I do wrong?

13
  • 2
    Since you're wrapping it twice, you'll have to use $this->con->con->query() then. Avoid the extra wrapper. And investigate PDO instead of mysqli. That's going to be less effort in the end (in particular for simpler parameter binding). Commented May 20, 2015 at 3:06
  • You are doing a double wrap so its going to be con->con. You should rethink your structure. Imo Commented May 20, 2015 at 3:07
  • @mario Okay now the error is gone but it is not added on my database. Commented May 20, 2015 at 3:14
  • check for errors. the syntax error is clear in the value; it's a string. checking for errors is part of coding/debugging, which you're not doing Commented May 20, 2015 at 3:15
  • Print mysqli_errors to know what DB is returning Commented May 20, 2015 at 3:16

2 Answers 2

4

In addition to the already given answer, strings in values need to be wrapped in quotes.

I.e.: value ('$name');"; or value ('".$name."');"; which is another method.

Sidenote: value and values are accepted and are both considered as valid syntax in MySQL.

As per the manual: http://dev.mysql.com/doc/refman/5.6/en/insert.html

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name,...)] 
    [(col_name,...)]
    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...

Sidenote: Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Sign up to request clarification or add additional context in comments.

Comments

-1

Change your class as follows. Since you are passing a Connection Object you need to set its con property to the $this->con.

<?php 
class Helpers{
    public $con = null;
    public function __construct($connection){
        $this->con = $connection->con;
    }

    public function register($name){
        $con = $this->con;
        $sql = "insert into name (Name) values ($name);";
        $con->query($sql);
    }
}

9 Comments

this will still cause a syntax error during the insert value($name)
Look $Helpers->register("Keannu"); <= it's a "string" ^
Should it not be a string? what data type should i use ?
@Fred-ii- Maybe you should provide your kind help as an answer, and the OP should accept :)
@Fred-ii- yes fred i'm gonna recode it now i know the concept . the double wrap and the string literals .Thank you very much guys, saved me from days of struggle.
|

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.