How can I use the External Database connection In Magento? - php

This is my core PHP code which is used in Magento.
$model_moodle = mysql_connect("localhost","root","123456")
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("moodle",$model_moodle)
or die("Could not select moodle");
//execute the SQL query and return records
$result = mysql_query("SELECT id FROM mdl_course where shortname ='".$data."'");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
$course_id = $row['id'];//display the results
}
//echo "Cource id is=".$course_id;
//close the connection
mysql_close($model_moodle);
I need to Use this code but Using the Magento External Database Connection.
I am using the "Moodle" database in Magento . but I need the code for Magento way.

Use this mysql command to connect to external database :
mysql -h clouddb.com -u root -p dbname;
It asks password for root, then enter password , Then you may login to your external DB!!

Related

how to create mysql database from sql query

I need to create a database from MySQL query. I have used the following, but it is not working.
$sql2 = "CREATE DATABASE DATABAE_NAME";
$query2=mysqli_query($connect_dude,$sql2);
This query is not creating any database. Probably,I have to use database username and password, but I'm not quite sure how to apply or use database username, password and host in this case.
Here is a sample php code about connecting DB
Hope this will help.
<?php
//mysqli query for connecting database
//mysqli_connect('host','username','password')
$connection=mysqli_connect('localhost','root','');
if($connection)
{
//saving query for creating database in a variable
$query="CREATE DATABASE student";
//creating Database
$newdb=mysqli_query($connection,$query);
//testing whether database created or not
if($newdb)
{
//setting database name to connect for database operation
$setDbName="student";
//query for connecting database
$connectionTesing=mysqli_select_db($connection,$setDbName);
if($connectionTesing)
{
echo "Connected!";
}
}
}
?>

Search for a term in mssql via php depending on an inconstant variable

Basically, I have a login panel where users can login and what I am trying to do is when they login I want to find out their Authentication ID that has been set in the MSSQL database.
And I want to be able to get the 'AuthID' of the user and make it 'die' (die $result) so that the result is shown in the browser.
At the moment I have this code and with it I am able to get the AuthID but it doesn't get the specific user that is logging in. It gets every single AuthID in the database.
<?php
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
$query = "SELECT nAuthID FROM tAccounts";
$result = mssql_query($query)
or die('A error occured: ' . mysql_error());
while ( $record = mssql_fetch_array($result) )
{
die ($record ['nAuthID']);
}
mssql_free_result($result);
mssql_close($dbhandle);
?>
Thanks.
If you want an user to be able to login to your site and stay logged in you have to save the information as which user he is logged in in some way -> SESSIONS / COOKIES
For login specific data you should use SESSION since their data is stored on the server.
Put session_start()at the beginning of every file you want to access any user information.
Then if the user logs in you should save the nEMID or even the nAuthID via $_SEESION['nID'] = ...your id...;
On any other site you can check if the user is logged in using if (isset($_SEESION['nID'])). Then you can query user-information using your "SELECT * FROM tAccounts WHERE nEMID=" . $_SEESION['nID']
Does this helps u a bit?
In your code, after connecting to the database, you are directly executing a query "SELECT nAuthID FROM tAccounts" which will fetch all the available nAuthID's from the table.
Your query should authenticate the user after entering the login details in your login form.
So your query in your code should be some thing like below...
<?php
$myServer = "Your-PC\SQLExpress";
$myUser = "user";
$myPass = "pass";
$myDB = "Account";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
// $query = "SELECT nAuthID FROM tAccounts";
//Your Query Should be something like this
$query = "SELECT nAuthID FROM tAccounts WHERE sUsername='username' AND sUserpass= 'yourpassword' ";
$result = mssql_query($query)
or die('A error occured: ' . mysql_error());
/*while ( $record = mssql_fetch_array($result) )
{
die ($record ['nAuthID']);
} */
if(mssql_num_rows($result)>0){
$record = mssql_fetch_array($result);
$nAuthID = $record ['nAuthID'];
session_start();
$_SESSION['nAuthID'] = $nAuthID;
}else{
echo 'Login Failed';
}
mssql_free_result($result);
mssql_close($dbhandle);
?>
Please make sure that you will be passing the dynamic values to the query. and make a note that you can use the session variable $_SESSION['nAuthID'] to get nAuthID whereever you want to display.
By doing so, you will be getting nAuthID of a particular user who have logged in successfully. Also, you can use else loop if the authentication failed and redirect the user back to login screen.
Hope this clarify your doubt??

using php to compare mysql columns to SQL Server

