I am using mysql with adodb and what changes i have to do if i want to shift to mysqli.
this is my connection file and i need to make changes in this file only or i have to update all my query files.
include_once("adodblib/adodb.inc.php");
class ADb {
function ADb()
{
global $dbserver;
global $dbuser;
global $dbpass;
global $database;
$dbuser = "root";
$dbpass = "asdasdasd";
$dbserver = "localhost";
$database = "DB_NAME";
$this->conn1 = &ADONewConnection('mysql');
$this->conn1->PConnect($dbserver, $dbuser, $dbpass, $database);
}
function query($sql){
$Result = $this->conn1->Execute($sql);
return $Result;
}
function ExecuteQuery($sql){
$Result = $this->conn1->Execute($sql);
return $Result;
}
function ExecuteQuery1($sql){
return $this->query($sql);
}
function Execute($sql){
return $this->query($sql);
}
}
You just need to change the parameter of ADONewConnection to mysqli.
Change the line
$this->conn1 = &ADONewConnection('mysql');
to
$this->conn1 = &ADONewConnection('mysqli');
Related
I'm currently switching all the mysql_* functions to mysqli_* functions and i am getting the following errors .
PHP Warning: mysqli_connect(): (HY000/1040): Too many connections in
PHP Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in
PHP Warning: mysqli_error() expects parameter 1 to be mysqli, boolean
In config.php :
$dbuser = "xxx";
$dbpass = "xxx";
$dbhost = "xxxx";
$dbname = "xxxxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass) or die("could not connect to mysql");
if($connection){
echo "\n Database connected ....\n\n";
}
mysqli_select_db($connection,$dbname) or die(mysqli_error($connection));
In other.php
require_once 'Lib/config.php';
class Mutex {
protected $connection;
public function __construct()
{
global $dbuser,$dbpass,$dbname,$dbhost; // globally declaring the config variables
$this->connection = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
//you need to exit the script, if there is an error
exit();
}
}
public function Prog($pack = null) {
if(isset($pack) && $pack != "") {
$sql_qry = "SELECT id from xxxx WHERE package like '" . addslashes($pack) . "'";
showSqlQuery($sql_qry);
$result = mysqli_query($this->connection,$sql_qry)or die(mysqli_error($this->connection));
if($result && mysqli_num_rows($result)>0) {
$row = mysqli_fetch_assoc($result);
if(!empty($row)) {
return "Exists";
}
}
return "NotExists";
}
return "InvalidProcess";
}
}
Tried many solutions, but none of them worked for me
Getting errors as shown above.
Please help me to solve this..
Thanks in advance.
I think your DB configuration has some issue.
Replace the following code of config.php file to bellow.
$dbuser = "xxx";
$dbpass = "xxx";
$dbhost = "xxxx";
$dbname = "xxxxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Rather than duplicating ( nearly ) the connection you could try the approach where you pass a reference to the db connection as a parameter to the Mutex constructor
<?php
/* lib/config.php */
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'xxx';
$dbname = 'xxx';
$db = new mysqli( $dbhost, $dbuser, $dbpass, $dbname );
?>
<?php
include 'lib/config.php';
class Mutex{
private $connection;
public function __construct( $dbo=object ){
$this->connection=$dbo;
}
public function Prog($pack = null) {
$result='InvalidProcess';
if( !empty( $pack ) ) {
$sql='select `id` from `xxxx` where `package` like ?';
$stmt=$this->connection->prepare( $sql );
if( $stmt ){
$var = '%'.$pack.'%';
$stmt->bind_param('s', $var );
$result=$stmt->execute();
if( $result && $stmt->num_rows > 0 ){
$stmt->store_result();
$stmt->bind_result( $id );
while( $stmt->fetch() ){/* do stuff perhaps */
echo $id;
}
$stmt->free_result();
$stmt->close();
$result='Exists';
}
}
}
return $result;
}
$pack='banana';
$mutex=new Mutex( $db );
$mutex->Prog( $pack );
?>
I'm getting this error message when trying to make a PDO connection:
Object of class dbConnection could not be converted to string in (line)
This is my code:
class dbConnection
{
protected $db_conn;
public $db_name = "todo";
public $db_user = "root";
public $db_pass = "";
public $db_host = "localhost";
function connect()
{
try {
$this->db_conn = new PDO("mysql:host=$this->$db_host;$this->db_name", $this->db_user, $this->db_pass);
return $this->db_conn;
}
catch (PDOException $e) {
return $e->getMessage();
}
}
}
The error is on the PDO line. Just in case, I insert the code where I access to the connect() method:
class ManageUsers
{
public $link;
function __construct()
{
$db_connection = new dbConnection();
$this->link = $db_connection->connect();
return $link;
}
function registerUsers($username, $password, $ip, $time, $date)
{
$query = $this->link->prepare("INSERT INTO users (Username, Password, ip, time1, date1) VALUES (?,?,?,?,?)");
$values = array($username, $password, $ip, $time, $date);
$query->execute($values);
$counts = $query->rowCount();
return $counts;
}
}
$users = new ManageUsers();
echo $users->registerUsers('bob', 'bob', '127.0.0.1', '16:55', '01/01/2015');
Change your connection setting to the following:
class dbConnection
{
protected $db_conn;
public $db_name = "todo";
public $db_user = "root";
public $db_pass = "";
public $db_host = "localhost";
function connect()
{
try {
$this->db_conn = new PDO("mysql:host={$this->db_host};{$this->db_name}", $this->db_user, $this->db_pass); //note that $this->$db_host was wrong
return $this->db_conn;
}
catch (PDOException $e) {
//handle exception here or throw $e and let PHP handle it
}
}
}
In addition, returning values in a constructor has no side-effects (and should be prosecuted by law).
Please follow below code , its tested on my server and running fine .
class Config
{
var $host = '';
var $user = '';
var $password = '';
var $database = '';
function Config()
{
$this->host = "localhost";
$this->user = "root";
$this->password = "";
$this->database = "test";
}
}
function Database()
{
$config = new Config();
$this->host = $config->host;
$this->user = $config->user;
$this->password = $config->password;
$this->database = $config->database;
}
function open()
{
//Connect to the MySQL server
$this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->user,$this->password);
if (!$this->conn)
{
header("Location: error.html");
exit;
}
return true;
}
I am migrating a legacy project from mysql_ to mysqli_.
Somewhere in the code there is
ob_start("do_output");
And in my
function do_output($output) {
[...]
querydb(' .... ');
[...]
}
where querydb() looks like thi
function querydb($query,$dieonerrors=true,$ec=false) {
global $db_conn, $db_type, $debuginfos, $stat_qcount;
...
Now when I check this with the debugger $db_conn is null. It was initialized with $db_conn = mysqli_connect() earlier. And the connection was working during the run. But inside of the output buffer handler it became null. How can this be? With mysql_*it was working.
EDIT Here is the full code demonstrating the problem:
<?php
$db_type = "mysqli";
$db_host = "localhost";
$db_database = "test";
$db_user = "root";
$db_pass = "root";
initdb();
querydb("SHOW TABLES");
ob_start('do_output');
querydb("SHOW TABLES");
echo "foobar";
function initdb($persistent = false)
{
global $db_host, $db_user, $db_pass, $db_database, $db_type, $db_conn;
$db_conn = mysqli_connect($db_host, $db_user, $db_pass);
mysqli_query($db_conn, "USE $db_database");
return $db_conn;
}
function querydb($query, $dieonerrors = true, $ec = false)
{
global $db_conn, $db_type, $debuginfos, $stat_qcount;
$result = mysqli_query($db_conn, $query);
if (!$result) {
if ($dieonerrors) {
die('error');
}
}
return $result;
}
function do_output($output)
{
querydb('SHOW TABLES'); // if I comment this I get an output 'foo'
return "foo";
}
I get no output and the error in /var/log/apache2/error.log is
PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/foo/bar/test.php on line 32
I'm getting this error
Fatal error: Non-static method Connect::connect() cannot be called statically in D:\xampp\htdocs\Panel\core\init.php on line 63
Here is my code
<?php
class Connect{
public $db_host = "localhost";
public $db_user = "root";
public $db_pass = "";
public $db_name = "panel";
public function connect(){
if(mysql_connect($db_host, $db_user, $db_pass)){
if(mysql_select_db($db_name)){
return true;
}else{
die(mysql_error());
}
}else{
die(mysql_error());
}
return false;
}
}
?>
How do I make the function static?
I tried adding 'static' to the function scope, but I got another error
Thanks :)
You have to pass the variables to function as parameter
function connect($db_host, $db_user, $db_pass,$db_name)
And call this function as
connect($db_host, $db_user, $db_pass,$db_name);
Edit
By seeing your pastebin, you are calling class variables, you have to use $this->variale_name to access them.
<?php
class Connect{
public $db_host = "localhost";
public $db_user = "root";
public $db_pass = "";
public $db_name = "panel";
public function connect(){
if(mysql_connect($this->db_host, $this->db_user, $this->db_pass)){
if(mysql_select_db($this->db_name)){
return true;
}else{
die(mysql_error());
}
}else{
die(mysql_error());
}
return false;
}
}
?>
PDO
<?php
class Connect{
private $db_host = "localhost";
private $db_user = "root";
private $db_pass = "";
private $db_name = "panel";
private $dbh = false;
public function connect(){
if ($this->dbh === false)
$this->dbh = new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_name, $this->db_user, $this->db_pass);
return $this->dbh;
}
}
?>
You have to declare the variables inside the function like this
<?php
function connect(){
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "panel";
if(mysql_connect($db_host, $db_user, $db_pass)){
if(mysql_select_db($db_name)){
}else{
die(mysql_error());
}
}else{
die(mysql_error());
}
}
?>
Or you can pass the parameters in the function like this
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "panel";
connect($db_host,$db_user, $db_pass, $db_name);
function connect($db_host,$db_user, $db_pass, $db_name){
if(mysql_connect($db_host, $db_user, $db_pass)){
if(mysql_select_db($db_name)){
}else{
die(mysql_error());
}
}else{
die(mysql_error());
}
}
?>
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "panel";
These variables are global and can't be accessible in function connect. If you have to use these global variables then use keyword global . Then these vars will be available within the function.
function connect(){
global $db_host, $db_user, $db_pass, $db_name ;
if(mysql_connect($db_host, $db_user, $db_pass)){
if(mysql_select_db($db_name)){
}else{
die(mysql_error());
}
}else{
die(mysql_error());
}
}
Is there anything wrong with connecting and closing to a database by calling the function below with the mysql_query and mysql_fetch_array commands between the two
<?php
function dbconnect()
{
$sql = "localhost";
$username = "------";
$password = "-----";
$connection = mysql_connect($sql, $username, $password) or
die("unwable to cct");
$databse = mysql_select_db("-------", $connection);
global $connection;
}
function close()
{
global $connection;
mysql_close($connection);
}
dbconnect();
$query = "Some SQL Statement";
$data = mysql_query($query, $connection); - L1
while (mysql_fetch_assoc($data))
{
//echo something
}
close();
?>
At present, I am getting an error saying that $connection at L1 needs to be a resource but is a BOOL. If I give a die statement there, the same is triggered. I have no idea what is wrong. Please spot any errors you can. I have to take a sabbatical from coding and I am back after a while.
Thanks & regards
You must use the global keyword before assigning the $connection variable. Otherwise, you declare a local $connection inside the function and then call a reference to the yet non-existent global $connection. In the other functions, that non-existent global is used.
function dbconnect()
{
// Global first to be sure the subsequent $connection is the global
// rather than a new one local to this function
global $connection;
$sql = "localhost";
$username = "------";
$password = "-----";
// Now this modifies the global $connection
$connection = mysql_connect($sql, $username, $password) or die("unwable to cct");
$databse = mysql_select_db("-------", $connection);
}
More readable would be to use the $GLOBALS array:
function dbconnect()
{
$sql = "localhost";
$username = "------";
$password = "-----";
// Using the $GLOBALS superglobal array
$GLOBALS['connection'] = mysql_connect($sql, $username, $password) or die("unwable to cct");
$databse = mysql_select_db("-------", $GLOBALS['connection']);
}
Best of all would be to return $connection from dbconnect() and use that value in other functions:
function dbconnect()
{
$sql = "localhost";
$username = "------";
$password = "-----";
$connection = mysql_connect($sql, $username, $password) or
die("unwable to cct");
$databse = mysql_select_db("-------", $connection);
// Return from the function
return $connection;
}
// call as
$connection = dbconnect();
// and define your other functions to accept $connection as a parameter
declare global $connection before calling mysql_connect()
function dbconnect()
{
global $connection;
$sql = "localhost";
$username = "------";
$password = "-----";
$connection = mysql_connect($sql, $username, $password) or
die("unwable to cct");
$databse = mysql_select_db("-------", $connection);
}
Not too sure but try closing it by using
mysql_Close($Connection);
everything else looks good
Just put the global $connection; line in the beginning of the function instead and it should work. Using global keyword at the end of the function implies that you want to use the global variable $connection. But the thing you will be doing now is, assingning global variable $connection "the actual connection resourceID".