$db = new DB() confusion - php

Can any please explain what does it mean
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
and it's giving me an Error
"Error: Could not load database file mysql!"
file contents
define('DB_DRIVER', 'mysql');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'XXX');
define('DB_PASSWORD', 'XXX');
define('DB_DATABASE', 'XXXX');
DB CLASS Constructor
public function __construct($driver, $hostname, $username, $password, $database) {
if (file_exists(DIR_DATABASE . $driver . '.php')) {
require_once(DIR_DATABASE . $driver . '.php');
} else {
exit('Error: Could not load database file ' . $driver . '!');
}
$this->driver = new $driver($hostname, $username, $password, $database);
}

You don't have driver for mysql database, check DIR_DATABASE folder for existence of the mysql.php file.

Assuming you're using OpenCart, have a look in your config.php file. Find a line that looks like the following:
define('DIR_DATABASE', '/something/something/system/database/');
Make sure the something/something is valid for your site. In particular, ensure the path points to a directory containing the mysql.php driver file. You'll probably find it doesn't, so you'll need to edit it so that it does.
You should also check that the Apache service user has privileges to access that file.

Related

Which database connection should be used to connect to MySQL with PHP, and why?

I have seen some ways to connect to the MySQL DB with PHP, but don't know why the one is better than the other, or safer or anything. Same thing goes with inserting or fetching data to/from the database.
This is some examples of MYSQL connection with PHP:
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
$db = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to database. '.mysqli_connect_error());
OR
class kobling extends mysqli {
public function __construct(
$host = "127.0.0.1",
$base = "test",
$user = "root",
$pw = "password",
$port = "3306") {
parent::__construct($host,$user,$pw,$base,$port);
}
}
if (!($db = new kobling())) {
die("Kunne ikke koble til databasen.");
}
Any suggestions or solutions?

Connection Issue - Unable to connect to database server?

I am getting this message in my site all of a sudden without making any changes in the config file. I will post my config code to see if there are any issues with it.
define('DB_SERVER', 'www.victorexoticagoa.com');
define('DB_SERVER_USERNAME', '******');
define('DB_SERVER_PASSWORD', '********');
define('DB_DATABASE', 'victor');
define('USE_PCONNECT', 'false');
define('STORE_SESSIONS', 'mysql');
define('CFG_TIME_ZONE', 'Asia/Kolkata');
The above code is from the configure.php file.
The below code is the connection:
tep_db_connect() or die('Unable to connect to database server!');
And the code below is the function which does the connection:
function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
global $$link;
if (USE_PCONNECT == 'true') {
$server = 'p:' . $server;
}
$link = mysqli_connect($server, $username, $password, $database);
if ( !mysqli_connect_errno() ) {
mysqli_set_charset($$link, 'utf8');
}
return $$link;
}
Any help will be gladly appreciated. Thanks
Check if your database is up by using the following command in a command shell:
mysql --host=www.victorexoticagoa.com --port=yourport --user=youruser --pass=yourpass
If it cannot connect, the problem is the server, not your code.
Have you checked if that is the way you need to connect or the port to connect? I dont see the port (thats not supposed to be port 80 most times). Please check docs of the host and ask for details. It should be documented.

Echo data not being displayed