I have two databases, one online (mysql) and one in my office (SQL Server) which I would like to compare and update where a value is different.
I am using php to connect to the SQL Server database and run a query to retrieve the information, then connecting to the Mysql database running a query. Then I need to compare the two queries and update where necessary.
Is there somewhere I can look for tips on how to do this, I am sketchy on PHP and struggling really.
This is as far as I have got-:
<?php
$Server = "**server**";
$User = "**user**";
$Pass = "**password**";
$DB = "**DB**";
//connection to the database
$dbhandle = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server");
//select a database to work with
$selected = mssql_select_db($DB, $dbhandle)
or die("Couldn't open database $DB");
//declare the SQL statement that will query the database
$query = "SELECT p.id, p.code, ps.onhand";
$query .= "FROM products p with(nolock)";
$query .= "INNER JOIN productstockonhanditems ps with(nolock)";
$query .= "ON ps.ProductID = p.ID";
$query .= "WHERE ps.StockLocationID = 1";
//execute the SQL query and return records
$get_offlineproduct2 = mssql_query($query);
mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$get_onlineproducts = mysql_query(SELECT w.ob_sku, w.quantity
FROM product_option_value AS w
ORDER BY ob_sku)
or die(mysql_error());
//close the connection
mssql_close($dbhandle);
?>
I am looking to compare the value p.code to w.ob_sku and whenever they match copy the value of ps.onhand to w.quantity so the online database has the correct quantities from the office database.
My question I guess is how close am I to getting this right? Also am I doing this the right way, I don't want to get so far and realise that i am just wasting my time...
Thanks!
You do not need to fetch any record from MySQL, since you actually want to update it.
I would do something like this:
$query = 'SELECT p.code, ps.onhand FROM (...)';
// execute the SQL query and return a result set
// mssql_query() actually returns a resource
// that you must iterate with (e.g.) mssql_fetch_array()
$mssqlResult = mssql_query($query);
// connect to the MySQL database
mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
while ( $mssqlRow = mssql_fetch_array($mssqlResult) ) {
$mssqlCode = $mssqlRow['code'];
$mssqlOnHand = $mssqlRow['onhand'];
mysql_query(
"UPDATE product_option_value SET quantity = $mssqlOnHand WHERE ob_sku = $mssqlCode"
// extra quotes may be required around $mssqlCode depending on the column type
);
}

Connecting to remote mySql database manually

I want to know how to connect to a MySql database in a remote server from windows 7 .I want to give an external connection from Administrative panel.How to give connection to the database ? I have a php coding to insert and retrieve values from the remote database .But before that i need to connect to the remote database manually ? Pls can you help me out with this .#
<?PHP
$user_name = "fbapp";
$password = " 123";
$database = "fb_db";
$server = "mysql.dfw1.stabletransit.com";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "INSERT INTO Sample(Firstname, Lastnamme)
VALUES (35,67)");
$result = mysql_query($SQL);
mysql_close($db_handle);
echo "Records added to the database";
}
else {
echo "Database NOT Found ";
mysql_close($db_handle);
}
?>
Simply create a mysql user and allow the same from your host to connect. So create user at
example1.com MYSQL and allow your new user to connect from your example2.com.
Then allow a firewall rule on mysql port from that very host
open command prompt
mysql -u user_name -ppassword -h ip-adress
use ip-adress of machine where mysql is running

mysql php : switching between mysql databases is slow

In my php script which connects to mysql, I have to query 2 databases in the same script to get different information. More specifically Faxarchives in one database and Faxusers in the other.
In my code I query faxusers and then foreach user, I query Faxarchives to get the user history.
I might do something like:
function getUserarchive( $userid) {
$out= "";
$dbname = 'Faxarchive';
$db = mysql_select_db($dbname);
$sql = "select sent, received from faxarchivetable where userid = '" . $userid . "'";
if ( $rs = mysql_query($sql) {
while ($row = mysql_fetch_array($rs) ) {
$out = $row['sent'] . " " . $row['received'];
}//end while
}//end if query
return ($out);
}//end function
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'Faxusers';
$db = mysql_select_db($dbname);
$sql="select distinct userid from faxuserstable";
if ( $rs = mysql_query($sql) {
while ($row = mysql_fetch_array($rs) ) {
$out = $row['userid'] . ":" . getuserarchive($row['userid']);
}//end while
}//end if query
I'm guessing the switching between databases for each user is causing the slowness. Anyways how i can improve the speed of the processing?
thanks in advance.
You have several problems. First, your function, getUserarchive, pulls back all rows all the time. Looks like you forgot to restrict your SQL to only a specific userid. That's a big cause of your speed issues.
Another option would be to pass in all userids you are interested in. Rewrite the SQL as SELECT sent, received FROM faxarchivetable WHERE userid IN (...), which means you'd have one select to pull back all info from your faxarchivetable, instead of one per each userid.
Finally, if both databases are on the same MySQL server, you could just do a single select to pull in all the information:
SELECT Faxusers.faxuserstable.userid, sent, received FROM Faxusers.faxuserstable JOIN Faxarchive.faxarchivetable ON Faxusers.faxuserstable.userid = Faxarchive.faxarchivetable.userid
Are you aware that you can query tables in separate databases by qualifying the tablename?
For example, I think you can get all your information in a single SQL query like this:
SELECT sent, received
FROM Faxarchive.faxarchivetable
WHERE userid IN ( SELECT DISTINCT userid FROM Faxusers.faxuserstable );
Oh, this is the same point ChrisInEdmonton makes. I didn't notice his answer.

Categories