Unidentified variable after being declared - php

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());
}
}

Related

php switching to server connection if localhost failed

I'm trying to write a script that connects to an online server if localhost connection failed or inaccessible. I have the script below that connects to a localhost database and if it's not accessible then reroute to a server connection. The script somehow fails to work and I just don't know why. Anyone has any suggestions?
<?php
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'admin';
$DB_CON_A = new PDO("mysql:host={$DB_HOST}", $DB_USER, $DB_PASS);
if(!$DB_CON_A) {
die($DB_CON_A);
$DB_HOST = 'www.xyz.com';
$DB_USER = 'admin';
$DB_PASS = '1235kasK';
$DB_NAME = 'admin';
}
try {
$DB_CON_A = new PDO("mysql:host={$DB_HOST}; dbname={$DB_NAME}", $DB_USER, $DB_PASS);
$DB_CON_A->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
Here is my solution for those who's looking for one:
<?php
function ping($DB_HOST) {
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($DB_HOST)), $res, $rval);
return $rval === 0;
}
$DB_HOST = 'www.xyz.com';
$connected = ping($DB_HOST);
if ($connected) {
$DB_HOST = 'www.xyz.com';
$DB_USER = 'user';
$DB_PASS = 'pass';
$DB_NAME = 'accounts';
} else {
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'accounts';
}
// Create database if not exists
$DB_CON_C = new PDO("mysql:host={$DB_HOST}", $DB_USER, $DB_PASS);
$DB_CON_C->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$DB_CON_C->query("CREATE DATABASE IF NOT EXISTS $DB_NAME");
$DB_CON_C->query("USE $DB_NAME");
try {
$DB_CON_C = new PDO("mysql:host={$DB_HOST}; dbname={$DB_NAME}", $DB_USER, $DB_PASS);
$DB_CON_C->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
?>

Object couldn't be converted to string

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;
}

Migration MYSQL to MYSQLi: DB function in output-buffer-handler not working

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

Multiple database connection using php userdefined function

This is the code for config.php I am including this file in all php files:
<?php
$db_name = 'localhost';
$db_user = 'XXXXXX';
$db_pass = 'XXXXXXXXXX';
function db_connect1()
{
global $db_name, $db_user, $db_pass;
$conn = mysql_connect($db_name, $db_user, $db_pass);
mysql_select_db('test1', $conn) or die('Could not select database.');
return $conn;
}
db_connect1();
?>
Here i need to connect with another one database test2.
You can make multiple calls to mysql_connect(), but if the parameters are the same you need to pass true for another connection
<?php
$db_name = 'localhost';
$db_user = 'XXXXXX';
$db_pass = 'XXXXXXXXXX';
function db_connect1()
{
global $db_name, $db_user, $db_pass;
$conn1 = mysql_connect($db_name, $db_user, $db_pass);
$conn2 = mysql_connect($db_name, $db_user, $db_pass,true);
mysql_select_db('test1', $conn1) or die('Could not select database test1.');
mysql_select_db('test2', $conn2) or die('Could not select database test2.');
$conn = new stdClass();
$conn->conn1 = $conn1;
$conn->conn2 = $conn2;
return $conn;
}
$conn = db_connect1();
Then to query database test1, do this:
mysql_query('select * from tablename', $conn->conn1);
and for database test2:
mysql_query('select * from tablename', $conn->conn2);
?>
try this
$db_name = 'localhost';
$db_user = 'XXXXXX';
$db_pass = 'XXXXXXXXXX';
function db_connect1($dbname) {
global $db_name, $db_user, $db_pass;
$conn = mysql_connect($db_name, $db_user, $db_pass);
if($conn) {
mysql_select_db($dbname, $conn) or die('Could not select database.');
return $conn;
} else {
die("Error occurred while connect to the server.");
}
}
Every time you call the function and set argument.
echo db_connect1('test1');
echo db_connect1('test2');
Echo the function because you are using return keyword and checked that if it returns 1 its means your server connection is ok.

ADODB mySQLi Connection

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');

Categories