cannot connect to Msql data base - php

i cannot seem to create a connection to Mysql database from the following code. Can you please let me know if there is anything missing from this code. Since i have not used php code much i might have missed out something.
on submitting code im getting the following error
Warning: mysql_connect() [function.mysql-connect]: Host '31.170.160.80' is not allowed to connect to this MySQL server in /home/a5847996/public_html/check_db.php on line 15
here is the code im using
<body>
<?php
$server = "-";
$database = "-";
$username = "-";
$password = "**************";
$mysqlConnection = mysql_connect($server, $username, $password);
if (!$mysqlConnection)
{
echo "Please try later.";
}
else
{
mysql_select_db($database, $mysqlConnection);
}
?>
</body>

Go to your MySQL Prompt and issue this command.
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root#'31.170.160.80' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
Don't forget to assign your parameters !

Related

need help traying to access my database with mysql

Just built my first database to test out learning skills using the simple query below:
<?php
mysqli_connect("xxx","yyy","zzz" , "newtone" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
This is what I got
:(42000/1044): Access denied for user 'yyy'#'xxx' to database 'newtone'......
I don't get it. So When i Use this:
mysqli_connect("xxx","yyy","zzz" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
I don't get an error message, just a blank page (notice how the database name is not included on the login argument). What am I doing wrong?
You want to connect with database then need three things for create connection dbhost username,dbhost password and your database name.
<?php
$dbhost = "localhost";
$dbuser = "root"; //In case of localhost (default db username)
$dbpass = ""; //In case of localhost
$dbname = "testDb";//Your database name
$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname );
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Your 2nd example works because you are successfully connecting to the MySql server, and defaulting to a default database other than "newtone"
If you tried a command like mysqli::select_db after connecting, you would see the same error. http://php.net/manual/en/mysqli.select-db.php
The crux of your problem seems to be permissions for your user yyy. Make sure user yyy has various permissions like read/write on the DB "newtone"

PHP/SQL noob, all users seem to connect to DB?

I'm trouble connecting to a db. I'm on a Mac, OSX, XAMPP latest version. I haven't messed with the users and privileges settings in PHP MyAdmin so everything is as it came with the package.
Here's what I'm doing:
created a testDB via PHP MyAdmin
created a "users" table inside it, 5 columns, ID, user, paass, name, surname
inserted 2 rows, Alex and Billy, via MyAdmin
trying to connect via PHP to the database
With this code:
$conn_error = 'Could not connect.';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die($conn_error);
echo "Connected to database.";
This returns "Connected to database." However, if I change the user from 'root' to 'anythingElse' - it still connects?! Also, 'OR die()' part of the code doesn't seem to do anything i.e. it doesn't kill the rest of the page if I input wrong user or pass.
What am I doin wrong? Thanks!
try this ..Hope it helps
<?php
$con=mysqli_connect("localhost","root","");
mysqli_select_db("yourdb name",$con);
// Check connection
if ($con)
{
echo "connected to database";
}
else
{
echo "not connected to database";
}
mysqli_close($con);
?>

mssql_query returns "Query failed"

Can't seem to find any solution to this, no matter how hard I google. I have a deadline tomorrow and need this to be resolved asap, hope that someone here can give me a hint :)
Basically I'm connecting to a MSSQL-server from PHP. Like so:
$db_server = 'servername';
$db_user = 'user';
$db_pwd = 'pwd';
$db_db = 'dbname';
$link = mssql_connect($db_server, $db_user, $db_pwd);
if (!$link || !mssql_select_db($db_db, $link)) {
die('Something went wrong while connecting to MSSQL');
}
After that I try to run this code:
mssql_query("CREATE TABLE ##TempUserTable (UserID VARCHAR(30))", $link);
And then I get this error:
Warning: mssql_query() [function.mssql-query]: Query failed in /var/www/test.php on line 4
And yes, I've fixed the serverstuff in freetds.conf and it should be able to create a temporary table. It worked before and it's working from Eclipse / SQLExplorer.
I'm getting frustrated!
Thanks in advance! :)

why can't connect to the mysql

one: i using this code to connect mysql databse which on a shared host.
<?php
mysql_connect("localhost", "user", "123") or die(mysql_error());
echo "Connected to MySQL<br />";
?>;
the user and password are created by the cpanel.which are the database's user and password.
two: i use this line mysql -hlocalhost -uuser -p123 in my window operation system MS-DOS window. but it still can't connect the mysql. why?
I think you need to declare a variable. ie $link = mysql_connect("...")...
or you might want to specify which db you want to connect to, the db name.
http://php.net/manual/en/function.mysql-connect.php
mysqli_connect("host_parameter", "user_parameter", "password_parameter, "database_parameter");
$link= mysqli_connect("localhost","root","root_password","my_database");
if(empty($link)){
die("Error:" . mysqli_error());
}
if it's your first time ( mysql user_parameter = root and password = "")
HOPE IT HELP U

connecting to phpMyAdmin database with PHP/MySQL

I've made a database using phpMyAdmin , now I want to make a register form for my site where peaple can register .I know how to work with input tags in HTML and I know how to insert data into a database but my problem is that I don't know how I can connect to the database that is already made in phpMyAdmin.
The database is a MySQL database, not a phpMyAdmin database. phpMyAdmin is only PHP code that connects to the DB.
mysql_connect('localhost', 'username', 'password') or die (mysql_error());
mysql_select_database('db_name') or die (mysql_error());
// now you are connected
Connect to MySQL
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'username';
/*** mysql password ***/
$password = 'password';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Also mysqli_connect() function to open a new connection to the MySQL server.
<?php
// Create connection
$con=mysqli_connect(host,username,password,dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Set up a user, a host the user is allowed to talk to MySQL by using (e.g. localhost), grant that user adequate permissions to do what they need with the database .. and presto.
The user will need basic CRUD privileges to start, that's sufficient to store data received from a form. The rest of the permissions are self explanatory, i.e. permission to alter tables, etc. Give the user no more, no less power than it needs to do its work.
This (mysql_connect, mysql_...) extension 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. (ref: http://php.net/manual/en/function.mysql-connect.php)
Object Oriented:
$mysqli = new mysqli("host", "user", "password");
$mysqli->select_db("db");
Procedural:
$link = mysqli_connect("host","user","password") or die(mysqli_error($link));
mysqli_select_db($link, "db");
$db = new mysqli('Server_Name', 'Name', 'password', 'database_name');

Categories