[quote=php]Catchable fatal error: Object of class Connection could not be converted to string[/quote]
Ok so I am *incredibly* new to all this and am just running through the PHP OOP manual, I'm trying to make a very basic class just to learn on. Starting with the basics and expanding. I know my code isn't very good, so please feel free to *gently* point me in the right direction so I can try and improve. I know it isn't very good, and am not trying to pass myself off as an expert, so don't take offense at my garbage, please!
That said....
I have 1 row in the table this is querying, but am still having issues, I know it's something really simple that I don't know/understand, and it's frustrating me that my own stupidity is getting in the way. I've spent the last few hours scratching my head on it.
The class
<?php
/*******************************
* Created: 30/04/2012
*******************************/
class Connection
{
protected $link;
private $qcondition;
private $outputQuery;
private function connect()
{
// setting username and password here as, per installation will always be using same database for this small project.
$server = "127.0.0.1";
$username = "root";
$password = "";
$database = "example";
$this->link = mysqli_connect($server, $username, $password);
mysqli_select_db($this->link, $database);
}
public function escape($string, $replace)
{
return mysqli_real_escape_string($this->link, $string);
if($replace != 0)
$string = str_replace("\"", "", $string);
$string = str_replace("\\", "", $string);
return $string;
}
public function query($query)
{
$this->connect(); // Connect always, as per PHP manual if connection exists will use it anyway.
// escaping all data.
$query = $this->escape($query, 1);
$queryresult = mysqli_query($this->link, $query) or die(mysqli_error($this->link));
while ($queryrow = mysqli_fetch_array($queryresult))
{
$this->outputQuery .= $queryrow['title'];
}
return $this->outputQuery;
}
}
?>
the example:
<?php
include('database.php');
$test = new Connection;
$test->query("SELECT * FROM blogentry WHERE id=1");
echo "$test";
?>
Sorry to be asking, what I'm sure is a really dumb question, I asked google first, but he's being moody today (nice birthday present to myself

New Topic/Question
Reply



MultiQuote




|