PHP Connection Error; Not allowed to connect to MySQL Server - php

I am trying to make a registration page where it adds the information to a SQL Table. Here is my PHP code to do this... Can anyone tell me what the error with this is?
<?php
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$DBsql = mysql_query("SELECT username, email from Profiles");
$sql = mysql_fetch_array($DBsql);
if(!($_POST['username'] == $sql['username']) && ($_POST['email'] == $sql['email'])) {
if ($_POST['password'] == $_POST['password2']){
$sql="INSERT INTO Profiles (firstName, lastName, username, email, password, region, group, verified)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[username]','$_POST[email]','$_POST[password]','$_POST[region]','member','no')";
}else{
echo '<script type="text/javascript">'
, 'window.alert("The passwords do not match.")'
, '</script>';
}
} else {
echo '<script type="text/javascript">'
, 'window.alert("That username or email is already in use.")'
, '</script>';
}
mysqli_close($con);
?>
It's giving these errors:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/1130): Host '31.170.161.96' is not allowed to connect to this MySQL server in /home/a5349216/public_html/register.php on line 2
Failed to connect to MySQL: Host 'ip' is not allowed to connect to this MySQL server
Warning: mysql_query() [function.mysql-query]: Access denied for user 'username'#'localhost' (using password: NO) in /home/username/public_html/register.php on line 8
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/username/public_html/register.php on line 8
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/username/public_html/register.php on line 9
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/username/public_html/register.php on line 27

If your mysql database is not on the same server(localhost) you need to give it access from a remote server for that mysql user/database for your server. See this article for some steps that might help you. It sounds like it might be remote.
https://support.rackspace.com/how-to/mysql-connect-to-your-database-remotely/

Create new php file, just test with below syntax. let me know your error.
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Related

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'usr'#'localhost' (using password: YES)

Warning: mysqli_connect(): (HY000/1045): Access denied for user
'usr'#'localhost' (using password: YES) in
/storage/emulated/0/htdocs/includes/condb.php on line 9 Failed to
connect to MySql: Access denied for user 'usr'#'localhost' (using
password: YES) Warning: mysqli_query() expects parameter 1 to be
mysqli, boolean given in /storage/emulated/0/htdocs/includes/condb.php
on line 19
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean
given in /storage/emulated/0/htdocs/includes/condb.php on line 20
Unable to connect
Im trying to connect to the database using this code but im an unable to. I dont know what i actually did wrong. Ive tried googling and reading some articles but none of those work.
UPDATED CODE still getting the error message above.
$server = "127.0.0.1"; $username = "usr"; $password = "123"; $database
= "contents";
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno($conn)) {
echo "Failed to connect to MySql: ".mysqli_connect_error(); }
$sql = "INSERT INTO contents('subj','article','day') VALUES
('".$_POST['This is the subj']."', '".$_POST['This is the
content']."', '".$_POST['2019-02-01 10:15:59']."')";
if(!mysqli_query($conn,$sql)) {
die("Unable to connect".mysqli_error($conn)); }else {
echo "Connected"; }
?>
Still getting the same error above unfortunately it was not a typo error. Still couldn't get the solution to this problem it has been days. Can someone with problem solving skills help me.
You don't have to pass $conn inside mysqli_connect_errno()
remove $conn
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno()) {
echo "Failed to connect to MySql: ".mysqli_connect_error();
}
You can fix this error by changing your mysql username and password. There are many tutorials for changing mysql user and password with a little search

Cron job returns an error message

