I can't access my database in localhost using xampp - php

i use Xampp and its not work on port 80 so i've changed it to 8080 and now apache and mysql works fine. my problem is that when im try to access to a database that i already created on phpmyadmin it wont work!
i use this code:
<?php
$requesturi = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$pos = strpos($requesturi, "uni");
$hostname_excal = "localhost:8080";
$database_excal = "my_database";
$username_excal = "root";
$password_excal = "123456";
$excal = mysql_pconnect($hostname_excal, $username_excal, $password_excal) or trigger_error(mysql_error(),E_USER_ERROR);
?>
i try localhost for hostname and it doesnt work either.
i test this in my online host and its works fine. but i need this to work on localhost so i can present this in a meeting.
help me please.

Did you changed the standart password? Xampp uses "" as initial password.
Try this:
$hostname_excal = "localhost";
$database_excal = "my_database";
$username_excal = "root";
$password_excal = "";

It looks like you have miss spelled the php function it should be:
mysql_connect
you have
mysql_pconnect
also I strongly recooment you do not use mysql at all, it is varnible to injections, try learing mysli or pdo

Related

How to connect to database on freewebhostingarea?

Ok so i am making account system for my website. I am using freewebhostingarea as host. But i dont know what is servername(for connecting to database). Can you help me? Here is my code for connecting:
<?php
$servername = "";
$server = "root";
$password = "my password";
$dbName = "name";
$conn = mysqli_connect($servername, $server, $password, $dbName);
?>
I dont know what is $servername for freewebhoatingarea. Pls reply.
It's all the same actually . I want you to try this 4 steps
After you hit create databaase it will give you an option to name/rename your database so save that name to something like a notepad for your reference
(e.g) freehosting_db_(sampledatabase)
Next to that it that before you hit next on the creation of database kindly remember that password that you are going to make and like the name of the website save it to a notepad so you can remember the password
(e.g) xHsnaAXa123 -> password database
Mostly the server always localhost on SOME freehosting
The servername is automatically generated so look for it. It will be something like
(e.g) freehosting_db_(sampledatabase)_user
And to set that up
$servername = "freehosting_db_(sampledatabase)_user";
$server = "localhost";
$password = "xHsnaAXa123";
$dbName = "freehosting_db_(sampledatabase)";
PS: This is mostly how you setup a connection from your database to your server . But it is always depends on the the hosting that you are going to use . Godadday, Hostgator, 000webhostingapp, they are all alike on how setting it up .

How to find PHPMyAdmin database details

$myhost = "";
$myuser = "";
$mypass = "";
$mydb = "";
Where in the PhpMyAdmin panel would I find these details? I can't find them on my hosting panel! (I use MediaTemple Grid Hosting)
I have the username and password but don't know what to put for myhost and mydb!
My PHPMyAdmin Username to login looks like this: db111111_test
But with different numbers after db.
Would db111111 be the db or would _test or test be the db? Also what would the host be?
http://(ip-of-server)/phpmyadmin
or you can read this
https://mediatemple.net/community/products/dv/204643690/managing-a-mysql-database-with-phpmyadmin

I can't connect Joomla! remotely with MySQL

I have two hosting (1: IP 192.186.234.192 and 2: IP 74.116.84.169), and I have Joomla website files on hosting 1, Joomla mysql database on hosting 2, both hosting 1 and 2 have Cpanel administration.
I want run Joomla website on host1 by linked it remotely to mysql database on host 2
Steps that I made:
In hosting 2 added IP address for hosting 1 in (Remote MySQL).
In Joomla configuration.php file I have changed database section like:
var $host = '74.116.84.169';
var $user = 'artjour';
var $password = 'abc#123';
var $db = 'apuser';
I also have changed the host name by server name like:
var $host = 'servername.mydomainname.com';
But unfortunately:(, Could not connect to MySQL, please it's very important for me to know what's my wrong here.
Thanks in advance.
Use a small PHP script like this to see the error other then "Could not connect to MySQL":
<?php
$db_host = "123.456.789";
$db_name = "database";
$db_user = "user";
$db_pass = "password";
$db_table_prefix = "prefix_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;
if(mysqli_connect_errno()) {
echo "Conn Error = " . mysqli_connect_error();
exit();
}
?>
in the hope that this may help someone I resolved the same situation.
I could access from server A and from command line mysql but I also could not connect my remote joomla database, the httpd server side needed selinux setsebool httpd_can_network_connect_db=1 to enable apache to connect to the remote database.
You probably have to allow remote SQL connection on server2 with Cpanel :
http://www.liquidweb.com/kb/enable-remote-mysql-connections-in-cpanel/

host is not allowed to connect to this server

quick mysql question.
I'm new at php/mysql and followed a tutorial(php/mysql for dummies) so I don't really know what I did wrong or if the tutorial is wrong.
I have a file, "database_connections.inc", that looks like this:
<?php
$user = "username";
$host = "host";
$password = "password";
$database = "database";
?>
With the actual credentials not included for obvious reasons.
Then in another file, login.php, I have:
include("database_connections.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die("Query died: Couldnt connect to server.");
I get an error message with the "or die" text, accompanied by a warning:
host xxxxx.000webhost.com is not allowed to connect to this mysql server in....
Why not? I'm sure my credentials are all correct.
I've read in a few places to run some shell statements...but can't really do that, I'm on Windows.
I'm using phpMyAdmin, so hopefully I can do something from there?
Open "database_connections.inc" and change it to look like this:
<?php
$user = "root";
$host = "localhost";
$password = "";
$database = "test";
?>
MySQL is by default configured to work with localhost (or 127.0.0.1), in order to allow "host xxxxx.000webhost.com" as host, open phpMyAdmin and select "SQL" and execute this query;
GRANT ALL ON your_database_name.* TO your_user#your_host_xx.xxx.xx.xx IDENTIFIED BY 'your_password';
Go into PHPMyAdmin, and edit your user.
Under Login Information, there should be an option for "Host"- try adding xxxxx.000webhost.com.

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).

Categories