how to use multiple database in opencart? - php

I have two project, first one in Yii and another in Open cart, I need use both DB in a project, in Yii site I have complete, now I have a issue in open cart so please explain how to use multiple database in open cart?

You can use multiple database something like this:
// Define database
$host = 'localhost';
$db1 = 'username1';
$pwd = 'pass';
$db2 = 'user2';
$pwd2 = 'pass';
$con1 = mysql_connect($host, $db1, $pwd) or die ('Error connecting to mysql');
$con2 = mysql_connect($host, $db2, $pwd2) or die ('2nd Db Error connecting to mysql');
$db_name1 = '`mydatabase`';
mysql_select_db($db_name, $con1);
$db_name2 = '`mydatabase2`';
mysql_select_db($db_name2, $con2);
This is your database actions:
$result = mysql_query("Select * from ......", $con1) or die(mysql_error());
$result = mysql_query("Select * from ......", $con2) or die(mysql_error());

Related

php mysql search error Couldn't execute query

I have written a php script to fetch data but somehow it has stopped working and giving Couldn't execute query, I am unable to find the problem.
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
$id = $_GET['id'];
$sql = "SELECT * FROM datatable WHERE id = $id";
$result = mysql_query($sql) or die("Couldn't execute");
You have used 2 times database name here. Just use this
$db_username = 'uname';
$db_password = 'password';
$db_name = 'db_name';
$db_host = 'localhost';
$db_select =mysql_connect('localhost', 'root','');
$db_=mysql_select_db($db_name,$db_select);
$sql = "SELECT * FROM datatable WHERE id = $id";
$result = mysql_query($sql) or die("Couldn't execute");

Why isn't this php code allowing me to connect to my database?

