Problems with Godaddy Database connections - mysql

I am having problem with connecting to Database in godaddy. I do not know is it my code that is giving the problem or godaddy that is giving the problem.
I tried 2 different ways of coding to connect to database;
Below is the working code:
$hostname = "xxx.db.xxx.hostedresource.com";
$username = "xxx";
$dbname = "xxx";
$password = "xxx";
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
Below is the non-working code:
define("DBHOST","xxx.db.xxx.hostedresource.com");
define("DBNAME","xxx");
define("DBUSER","xxx");
define("DBPASS","xxx");
mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
Or is it because godaddy can't use define function? Thanks guys.

You're defining DBHOST but using DB_HOST. The same problem with all other defined values as well. define is NOT banned on GoDaddy.

Related

Cannot connect to my Mysql DB with PHP7.0 and Apache2 (2 different VM)

I'm trying to connect to my MySQL DB located on 192.168.23.140 from my Web Frontend on .23.139
Here is my code :
$servername = "192.168.23.140";
$username = "web";
$password = "rootnetwork";
$dbname = "test";
try {
$conn = new PDO('mysql:host=$servername; dbname=$dbname', $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
Here is what i get :
Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
I've been looking for some answers on the internet but none of them work.
Thank you for your help
$variables are not expanded in a single quotes string literal, they are only expanded in a double quoted string literals.
So
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
You will also have to make sure that this MYSQL user account web is setup to allow connections from ip addresses that are not the ip of the machine running MySQL

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

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

Uploading MySQL database to server?

I have created a website that has a MySQL database. I'm using phpmyadmin and xampp. Now I have a website to upload the files to (using Filezilla). How do I upload the MySQL database to the server and connect it to the website? I'm guessing there is already a tutorial somewhere but I couldn't find it.
If you have your database file ready to upload then in phpmyadmin there is an import option as shown in the image below:
In order to connect it to your website will depend on your server side language choice...
Example with PHP to connect your website to your database (using pdo):
<?php
$dsn = 'mysql:dbname=name_here;host=127.0.0.1';
$user = 'root'; //change to your username
$password = '******'; //change to your password
try {
$pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
Once you have that at top of your PHP file you can connect. This one way to do it via PHP.

perl script to connect to mysql server port 3307

I am trying to connect to a mysql server which is running at port 3307. How do I connect to the server? I do not see any other way to specify port. I am using like this:
#!/usr/bin/perl
use Mysql;
$host = "localhost";
$database = "abc";
$tablename = "def";
$user = "uuu";
$pw = "ppp";
$connect = Mysql->connect($host, $database, $user, $pw) or die "Cannot connect to MySQL server\n";
I want to use MySQL package and not DBI.
Thank you.
You are mistaken. You want to use DBI and not Mysql. The Mysql module became obsolete 12 years ago, when it was replaced with a compatibility module that's just a wrapper around DBI. Even the compatibility module has been removed from the current distribution; you have to install an old DBD::mysql just to get it (it last shipped in DBD-mysql 3.0008, released back in 2006).
#!/usr/bin/perl
use strict;
use DBI;
my $host = "localhost";
my $database = "abc";
my $port = 3307;
my $tablename = "def";
my $user = "uuu";
my $pw = "ppp";
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port",
$user, $pw)
or die "Cannot connect to MySQL server\n";
Try specifying host like localhost:3307
For me...Following seems to be working ...
#!/usr/bin/perl
use strict;
use DBI;
my $host = "rajeshk-W7";
my $database = "rajesh";
my $port = 3307;
my $tablename = "def";
my $user = "rajesh";
my $pw = "rajesh123";
#my $dbh = DBI->connect("DBI:mysql:rajesh:rajeshk-W7","rajesh","rajesh123") or die "Cannot connect to MySQL server\n";
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host",$user, $pw) or die "Cannot connect to MySQL server\n";
where i dint mentioned port. When i add port, its unable to connect.