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?