I'm using a WAMP server and on it I created a database and a table. All the names are correct and the user has full access to everything. When I run the code, it prints out "Unable to select database". Thanks.
<?php
if(isset($_POST["Submit"])){
print_r ($_POST["nutrient"]);
}
session_start();
//establish connection
$server = "localhost";
$db_username = "root";
$db_password = "";
$database = "gainlife_cavin";
$table = "cavintable";
//connect PHP script to database
$connection = mysqli_connect($server, $db_username, $db_password, $database);
//select database to use
#mysql_select_db($database) or die( "Unable to select database");
//$query = "INSERT INTO $table VALUES("")"
//mysql_query($query)
mysql_close();
?>
<body>
</form>
Try something like the following.
<?php
//establish connection
$server = "localhost";
$db_username = "root";
$db_password = "";
$database = "gainlife_cavin";
$table = "cavintable";
//connect PHP script to database
$connection =mysqli_connect("$server","$db_username","$db_password","$database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Your query here
mysqli_close($connection);
?>
I use simple code 1 line. here is my code that i'm using.
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
You went from MySQLI to not using MySQLI in selecting the database as well as the rest of your code.
Try this. I have change mysqli_connect to mysql_connect and mysql_select_db variable.
//connect PHP script to database
$connection = mysql_connect($server, $db_username, $db_password, $database);
//select database to use
$select = mysql_select_db($connection) or die( "Unable to select database");

Backing up a MySQL database using PHP

I was trying to make a backup of my MySQL db called "backup" using a PHP script below, but for some reason, it doesnt work. Any ideas what is wrong? I wanted to create a file called test.sql in the same folder that would contain the data (because the db is quite big I only selected values of Temp>35, but I could change that later). Right now when I run it, I get the echo, but no file created.
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$tableName = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * WHERE Temp>35 INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
echo "Backed up";
?>
Just Try.
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName WHERE Temp>35";
$result = mysql_query($query) or die(mysql_error());
Try this and let me know:
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$databasename = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $databasename WHERE Temp>35";
$result = mysql_query($query);
echo "Backed up";
?>
<?php
$source_db='source_db';
$target_db='target_db';
$server='127.0.0.1';
$user='root';
$password='';
mysql_connect($server,$user,$password);
mysql_select_db($source_db);
// Get names of all tables in source database
$result=mysql_query("show tables");
while($row=mysql_fetch_array($result)){
$name=$row[0];
$this_result=mysql_query("show create table $name");
$this_row=mysql_fetch_array($this_result);
$tables[]=array('name'=>$name,'query'=>$this_row[1]);
}
// Connect target database to create and populate tables
mysql_select_db($target_db);
$total=count($tables);
for($i=0;$i < $total;$i++){
$name=$tables[$i]['name'];
$q=$tables[$i]['query'];
mysql_query($q);
mysql_query("insert into $name select * from $source_db.$name");
}
?>

How to connect to database in real server

I am trying to connect to my database wich it created in PhpMyAdmin in my own server.
I write the code in a php file as below:
<?php
echo "welcome";
echo "<br>";
$conn = mysqli_connect("mydomainename.com:2080", "database_user_in phpmyadmin", "password of my database name", "name of database");
$result = mysqli_query( $this->conn, "SELECT * FROM `ad` WHERE 1");
while ( $row = mysqli_fetch_array( $result ) ) {
$ad_level = $row['ad_level'];
}
echo $ad_level;
?>
If I access to this page , its just return : welcome
I think that the error in the syntax of 'mysqli_connect' .... ist correct ??
have anyone any idea about this ?????
How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.
Best regards and thanks in advance, Fadel.
the real syntax is
mysqli_connect(host,username,password,dbname,port,socket);
so as you have written host name along with the port there must be an error.
you can refer the below link for more insite
http://www.w3schools.com/php/func_mysqli_connect.asp
Here is the general mySQL DB Connection Process:
<?php
// Create connection
$con = mysqli_connect("example.com", "peter", "abc123", "my_db");
// Check connection
if ( mysqli_connect_errno( $con ) ) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try this
Use $conn in place of $this->conn
$result = mysqli_query($conn, "SELECT * FROM `ad` WHERE 1");
If you have error in mysqli connection the use die after this function like
$conn= mysqli_connect("myhost","myuser","mypassw","mybd")
or die("Error " . mysqli_error($conn));// use die here
Read this http://php.net/manual/en/function.mysqli-connect.php
I replaced
$conn = mysqli_connect("mydomainename.com:2080", "database_user_in phpmyadmin", "password of my database name", "name of database");
by this:
$conn = mysqli_connect("localhost", "database_user_in phpmyadmin", "password of my database name", "name of database");
but why !!! i have not any idea !! just it worck ... why ???? have anyone any idea about this ????
Try this way. I am currently using this method and its working perfect:
<?php
$server = "localhost";
$login = "root";
$pw = "myPassword";
$db = "myDatabase";
$con = mysql_connect($server, $login, $pw);
mysql_select_db($db, $con);
$qry = "select * from members where Username ='$Username' and Password = '$Password' and Status = 1" ;
$conQry = mysql_query($qry , $con);
?>
NOW YOU CAN FETCH YOUR DATA

SQL not working unless multiple db_connect(); statements

In the past I've had no issues using one simple:
$link = db_connect();
in a single file with multiple SQL commands like so:
$sql = "UPDATE table SET...";
$sql_result = mysql_query($sql, $link)
or die("Couldn't execute query.");
$sql2 = "UPDATE table2 SET...";
$sql_result2 = mysql_query($sql2, $link)
or die("Couldn't execute query.");
Now any new file I create won't work without multiple/separate db_connects. For example:
$link = db_connect();
$link2 = db_connect();
$sql = "UPDATE table SET...";
$sql_result = mysql_query($sql, $link)
or die("Couldn't execute query.");
$sql2 = "UPDATE table2 SET...";
$sql_result2 = mysql_query($sql2, $link2)
or die("Couldn't execute query.");
The old files with multiple mysql_queries referencing a single db_connect(); are still working fine. What could have changed?
Thanks.
Updated to include the db_connect(); function:
function db_connect($db="database", $host="localhost", $user="user", $p="password") {
$dbcnx = #mysql_connect($host, $user, $p);
if (!$dbcnx)
{
echo( "<p>Unable to connect to the database server at this time.</p>" );
exit();
}
$database = #mysql_select_db($db, $dbcnx);
if (!$db)
{
echo "<p>Unable to locate the database at this time.</p>";
exit();
}
return $dbcnx;
}
If you want to have $links separated, add true as fourth mysql_connect() parameter:
$dbcnx = #mysql_connect($host, $user, $p, true);
Additionally if you want to use different connections (different databases, users, passwords), you need to explicitly pass parameters to db_connect() second time:
$link2 = db_connect('database2', 'whateverthehost', 'user2', 'andhispassword');
What you have posted looks fine unless you have something unset() ing $link, assigning it a value, or something else that is making $link no longer pointing to the database.

Categories