I'm trying to connect to my database via cron job. However i keep receiving an error messages. After many frustrating hours im posting here for help.
My cron script php file:
<?php
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect('HOST', 'USERNAME', 'PASSWORD','DB_DATABASE');
// Check connection
if (mysqli_connect_errno())
{
"Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check if server is alive
if (mysqli_ping($conn))
{
"Connection is ok!";
}
else
{
"Error: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
This is the error i received:
mysqli_connect(): (HY000/2005): Unknown MySQL server host 'HOST' (0)
mysqli_ping() expects parameter 1 to be mysqli
mysqli_error() expects parameter 1 to be mysqli
mysqli_close() expects parameter 1 to be mysqli
Any help? Thanks!
You're quoting constants, which makes them strings.
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DB_DATABASE);
You should write constants out of quotes as following. Otherwise they are usual strings.
$conn = mysqli_connect(HOST, USERNAME, PASSWORD,DB_DATABASE);

Information from registration form is not submitted into table in phpmyadmin

I do not know why it won't connect I have no ideas. When I try to connect it also tells me it's successfully connected to the mysql its weird please help.
It's been annoying the crap out of me and I really need it to work soon because it is driving me nuts.
<?
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$email = mysql_real_escape_string($_POST['email']);
$checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'");
if(mysql_num_rows($checkusername) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
$checkemail = mysql_query("SELECT * FROM users WHERE email = '".$email."'");
if(mysql_num_rows($checkemail) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that email is taken. Please go back and try again.</p>";
}
}
else
{
$registerquery = mysql_query("INSERT INTO users (Username, Password, EmailAddress) VALUES('".$username."', '".$password."', '".$email."')");
if($registerquery)
{
echo "<h1>Success</h1>";
echo "<p>Your account was successfully created. Please click here to login.</p>";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
}
}
}
else
{
?>
<h1>Register</h1>
<p>Please enter your details below to register.</p>
<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<label for="email">Email Address:</label><input type="text" name="email" id="email" /><br />
<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>
<?php
}
?>
Mysql Connected Successfully
Warning: mysql_real_escape_string(): Access denied for user 'UNKNOWN_USER'#'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 28
Warning: mysql_real_escape_string(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 28
Warning: mysql_real_escape_string(): Access denied for user 'UNKNOWN_USER'#'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 29
Warning: mysql_real_escape_string(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 29
Warning: mysql_real_escape_string(): Access denied for user 'UNKNOWN_USER'#'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 30
Warning: mysql_real_escape_string(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 30
Warning: mysql_query(): Access denied for user 'UNKNOWN_USER'#'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 32
Warning: mysql_query(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 32
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/parap00per/public_html/register.php on line 34
Warning: mysql_query(): Access denied for user 'UNKNOWN_USER'#'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 48
Warning: mysql_query(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 48
Error
Sorry, your registration failed. Please go back and try again.
You're not selecting a database with mysql_select_db().
(as per your original posted code which was edited).
Replace the xxx with your own information.
$host="localhost"; // Host name.
$db_user="xxx"; // MySQL username.
$db_password="xxx"; // MySQL password.
$database="xxx"; // Database name.
$link = mysql_connect($host,$db_user,$db_password);
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t use the DB : ' . mysql_error());
}
You're also closing your connection prematurely using mysql_close($link); place it after you've finished querying, or don't use it at all. Your connection will close anyway once it's finished executing.
Edit:
I noticed you're using EmailAddress and email as column names. One of them or others may not be correct.
Add error reporting to the top of your file(s) which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also, use mysqli_* with prepared statements, or PDO with prepared statements.
Footnotes:
mysql_* functions deprecation notice:
http://www.php.net/manual/en/intro.mysql.php
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

Converting Mysql to Mysqli [duplicate]

This question already has answers here:
mysqli::query(): Couldn't fetch mysqli
(4 answers)
Closed 1 year ago.
I was told to stop using MySQL and now learning MySQLi. However I ran into a problem using fetch array and don't know what I did wrong.
//Connect to db
include "mysqli_connect.php";
// Construct our join query
$sql = "SELECT userID, username, lastlogin FROM users";
//Crate results
$result = mysqli_query($link, $sql);
// Print out the contents of each row into a table
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
// Free result set
mysqli_free_result($result);
// Close connection
mysqli_close($link);
Messages I see:
[Connection Successful] Host info: db413417616.db.1and1.com via
TCP/IP
Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch
mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php
on line 9
Warning: mysqli_fetch_array() expects parameter 1 to be
mysqli_result, null given in
/homepages/9/d413002686/htdocs/maintenance/testsession.php on line 12
Warning: mysqli_free_result() expects parameter 1 to be
mysqli_result, null given in
/homepages/9/d413002686/htdocs/maintenance/testsession.php on line 15
Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch
mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php
on line 18 ()
I used this to for mysqli_connect.php: http://www.php.net/manual/en/mysqli.construct.php
<?php
// $link (host, username, password, database)
$link = mysqli_connect('host', 'username', 'password', 'database');
//If connection is successful, otherwise show error message.
if (!$link) {
die('[Connect Fail] Error: (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
// Echo success message
echo '[Connection Successful] Host info: ' . mysqli_get_host_info($link) . "\n";
// Close the link
mysqli_close($link);
?>
This line in your include file should be removed:
// Close the link
mysqli_close($link);
You're closing $link, a.k.a. your connection to the database, and then trying to use it in your query. You only want to close it when you're all done with it.

mysql_query() Access denied for user

I'm making a PHP and MySQL RPG game, and my changeEquipment page is not working. I'm getting these errors:
Warning: mysql_query() [function.mysql-query]: Access denied for user
'devinfa1'#'localhost' (using password: NO) in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 21
Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 21
Warning: mysql_query() [function.mysql-query]: Access denied for user
'devinfa1'#'localhost' (using password: NO) in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 27
Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 27
Here's my code:
<?php
// Get commonFunctions.php
require_once('commonFunctions.php');
// Start the session and connect to the database
session_start();
$con = mysql_connect("localhost","devinfa1_user","dbpass");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("devinfa1_spellsword", $con);
// If logged in
if(isset($_SESSION['account_name'])) {
// If form is submitted
if(isset($_POST['submit']) || isset($_POST['submit_x'])) {
// If the hero has more than one weapon
if(hero_has_multiple_weapons()) {
// Set new weapon
mysql_query("UPDATE Hero_Weapons SET active_weapon = TRUE WHERE HW_hero_id =
(SELECT hero_id FROM Heroes WHERE hero_name = '$_SESSION[hero_name]')
AND HW_item_id = (SELECT item_id FROM Items WHERE item_name = '$_POST[weapon]')");
// Unset previous weapon
if(isset($_SESSION['hero_name'])) {
mysql_query("UPDATE Hero_Weapons SET active_weapon = FALSE WHERE HW_hero_id =
(SELECT hero_id FROM Heroes WHERE hero_name = '$_SESSION[hero_name]')
AND HW_item_id = (SELECT item_id FROM Items WHERE item_name = '$_SESSION[weapon_name]')");
}
}
// Close the database connection
mysql_close($con);
?>
So, the PHP should be connecting to my databse using the devinfa1_user username and dbpass password, but it's apparently trying to use the devinfa1 username with no password.
I have no idea why this is the case, because I have the exact same connection code and an update statement on my changeHero page and it works perfectly. Any ideas?
Pass the connection in with the query, per the manual : The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
see if that works, it looks as if is overriding the last connection (ie in the call to hero_has_multiple_weapons() a db connection is opened) or your last connection didnt really connect

Categories