Error while connecting to Database on hosted server - mysql

Warning: mysql_connect(): (HY000/2002): Connection refused in
/home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line
7
Warning: mysql_select_db(): No such file or directory in
/home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line
8
Warning: mysql_select_db(): A link to the server could not be
established in
/home/vol14_1/byethost31.com/b31_16461744/htdocs/Mysql/con.php on line
8
I have the below code
<?php
$localhost="localhost";
$username=b31_16461744;
$pass=test123;
$dbname=b31_16461744_user;
$a= mysqli_connect($localhost,$user,$pass);
mysql_select_db($dbname);
if($a)
{
echo "connected..";
}
else
{
echo "not...!!";
}
?>

Sidenote: Assuming the credentials are correct, given to you by your web host.
There are several problems with this code (taken from a comment you left).
Firstly, three of your declarations are not quoted and are being treated as constants.
PHP error reporting would have thrown notices of undefined constants.
These are treated as constants:
$username=b31_16461744;
$pass=test123;
$dbname=b31_16461744_user;
You are also referencing the wrong variable for the username being $user which should be $username. Error reporting would have signabled an undefined variable notice.
Then you're mixing mysql_ with mysqli_ syntax. Those different MySQL APIs do NOT intermix. You must use the same one throughout your code.
Sidenote: The other question you posted Access denied for user 'test123'#'192.168.0.38' (using password: NO) you are using sql306.byethost31.com for the host. Make sure that is correct. I have no idea what settings that host wants you to use.
<?php
$localhost="localhost";
$username="b31_16461744";
$pass="test123";
$dbname="b31_16461744_user";
$a= mysqli_connect($localhost, $username, $pass);
mysqli_select_db($a, $dbname);
if($a)
{
echo "connected..";
}
else
{
echo "not...!!";
}
?>
or just use all four parameters:
<?php
$localhost="localhost";
$username="b31_16461744";
$pass="test123";
$dbname="b31_16461744_user";
$a= mysqli_connect($localhost, $username, $pass, $dbname);
if($a)
{
echo "connected..";
}
else
{
echo "not...!!" . mysqli_error($a);
}
?>
However, your else with the echo does not help you. Use mysqli_error() to get the real error.
I.e.: or die("Error " . mysqli_error($a));
Example from the manual
$link = mysqli_connect("myhost","myuser","mypassw","mydb")
or die("Error " . mysqli_error($link));
References:
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.mysqli-connect.php
http://php.net/manual/en/language.constants.php
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production

I Think Credentials are not correctly set. See Your connection statement.
For Reference :
While Working On Localhost, We write connection statement as :
$con=mysql_connect("localhost","root","");
$db1=mysql_select_db("DatabaseName",$con);
But, While working on server, we need to change the following credential.
Username and password values are must.
$con=mysql_connect("localhost","Username","password");
$db1=mysql_select_db("DatabaseName",$con);

Related

I cant connect to my MYSQL Database using Php

Hi i have a problem connecting with my Mysql database i have the following code:
<?php
$servername = "dt5.ehb.be";
$username = "TVOSAPP";
$password = "*****";
$dbname = "TVOSAPP";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT storyId, title, score FROM Stories";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["storyId"]. "Naam" . $row["title"]. "score" . $row["score"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
which gives the following error:
Connection failed: Access denied for user 'TVOSAPP'#'10.3.101.30' (using password: YES)
I hope you can help me out to solve this problem it would be very much appreciated
the user that you are trying to connect with might not have the permission to connect remotely to the DB. please verify the same. To give remote access you might need to run
grant <privilage name> on TVOSAPP.* to 'TVOSAPP'#'<youripaddress>' identified by <yourpassword>
Ref: http://dev.mysql.com/doc/refman/5.7/en/grant.html
EDIT:
If your IP address is not static then you can use a percentage sign (%) in place of your IP address. However this approach is less secure as it allows any host to connect to the database remotely.
I fixed it about a week ago is had a space too much thats why I couldn't connect thank you all for your responses

mysql Access denied for user 'www-data'#'localhost'

