How to use a offsite mysql database - php

i hope this makes since so ill try my best to explain as much as i can
i have a personal server that i use for testing my scripts before i put them on my home page in my home page i have a script that uses a feed from a database on another server
how would i be able to get the contents from the off server to the server i am testing on can i do it like normal just change the urls?
e.g:
//Before//
$dbms = 'mysql';
$dbserver = 'localhost';
$dbport = '';
//after//
$dbms = 'mysql';
$dbserver = 'homesite.com';
$dbport = '21';
hope this makes since and if it has been answered before sorry just point me to right direction :)
Thanks
Rob

MySQL's usual port is 3306, so that not only needs to be forwarded through any router(s) there may be, but also allowed on any firewall(s). Further, the users themselves in MySQL need access, unless their host in the mysql.users table is set to %, which I don't recommend, however, instead have your IP and/or hostname (depending on MySQL's look up settings in my.ini), however if the IP address changes, you'll need to update this.
Some more info:
http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

Related

PHP WMI Query timeout

I am scanning a subnet to gather WMI information on assets on the network. However, if a machine is stuck in POST or has some unknown issue, it will freeze the script and never move on to the next machine. Question is, is there a way to set a timeout on PHP COM WbemScripting.SWbemLocator?
$host = 10.1.1.5; //Host is online, but may be hung trying to shutdown
//Check if host is online
if(#$fp = fsockopen($host,135,$errCode,$errStr,0.1))
{
//Create new connection
$WbemLocator = new COM ("WbemScripting.SWbemLocator");
//Connect to remote workstation
$WbemServices = $WbemLocator->ConnectServer($host, 'root\\cimv2',$user,$password);
$WbemServices->Security_->ImpersonationLevel = 3;
//Basic WMI query
$system = $WbemServices->execQuery("Select * from Win32_ComputerSystem");
foreach($pcsystem AS $n){
$hostname = $n->Name; //Hostname
}
//Process all data ->Insert into DB
}
//Move on to next machine. In this case, the script will never move on
Short answer, no - because it's a COM object, not a PHP object, and there's no facility to control this directly.
Longer, speculative answer, you could try tweaking the TCP parameters via the registry notably TcpInitialRtt although there doesn't seem to be a lot of scope for changing the behaviour unless you are running on a very reliable and uncongested network. And you'll probably break other things running on the machine.

Joomla 2.5 Database Connection

I am working on a Joomla website and we are running jomsocial, my question is about the database connection using JFactory. Currently I need information from a database table that is not part of jomsocial so I have to get in the database from a specific page, and I'm doing it wrong. Only because I can't figure out how to make the right way work, so I'm asking here for help. Currently I am just using regular php and mysql_connect.. my DB connection looks like...
$db_host = 'localhost';
$db_user = 'xxxxxxxx';
$db_pass = 'xxxxxxxx';
$db_database = 'xxxxxxxx';
$link = mysql_connect($db_host,$db_user,$db_pass) or die('Not connected');
mysql_select_db($db_database,$link);
$user = CFactory::getUser($userid);
$user_plan = mysql_query("SELECT blah FROM blah WHERE blah = '".$user->id."'");
$user_plan_row = mysql_fetch_array($user_plan);
And this works, even though its wrong. What I want to do is use
JFactory::getDbo();
I have found this page which describes the syntax well I think, but I can not implement any of the methods described without breaking the page...
Any Ideas or help is greatly appreciated, as it stands right now I have all my secure information in the file and I don't want that I want to use the correct method, because when it comes time to move the site I'm going to have to go change each one of these manually instead of just changing the config file like usual.
The resource you linked to seems fine to me , you will need to post some specific errors.

Connecting to database via php code

I am programming a game in PHP and have the following code to connect to a database
//$sqldb=mysql_connect('godaddy.hostedresource.com', 'godaddyUserName', 'godaddyPassword') OR die ('Unable to connect to database! Please try again later.');
$sqldb=mysql_connect('localhost', 'root', 'mypassword') OR die ('Unable to connect to database! Please try again later.');
The trick here is that if I am on the production server I comment out the godaddy database; when I upload the code to the server I then comment out the localhost code instead.
Unfortunately the ineveitable has happened and I uploaded the code with the wrong connection commented out; this led to 24 hours of locked out customers! :(
Is there a way to have the code to tell if it is on the localhost server, and if it isn't it then looks for the godaddy connection?
you can try this to identify if its on live or localhost
if($_SERVER["SERVER_NAME"] == "localhost"
&&
$_SERVER["SERVER_ADDR"] == "127.0.0.1"){
// in localhost
$hostname = "localhost";
$username = "localuser";
$password = "localpassword";
}else{
// not in localhost
$hostname = "livehost";
$username = "liveuser";
$password = "livepassword";
}
and fail if couldn't connect to database but save the error into a file.
if(!mysql_connect($hostname,$username,$password)){
file_put_contents("mysql_connect.error",mysql_error(),FILE_APPEND);
die("Couldn't connect to database");
}
a suggestion, try not to use mysql_* anymore, switch to PDO or mysqli ..
if ($_SERVER['SERVER_NAME'] == 'the.name.of.your.devel.server') {
$host = 'localhost';
} else {
$host = 'name.of.godaddy.server';
}
$sqldb = mysql_connect($host, ....);
i normally use a method of obtaining the URL / domain of the site? This can work in certain situations and setups. Otherwise if your operating with a fixed IP than you can also use this method
Have a look over the methods using $_SERVER
PHP $_SERVER
One way would be for you to check your external IP address and see where you are. A solution should present itself by looking at the properties inside the $_SERVER global variable.
I have a good suggestion : You coding a game , game is a big program, you don't use mysql* function directly in big program , because yourself should handling them, such as error handling.i suggest you use a DB-Handler. please google for : DB-Handler PHP
As has been mentioned by other people, you can obtain the current site your script is running on using the $_SERVER variable. However, I would like to provide an alternative solution.
You could make a folder in your website (both local and production), something like config, then store a configuration file in it, for example config.php, with the following:
<?php
// Local
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'mypassword';
?>
And for production:
<?php
// Production
$db_host = 'godaddy.hostedresource.com';
$db_username = 'godaddyUserName';
$db_password = 'godaddyPassword';
?>
and disallow access to the directory with a .htaccess file in the directory, something like:
deny from all
Then, in your PHP code, do the following:
<?php
require_once($_SERVER["DOCUMENT_ROOT"] . "/config/config.php");
$sqldb=mysql_connect($db_host, $db_username, $db_password) OR die ('Unable to connect to database! Please try again later.');
?>
Now, simply leave the different configuration files where they're at and upload everything else, so your code will access different configuration files whenever it runs.
Also, the .htaccess file should prevent anyone from accessing the file via HTTP, and having the file contents in PHP tags, as well as a .php extension should prevent anyone from seeing any contents if they were able to access the file (PHP would parse the file before it is rendered, and would output nothing).

can not access to database in moodle

I am using moodle and I change location of my web site, but I am facing this error.
My config.php is:
<?php /// Moodle Configuration File
unset($CFG);
$CFG->dbtype = 'mysql';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'lightsys_test';
$CFG->dbuser = 'lightsys_test';
$CFG->dbpass = '123456a';
$CFG->dbpersist = false;
$CFG->prefix = 'mdl_';
$CFG->wwwroot = 'http://www.lightsystem.ir/aya/moodle';
$CFG->dirroot = '/home/lightsys/public_html/aya/moodle';
$CFG->dataroot = '/home/lightsys/public_html/aya/moodledata';
$CFG->admin = 'admin';
$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode
require_once("$CFG->dirroot/lib/setup.php");
// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,
// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.
// /home/lightsys/public_html/aya/moodle/
?>
And my error is:
Error: Database connection failed.
It is possible that the database is overloaded or otherwise not running properly.
The site administrator should also check that the database details have been correctly specified in config.php
Thanks to everyone.
My problem is solved. I had not set the right permission to my database, for my user, and when I did it my problem was solved.
This error message could mean:
mysql server is not running on localhost (the indicated server)
any of the other parameters (db name, login, password) in config.php are wrong
Can you connect to mysql using those credentials through the mysql command line client (or phpmyadmin or similar)?
You may want to post more info about your server environment.
I had the same problem.
I edited the database port because I'm not using the default MySQL port which is 3306.
If you are using the default port, leave it as an empty string.
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO username#localhost;
Use the following query to grant permissions to your user. Remember to change username with your db user that you've created.

Database connection from one server to another server

Is it possible to connect to a database lying on different server through another server ?
For example :-
I want to access the users table of www.abc.com from www.xqz.com, both are on different server.
Can you give me any ideas ?
will really appreciate if provide me the php query to connect.
Thanks.
"Is it possible" is a bit over-reaching, I think. To give you the shortest answer, yes, it is possible. However, whether or not you could do it depends on a lot of variables, not least of which is whether your hosting providers allow it. For instance, I use Hostmonster for Linux hosting, and to remotely access my MySQL database I have to first whitelist the IP address of the machine that will be accessing it.
Most hosting services provide a F.A.Q. section that should do a decent job of answering this particular question for you. Some will even provide support technicians help you set it up (Hostmonster does, within reason).
HTH.
Try This :
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
// Suppose your "www.abc.com" has an IP = 192.168.1.1
// So to connect, do something like this :
$server = '192.168.1.1,PORT_NUMBER'; // FOR WINDOWS
$server = '192.168.1.1:PORT_NUMBER'; // FOR LINUX
// Connect to MSSQL
$link = mssql_connect($server, [username], [password]);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>

Categories