I am new to php development.I have created a database using phpmyadmin.Now i want all the data to be displayed on the page.But when ever i try to run the file nothing is showing up.I dont know why this is happening.
db_config.php
<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "moviesreview"); // database name
define('DB_SERVER', "localhost"); // db server
$con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
if($con->connect_errno > 0) {
die('Connection failed [' . $con->connect_error . ']');
}
db_connect.php
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error($con));
// Selecing database
$db = mysqli_select_db($con,DB_DATABASE) or die(mysqli_error()) or die(mysqli_error($con));
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
Code
<?php
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
$con=new db_connect();
$strQuery="SELECT * FROM `moviesdetails WHERE `recent_upcoming` =0";
$result=mysqli_query($con,$strQuery);
while($row=mysqli_fetch_array($result)){
$row_array['movies_id']=$row['movies_id'];
$row_array['movies_name']=$row['movies_name'];
$row_array['movies_description']=$row['movies_description'];
$row_array['movies_time']=$row['movies_time'];
$row_array['movies_release_date']=$row['movies_release_date'];
$row_array['movies_youtube_link']=$row['movies_youtube_link'];
$row_array['recent_upcoming']=$row['recent_upcoming'];
$row_array['movies_image']=$row['movies_image'];
$row_array['movies_actors']=$row['movies_actors'];
array_push($response,$row_array);
}
echo json_encode($response);
?>
Error what i am getting
There are a few things wrong with your code.
The space between <? and php
A missing tick for your table, and a missing semi-colon for your query.
A missing DB connection link.
Your code:
<? php
^ space
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
$strQuery="SELECT * FROM `moviesdetails WHERE `recent_upcoming` =0"
^ ^
$result=mysqli_query($strQuery);
^ no DB connection
<?php
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
$strQuery="SELECT * FROM `moviesdetails` WHERE `recent_upcoming` =0";
$result=mysqli_query($con,$strQuery);
Using error reporting http://php.net/manual/en/function.error-reporting.php would have signaled that, including or die(mysqli_error($con)) to mysqli_query()
You've edited your question. You are mixing mysqli_ and mysql_ functions. They do not mix.
All mysql_error should be mysqli_error($con) and mysql_close() to mysqli_close($con)
Edit:
Replace the entire contents of db_connect.php with the following:
<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "moviesdetails"); // database name
define('DB_SERVER', "localhost"); // db server
$con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
if($con->connect_errno > 0) {
die('Connection failed [' . $con->connect_error . ']');
}
Edit #2:
while($row=mysqli_fetch_array($result)){
$response[] = $row;
$row['movies_id'];
$row['movies_name'];
$row['movies_description'];
$row['movies_time'];
$row['movies_release_date'];
$row['movies_youtube_link'];
$row['recent_upcoming'];
$row['movies_image'];
$row['movies_actors'];
}
echo json_encode($response);
You have missing connection link as parameter in your mysqli_query() function. Should be:
$result = mysqli_query($connection_link, $strQuery);
http://php.net/manual/en/mysqli.query.php
$connection_link will have in this case following format:
// include db connect class
require_once __DIR__ . '/db_connect.php';
$connection_link = new DB_CONNECT();
But as #Fred -ii- said in comments, you have some syntax errors in PHP and SQL query too.
Your DB class is badly designed, you should store connection link in property and making queries through this class.

multiple mysql db connection on zencart