I have ran into a wall with trying to connect to my site now it worked fine till I added a shoutbox but I don't think it has caused it to run into a Access denied for user 'www-data'#'localhost' (using password: NO).
I have several sites running and only this one site seems to be running into this problem I have reset password and still no luck please someone help me.
config.php
$site_settings['mysql_host'] = "localhost";
$site_settings['mysql_user'] = "root";
$site_settings['mysql_pass'] = "PasswordHash";
$site_settings['mysql_db'] = "project";
cms.php
function dbconn()
{
global $site_settings;
if (!#mysql_connect($site_settings['mysql_host'], $site_settings['mysql_user'], $site_settings['mysql_pass']))
{
switch (mysql_errno())
{
case 1040:
case 2002:
if ($_SERVER['REQUEST_METHOD'] == "GET")
die("<html><head><meta http-equiv='refresh' content=\"5 $_SERVER[REQUEST_URI]\"></head><body><table border='0' width='100%' height='100%'><tr><td><h3 align='center'>The server load is very high at the moment. Retrying, please wait...</h3></td></tr></table></body></html>");
else
die("Too many users. Please press the Refresh button in your browser to retry.");
default:
die("[" . mysql_errno() . "] dbconn: mysql_connect: " . mysql_error());
}
}
mysql_select_db($site_settings['mysql_db'])
or die('dbconn: mysql_select_db: ' . mysql_error());
mysql_set_charset('utf8');
}
screenshot - http://prntscr.com/b7nnc7
P.S I have gotten error_reporting(E_ALL); intact also.

How to solve mysql close warning

I have tried
this code-
$query=mysql_query("insert into ims(emp_name,emp_id,department,subject,date,matter)
value('".$_SESSION['name']."','".$_SESSION['eid']."','".$_SESSION['dept']."','".$_POST['subject']."','".$_POST['date01']."','".$_POST['textarea2']."')") or die("Inenatry Error");
<?php mysql_close($query);?>
I got error:
Warning: mysql_close(): supplied resource is not a valid MySQL-Link resource in
mysql_close() accepts a connection resource as parameter
Suppose you have your connection like this
$connection = mysql_connect(...);
then use
mysql_close($connection);
For more information see this.
Warning : mysql_* is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used
you have to do something like
$db_conn = mysql_connect("localhost", "root", "******");
mysql_close($db_conn);
Well, you'd have to show us this line. Generally, though, using mysql_close() isn't needed - the connection is automatically closed when the script has finished executing.
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
You are doing it wrong. mysql_close will take mysql connection resource not query object. So your code should like that.
$conn = mysql_connect('DB_HOST', 'DB_USER', 'DB_PASS');
mysql_select_db('DB_NAME');
$query = mysql_query("insert into ims(emp_name,emp_id,department,subject,date,matter) value('".$_SESSION['name']."','".$_SESSION['eid']."','".$_SESSION['dept']."','".$_POST['subject']."','".$_POST['date01']."','".$_POST['textarea2']."')") or die("Inenatry Error");
mysql_close($conn);

Getting "Unexpected Token < " on my Google Maps api v3

I have a problem, I'm using this project to use of base on my project. I tryied to get the same result of the guys project, and used the same project to test in my SQL.
I done the "index.php" works, and when I click "Save Route" it send a mensage "Updated", so, when I open "loady.htm" it give that error : "SintaxError: Unexpected Token < "
I used the same code, but changed the local host on process.php and the account and password.
But this is the unique change.
What's wrong on load.htm ? Or its an error on teste.php, I cant load the waypoints in "loady.htm"
Links for test:
www.inventoresdegaragem.com/dbteste/index.htm
and
www.inventoresdegaragem.com/dbteste/loady.htm
Edit 2: This is my process.php
<? ob_start(); header('Cache-Control: no-store, no-cache, must-revalidate');
#$data = $_REQUEST['*******'];
$host = 'localhost';
$usuario = '******';
$banco = '******';
$senha = '******';
$db = mysql_connect($host, $usuario, $senha);
mysql_select_db($banco, $db);
if($_REQUEST['command']=='save')
{
$query = "update mapdir set value='$data'";
if(mysql_query($query))die('bien');
//die(mysql_error());
}
if($_REQUEST['command']=='fetch')
{
$query = "select value from mapdir";
if(!($res = mysql_query($query)));
$rs = mysql_fetch_array($res,1);
die($rs['value']);
}
?>
Your process.php cannot connect to your database.
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'http' (1) in /home/i/inventoresdegara/www/dbteste/process.php on line 10
It would appear that your current live version of process.php does not have localhost specified as the server. Note that it should be just a server name and should not include the protocol:
$host = 'localhost';
$host = 'www.mydomainnamehere.com';
(or whatever domain name you want to use) and not
$host = 'http://www.mydomainnamehere.com';
I believe the error is occurring because the database error message I've reproduced above is formatted as HTML and starts with <:
<br />
<b>Warning</b>: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Unknown MySQL server host 'http' (1) in <b>/home/i/inventoresdegara/www/dbteste/process.php</b> on line <b>10</b><br />
The html on your "after" page is not valid
looks like jax.responseText is empty.

Can't connect to local MySQL server through socket

I'm getting the following error on my site when I upload it or submit a page:
mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
What in the world does this mean?
Since the error is being thrown by the call to mysql_real_escape_string() it rather implies that you didn't call mysql_connect() first and pass a valid db handle to mysql_real_escape_string (or the call to mysql_connect() failed).
In some circumstances, the mysql extension will attempt to connect automatically using the default settings in php.ini, failing over to my.cnf if these are not available - which obviously are not valid. Or it may be that the settings are valid but the mysqld is not running.
Have you got scripts which are connecting to the database successfully?
Do you have a username and password for the database?
Try:
check_running();
$user=''; // fill in your details
$password=''; // fill in your details
$hosts=array(
'localhost', '127.0.0.1', $_SERVER['HTTP_HOST'], $_SERVER['SERVER_ADDR']
);
foreach ($hosts as $addr) {
try_con($addr, $user, $password);
try_con($addr . ':3306', $user, $password);
}
function try_con($host, $user, $password)
{
$dbh=mysql_connect($host, $user, $password);
if ($dbh) {
print "Connected OK with $host, $user, $password<br />\n";
} else {
print "Failed with $host, $user, $password<br />\n";
}
}
function check_running()
{
// this assumes that you are using Apache on a Unix/Linux box
$chk=`ps -ef | grep httpd | grep -v grep`;
if ($chk) {
print "Checking for mysqld process: " . `ps -ef | grep mysqld | grep -v grep` . "<br />\n";
} else {
print "Cannot check mysqld process<br />\n";
}
}
Yes exactly, some servers pass the default connection parameters when using mysql functions before connection and throw off an error, some other servers work just fine wherever you place the code
it is always safer to just place mysql_real_escape_string() after establishing mysql_connect() connection