This Is My Database configuration
It's Extension is Mysql. iwant to change its extension to Mysqli. Help me please.Thanks In Advance.I Want to Change This Because MySQL EXTENSION is no longer available in latest php
class CMySQL {
// variables
var $sDbName;
var $sDbUser;
var $sDbPass;
var $vLink;
// constructor
function CMySQL() {
$this->sDbName = 'YOUR_DB_NAME';
$this->sDbUser = 'DB_USER_NAME';
$this->sDbPass = 'DB_USER_PASS';
// create db link
$this->vLink = mysql_connect("localhost", $this->sDbUser, $this->sDbPass);
//select the database
mysql_select_db($this->sDbName, $this->vLink);
mysql_query("SET names UTF8");
}
// return one value result
function getOne($query, $index = 0) {
if (! $query)
return false;
$res = mysql_query($query);
$arr_res = array();
if ($res && mysql_num_rows($res))
$arr_res = mysql_fetch_array($res);
if (count($arr_res))
return $arr_res[$index];
else
return false;
}
// executing sql
function res($query, $error_checking = true) {
if(!$query)
return false;
$res = mysql_query($query, $this->vLink);
if (!$res)
$this->error('Database query error', false, $query);
return $res;
}
// return table of records as result in pairs
function getPairs($query, $sFieldKey, $sFieldValue, $arr_type = MYSQL_ASSOC) {
if (! $query)
return array();
$res = $this->res($query);
$arr_res = array();
if ($res) {
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
}
mysql_free_result($res);
}
return $arr_res;
}
// return table of records as result
function getAll($query, $arr_type = MYSQL_ASSOC) {
if (! $query)
return array();
if ($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
$arr_type = MYSQL_ASSOC;
$res = $this->res($query);
$arr_res = array();
if ($res) {
while ($row = mysql_fetch_array($res, $arr_type))
$arr_res[] = $row;
mysql_free_result($res);
}
return $arr_res;
}
// return one row result
function getRow($query, $arr_type = MYSQL_ASSOC) {
if(!$query)
return array();
if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
$arr_type = MYSQL_ASSOC;
$res = $this->res ($query);
$arr_res = array();
if($res && mysql_num_rows($res)) {
$arr_res = mysql_fetch_array($res, $arr_type);
mysql_free_result($res);
}
return $arr_res;
}
// escape
function escape($s) {
return mysql_real_escape_string($s);
}
// get last id
function lastId() {
return mysql_insert_id($this->vLink);
}
// display errors
function error($text, $isForceErrorChecking = false, $sSqlQuery = '') {
echo $text; exit;
}
}
$GLOBALS['MySQL'] = new CMySQL();
mysqli has object oriented approach to all the funtions.
for example,
$this->vLink = new mysqli("localhost", $this->sDbUser, $this->sDbPass,$this->sDbName);
But along with the object oriented approach mysqli procedural approach also exists which has just mysqli instead of mysql everywhere. But I personally recommend you to go with the object oriented approach.you can see other important functions along with the old mysql functions here.
Related
<?php
class CMySQL {
// variables
var $sDbName;
var $sDbUser;
var $sDbPass;
var $vLink;
// constructor
public function CMySQL(){
$this->engine = 'mysql';
$this->host = 'Localhost';
$this->database = 'api';
$this->user = 'root';
$this->pass = '';
$dns = $this->engine.':dbname='.$this->database.";host=".$this->host;
$this->vLink = $dns;
}
// return one value result
function getOne($query, $index = 0) {
if (! $query)
return false;
$res = mysql_query($query);
$arr_res = array();
if ($res && mysql_num_rows($res))
$arr_res = mysql_fetch_array($res);
if (count($arr_res))
return $arr_res[$index];
else
return false;
}
// executing sql
function res($query, $error_checking = true) {
if(!$query)
return false;
$res = $this->vLink;
if (!$res)
$this->error('Database query error', false, $query);
return $res;
}
// return table of records as result in pairs
function getPairs($query, $sFieldKey, $sFieldValue, $arr_type = MYSQL_ASSOC) {
if (! $query)
return array();
$res = $this->res($query);
$arr_res = array();
if ($res) {
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
}
mysql_free_result($res);
}
return $arr_res;
}
// return table of records as result
function getAll($query, $arr_type = MYSQL_ASSOC) {
if (! $query)
return array();
if ($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
$arr_type = MYSQL_ASSOC;
$res = $this->res($query);
$arr_res = array();
if ($res) {
while ($row = mysql_fetch_array($res, $arr_type))
$arr_res[] = $row;
mysql_free_result($res);
}
return $arr_res;
}
// return one row result
function getRow($query, $arr_type = MYSQL_ASSOC) {
if(!$query)
return array();
if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
$arr_type = MYSQL_ASSOC;
$res = $this->res ($query);
$arr_res = array();
if($res && mysql_num_rows($res)) {
$arr_res = mysql_fetch_array($res, $arr_type);
mysql_free_result($res);
}
return $arr_res;
}
// escape
function escape($s) {
return mysql_real_escape_string($s);
}
// get last id
function lastId() {
return mysql_insert_id($this->vLink);
}
// display errors
function error($text, $isForceErrorChecking = false, $sSqlQuery = '') {
echo $text; exit;
}
}
$GLOBALS['MySQL'] = new CMySQL();
?>
I'm learning creating APIs using PHP and wish to convert "mysql_fetch_array and mysql_free_result" to PDO and allow multiple parameters as given. The error am getting for the above code is
mysql_fetch_array() expects parameter 1 to be resource, string given
and
mysql_free_result() expects parameter 1 to be resource, string given
How can I work around this code I came across with..
You are missing the connection logic, I see that you commented this line:
//parent::CMySQL( $dns, $this->user, $this->pass );
However seems that you do not have an parent class for this one.
Check this link
I have written a piece of code for coupon. whenever I use the function to check if the Coupon Code exists, it returns true and prints the statement. But I can't get the false return and cannot get the statement printed for false case.
function db_connect() {
static $connection;
if (!isset($connection)) {
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
}
if ($connection === false) {
return mysqli_connect_error();
}
return $connection;
}
function db_query($query) {
$connection = db_connect();
$result = mysqli_query($connection, $query);
return $result;
}
function db_error() {
$connection = db_connect();
return mysqli_error($connection);
}
function db_select($query) {
$rows = array();
$result = db_query($query);
if ($result === false) {
return false;
}
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
function db_rows($query) {
$result = db_query($query);
if ($result === false) {
return false;
}
$total_rows = mysqli_num_rows($result);
return $total_rows;
}
function couponExists($cc) {
$results = db_rows("SELECT * from ms_coupons where coupon_code='".$cc."'") or die(db_error());
if ($results > 0) {
return true;
} else {
return false;
}
}
DB table : ms_coupons
coupon_id coupon_code
1 CODE50
check_coupon.php
$coupon = $_REQUEST['coupon_code'];
if (!couponExists($coupon)) {
echo "Does not exist!";
} else {
echo "Coupon exists!";
}
when i supply coupon_code as CODE50 it prints, Coupon exists! but when i supply something different like CODE51 then the pays prints nothing.
Your problem is with this line in your code:
$results = db_rows("SELECT * from ms_coupons where coupon_code='".$cc."'") or die(db_error());
db_rows function returns number of rows, in your case it can be only two values - 1 or 0. If db_rows return 0 your script executes the die(db_error()) part (but there is no mysqli_error).
Remove the or die(db_error()) part, like this:
$results = db_rows("SELECT * from ms_coupons where coupon_code='".$cc."'");
If you want to check for mysqli_errors, move it to your db_query function.
My function is always returning false when it should return true, and I can't find why
public function isReselling($key)
{
if ($this->validateKey($key)) {
return false;
}
$apis = mysql_connect("mysql.hostinger.fr", "u770656121_uapi", "testpass") or die(mysql_error());
mysql_select_db("u770656121_api", $apis);
$sql = "
SELECT * FROM api_id
";
$result = mysql_query($sql, $apis);
while($row = mysql_fetch_array($result)) {
$blacklisttho = $row['Banned'];
if ($blacklisttho == 1) {
return true;
}
}
return false;
}
Well, you need to check where exactly the 'return' is beign made, and investigate based on that:
public function isReselling($key)
{
if ($this->validateKey($key)) {
die('validate fails');
return false;
}
$apis = mysql_connect("mysql.hostinger.fr", "u770656121_uapi", "testpass") or die(mysql_error());
mysql_select_db("u770656121_api", $apis);
$sql = "
SELECT * FROM api_id
";
$result = mysql_query($sql, $apis);
while($row = mysql_fetch_array($result)) {
$blacklisttho = $row['Banned'];
if ($blacklisttho == 1) {
return true;
}
}
die('no results.');
return false;
}
and btw, you don't want to have multiple 'returns' around the code, that's bad practice.
I would change your code to something like:
public function isReselling($key)
{
$retValue = false;
if ($this->validateKey($key) === false) {
$apis = mysql_connect("mysql.hostinger.fr", "u770656121_uapi", "testpass") or die(mysql_error());
mysql_select_db("u770656121_api", $apis);
$sql = "SELECT * FROM api_id";
$result = mysql_query($sql, $apis);
while($row = mysql_fetch_array($result)) {
if ($row['Banned'] == 1) {
$retValue = true;
break;
}
}
}
return $retValue;
}
My code always return "true" whatever the string is I tried a bunch of thing but they are only return false... I have no idea what I can do to fix this... my code:
public function isBlacklisted($string)
{
$apis = mysql_connect("mysql.hostinger.fr", "mysql user", "testpass") or die(mysql_error());
mysql_select_db("u770656121_api", $apis);
$sql = "SELECT * FROM blacklist";
$result = mysql_query($sql, $apis);
while($row = mysql_fetch_array($result)) {
$username = $row['username'];
if (strpos($string,$username) !== 0) {
return true;
break;
} else {
return false;
break;
}
}
}
strpos returns false is it is not found, and 0 if it is in the first position of the string. It sounds like you want to do:
if (strpos($string,$username) !== false) {
Also, you are looping over the DB results, you should only return if it is found:
while($row = mysql_fetch_array($result)) {
$username = $row['username'];
if (strpos($string,$username) !== false) {
return true;
}
}
return false;
Or, even better:
$sql = "SELECT * FROM blacklist WHERE 'username' LIKE '%$username%'";
$result = mysql_query($sql, $apis);
while($row = mysql_fetch_array($result)) {
if ($row['username']) return true;
}
making sure to escape $username and ideally switching to mysqli or pdo instead of using deprecated mysql statements.
I am using same query again and again on different pages in between to fetch result. I want to make a function for this query.
$result = mysql_query(" SELECT name FROM tablename where id= '$row[name_id]'");
$row = mysql_fetch_array($result);
echo $row ['name'];
How to make and how to call the function?
sample class stored sample.php
class sample
{
function getName(id)
{
$result = mysql_query("SELECT name FROM tablename where id='$id'");
$row = mysql_fetch_array($result);
return $row ['name'];
}
}
use page include sample.php,then create object,then call getName() function.
<?php
include "db.class.php";
include "sample.php";
$ob=new sample(); //create object for smaple class
$id=12;
$name=$ob->getName($id); //call function..
?>
This is a good idea but first of all you have to create a function to run queries (as you have to run various queries way more often than a particular one)
function dbget() {
/*
easy to use yet SAFE way of handling mysql queries.
usage: dbget($mode, $query, $param1, $param2,...);
$mode - "dimension" of result:
0 - resource
1 - scalar
2 - row
3 - array of rows
every variable in the query have to be substituted with a placeholder
while the avtual variable have to be listed in the function params
in the same order as placeholders have in the query.
use %d placeholder for the integer values and %s for anything else
*/
$args = func_get_args();
if (count($args) < 2) {
trigger_error("dbget: too few arguments");
return false;
}
$mode = array_shift($args);
$query = array_shift($args);
$query = str_replace("%s","'%s'",$query);
foreach ($args as $key => $val) {
$args[$key] = mysql_real_escape_string($val);
}
$query = vsprintf($query, $args);
if (!$query) return false;
$res = mysql_query($query);
if (!$res) {
trigger_error("dbget: ".mysql_error()." in ".$query);
return false;
}
if ($mode === 0) return $res;
if ($mode === 1) {
if ($row = mysql_fetch_row($res)) return $row[0];
else return NULL;
}
$a = array();
if ($mode === 2) {
if ($row = mysql_fetch_assoc($res)) return $row;
}
if ($mode === 3) {
while($row = mysql_fetch_assoc($res)) $a[]=$row;
}
return $a;
}
then you may create this particular function you are asking for
function get_name_by_id($id){
return dbget("SELECT name FROM tablename where id=%d",$id);
}
You should probably parse the database connection as well
$database_connection = mysql_connect('localhost', 'mysql_user', 'mysql_password');
function get_row_by_id($id, $database_link){
$result = mysql_query("SELECT name FROM tablename where id= '{$id}");
return mysql_fetch_array($result);
}
Usage
$row = get_row_by_id(5, $database_connection);
[EDIT]
Also it would probably help to wrap the function in a class.
function getName($id){
$result = mysql_query("SELECT name FROM tablename where id= '$row[name_id]'");
$row = mysql_fetch_array($result);
return $row ['name'];
}
call the function by
$id = 1; //id number
$name = getName($id);
echo $name; //display name