I have a requirement to connect to two separate databases for a client. One db holds all the standard store data, customers, orders etc, the other is to hold around 41,000 promo codes.
I've looked at PDO to connect to multiple db's as that is the new standard, but i have no idea how to achieve this when we have a configure.php file that contains the db connection info.
configure.php listed below:
define('HTTP_SERVER', 'http://localhost');
define('HTTPS_SERVER', 'https://localhost');
// Use secure webserver for checkout procedure?
define('ENABLE_SSL', 'false');
// NOTE: be sure to leave the trailing '/' at the end of these lines if you make changes!
// * DIR_WS_* = Webserver directories (virtual/URL)
// these paths are relative to top of your webspace ... (ie: under the public_html or httpdocs folder)
define('DIR_WS_CATALOG', '/');
define('DIR_WS_HTTPS_CATALOG', '/');
define('DIR_WS_IMAGES', 'images/');
define('DIR_WS_INCLUDES', 'includes/');
define('DIR_WS_FUNCTIONS', DIR_WS_INCLUDES . 'functions/');
define('DIR_WS_CLASSES', DIR_WS_INCLUDES . 'classes/');
define('DIR_WS_MODULES', DIR_WS_INCLUDES . 'modules/');
define('DIR_WS_LANGUAGES', DIR_WS_INCLUDES . 'languages/');
define('DIR_WS_DOWNLOAD_PUBLIC', DIR_WS_CATALOG . 'pub/');
define('DIR_WS_TEMPLATES', DIR_WS_INCLUDES . 'templates/');
// * DIR_FS_* = Filesystem directories (local/physical)
//the following path is a COMPLETE path to your Zen Cart files. eg: /var/www/vhost/accountname/public_html/store/
define('DIR_FS_CATALOG', '/');
define('DIR_FS_DOWNLOAD', DIR_FS_CATALOG . 'download/');
define('DIR_FS_DOWNLOAD_PUBLIC', DIR_FS_CATALOG . 'pub/');
define('DIR_WS_UPLOADS', DIR_WS_IMAGES . 'uploads/');
define('DIR_FS_UPLOADS', DIR_FS_CATALOG . DIR_WS_UPLOADS);
define('DIR_FS_EMAIL_TEMPLATES', DIR_FS_CATALOG . 'email/');
// define our database connection
define('DB_TYPE', 'mysql');
define('DB_PREFIX', ''); // prefix for database table names -- preferred to be left empty
define('DB_SERVER', 'localhost');
define('DB_SERVER_USERNAME', '');
define('DB_SERVER_PASSWORD', '');
define('DB_DATABASE', '');
// The next 2 "defines" are for SQL cache support.
// For SQL_CACHE_METHOD, you can select from: none, database, or file
// If you choose "file", then you need to set the DIR_FS_SQL_CACHE to a directory where your apache
// or webserver user has write privileges (chmod 666 or 777). We recommend using the "cache" folder inside the Zen Cart folder
// ie: /path/to/your/webspace/public_html/zen/cache -- leave no trailing slash
define('SQL_CACHE_METHOD', 'none');
define('DIR_FS_SQL_CACHE', '/enter/your/path/to/public_html_or_htdocs/and/zencart/here/zen/cache');
Digging through the php files i found this, which appears to be where the connection is created:
// Load queryFactory db classes
require(DIR_FS_CATALOG . DIR_WS_CLASSES . 'db/' .DB_TYPE . '/query_factory.php');
$db = new queryFactory();
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
I looked at using:
try {
$db1 = new PDO('mysql:dbname=databas1;host=127.0.0.1', 'username', 'password');
$db2 = new PDO('mysql:dbname=databas2;host=127.0.0.1', 'username', 'password');
} catch (PDOException $ex) {
echo 'Connection failed: ' . $ex->getMessage();
}
in place of the existing:
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
but i'm concerned this means that all the existing queries will need to be changed to cover the $db1, $db2 issue.
Both databases are located on the same server and use identical user/pass combinations.
Any help greatly appreciated.
I tried, and failed with PDO and ended up going with:
$db = new queryFactory();
$db2 = new queryFactory();
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE, USE_PCONNECT, false);
$db2->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE2, USE_PCONNECT, true);
Also had to add the define for DB_DATABASE2 to the configure.php file.
Hope it helps other zen cart users.
Simple really :)

Using php config file to connect to database instead of PDO connection line

I have the following lines Of code for a database connection, however I would just want to use my include("config.php") Instead of this long line of code, how would i go about doing that
try {
$dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
die('unable to connect to database ' . $e->getMessage());
}
// create LM object, pass in PDO connection
$lm = new lazy_mofo($dbh);
Just make a config.php file, for example consider this file is stored in web root (/var/www/), then the contents of the config.php are like this:
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
die('unable to connect to database ' . $e->getMessage());
}
// create LM object, pass in PDO connection
$lm = new lazy_mofo($dbh);
?>
then in your other PHP scripts use the include function:
<?php
include '/var/www/config.php';
...
Now in your PHP scripts you can access the $lm object and $dbh object.
for example include("config.inc.php") and for the config.inc.php:
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=lms;charset=UTF-8', 'root', 'password');
}
catch (PDOException $e) {
die('unable to connect to database ' . $e->getMessage());
}
$lm = new lazy_mofo($dbh);
?>
You can create a db wrapper class in your include file and get really lazy about it =)
define('DB_TYPE', 'mysql');
define('DB_HOST', 'localhost');
define('DB_NAME', 'lms');
define('DB_USER', 'root');
define('DB_PASS', 'password');
class Database{
public function __construct() {
$db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
$o = new Database();

Categories