I am learning to use mysqli instead of mysql and facing some problems. This is my code.
require("classes/dbo.class.php");
$db->dml('update table set name="abc" where a_id=5);
echo "Rows Affected : ".mysqli_affected_rows($db->link());
Here's dbo.class.php
class dbo
{
private $db = "dbname";
private $user = "root";
private $pass = "";
private $host = "localhost";
function link()
{
$link = mysqli_connect($this->host, $this->user, $this->pass) or die(mysqli_error($link));
return $link;
}
function dml($q)
{
$link = mysqli_connect($this->host, $this->user, $this->pass) or die(mysqli_error($link));
mysqli_select_db($link,$this->db) or die(mysqli_error($link));
mysqli_query($link, $q) or die(mysqli_error($link));
}
}
$db = new dbo();
Now the problem is I don't understand how to pass database link ($link) in mysqli_affected_rows() function. I tried above, but it seems to create a new database connection, so mysqli_affected_rows returns 0 instead of 1.
I thought of creating a new method dmlWithMysqliAffectedRow() in dbo.class.php that returns affected rows instead of true and false.
My solution looks stupid to me. Which is the better way to do this?
First you have an error in this code, you have not closed the string for your query. Its also better to use a double quoted string and single quotes around you SQL data parameters.
Second
mysqli_
has a perfectly good OO implementation, use that rather than trying to re-invent the wheel, and getting a square one.Documentation: for this can be found here, in many languages