MySQL:Access denied for user - php

I want to create a login system of my website,so I read this page
http://www.codingcage.com/2015/01/user-registration-and-login-script-using-php-mysql.html
and also I create a MySql database
and I got this message:"oops database selection problem ! -->
Access denied for user 'a9891486_UsersID'#'10.1.1.31' to database 'dbtest'"
and I know it is some thing wrong in my dbconnect.php
dbconnect.php code:
<?
if(!mysql_connect("mysql9.000webhost.com","Username","Password","dbtest"))
}
die('oops connection problem ! --> '.mysql_error());
}
if(!mysql_select_db("dbtest"))
}
die('oops database selection problem ! --> '.mysql_error());
}
?>
and I know what is "Username" and "Password"
THANK A LOT!!!!!!!!!!!!!!!!!!!

Try this:
<?
$conn = mysql_connect("mysql9.000webhost.com", "username", "password") or die(mysql_error());
//to select the targeted database
mysql_select_db("dbtest", $conn) or die(mysql_error());
?>

Just a couple of points about the code
mysql_connect accepts only 3 parameters the dbname goes in the mysql_select_db()
your if statements have incorrect bracketting
its always safer to use <?php rather than <?
Suggested code changes
<?php
if(!mysql_connect("mysql9.000webhost.com", "Username", "Password"))
{
die('oops connection problem ! --> '.mysql_error());
}
if(!mysql_select_db("dbtest"))
{
die('oops database selection problem ! --> '.mysql_error());
}
?>
But again I have to say
Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7)
Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions,
and here is some help to decide which to use

Related

select database using mysqli in config.php file

I have a nice free for searching a database. It was written with PHP and MySql...however for whatever reason, I occasionally need to add an "i" at the end of MySql occasionally to get things working. I can connect and login, but not select the database, since it passes through until I get the
"request "Unable to select database."
Here's the meat from the config.php file that probably has the issue :
$SETTINGS["hostname"]='localhost';
$SETTINGS["mysql_user"]='root';
$SETTINGS["mysql_pass"]='root';
$SETTINGS["mysql_database"]='myDB';
$SETTINGS["data_table"]='data'; // this is the default database name that we used
/* Connect to MySQL */
if (!isset($install) or $install != '1') {
$connection = mysqli_connect($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"]) or die ('Unable to connect to MySQL server.<br ><br >Please make sure your MySQL login details are correct.');
$db = mysqli_select_db($SETTINGS["mysql_database"], $connection) or die ('request "Unable to select database."');
};
?>
The problem you are facing is the line in which you select the database;
$db = mysqli_select_db($SETTINGS["mysql_database"], $connection) or die ('request "Unable to select database."');
As defined by the documentation of mysqli_select_db() the connection $connection ($link in the docs) should be the first argument:
$db = mysqli_select_db($connection, $SETTINGS["mysql_database"]);
The reason why you need to occasionally add an i to every mysql_* function is because all mysql_* functions are officially deprecated, no longer maintained and removed in PHP 7.0.0. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.
With mysqli, you can select your database directly using the connection method :
$connection = mysqli_connect($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"], $SETTINGS["mysql_database"]);
If you want to use mysqli_select_db, you need to reverse your arguments. First the connection, then the database :
mysqli_select_db($connection, $SETTINGS["mysql_database"]);

frequent lost of connection to mySQL server

I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?
Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here

How to close database connection in php while database file is included?

I have a page say pae1.php has code like
include_once('db.php');
in db.php the connection is established
$con= mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
I then simply run a query on page1.php as
$delete_s1ql="DELETE FROM tbl_info WHERE id='$id' ";
$res_del=mysql_query($delete_s1ql) or die(mysql_error());
Now I want to close my database connection on page1.php.
How can I do this?
Change db.php into this (to make sure the selected database is bound to the resource):
$con= mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dbname", $con) or die(mysql_error());
In page1.php add this to the end (use the global resource-variable to close the correct connection):
mysql_close($con);
PS: take a look at PDO for a better way to connect and talk to your database.
Try this one
mysql_close($con);
MySQL 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.
you can simply use mysql_close($con); in pae1.php since include_once also includes the variables defined in db.php
Use PDO... Basically, try using PHP libraries for infrastructure purposes like DB

Could anyone with MySQLi experience help me with this short SELECT code?

I have researched my question but there just isn't much help out there for MySQLI functions. I'm also new here so, please bear with me.
I have a test database named "website" and it contains a table called "users" which has data for all of the members of my website. In a separate file I have the php code that connects to my localhost and selects the database "website." I'm using the newest functions of MySQL so instead of the connection string containing mysql_connect it contains mysqli_connect. The registration process works great and I can find no error in any of the other code that uses the mysqli functions.
My include file (connect.php)
<?php
$link = mysqli_connect("localhost", "useracc", "useracc1", "website");
?>
I set up a test script for the login section.
<?php
include('connect.php');
$user = $_POST['user'];
$query = "SELECT birthday FROM users";
$result = mysqli_query($link, $query) or die ("RESULT ERROR");
echo "$user";
echo "$result";
?>
There is a password box on the previous page but for the test script I left that out and so far, only the $user is shown on the page. I get no error even when I replace "RESULT ERROR" with mysqli_error($link). I've tried interchanging ' for " in every part of the code that uses quotes but that didn't work. I've tried rewording the $query line but that also had no effect. I'm really new to MySQL and php so if you guys could tell me where I'm going wrong I'd really appreciate it.
Thanks for reading my question. Have a great day.
Add the following lines to the connect.php after you connect line
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
this will ensure the connection is made without any errors and return them if there are ...

Correct Procedure for mysqlConnect

I am new to php syntax and am looking for advice on creating the most acceptable or correct code. I focus on front end design, but I like to make sure my code is proper. I am in a digital media program, my instructor has given us this code for connecting to our MYSQL databases.
<?php
mysql_connect("localhost", "root", "root")or die("Cannot Connect to DB");
mysql_select_db("Example")or die("cannot select the DB Example ");
?>
However when I look at connect scripts online they set the mysql_connect function as a variable lets say $connect and run an if statement stating; if not $connect produce error, and the same for mysql_select_db. They also close the script with mysql_close($connect); like below
<?php
$connect = mysql_connect("localhost", "root", "root");
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("Example", $connect);
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysql_error());
}
mysql_close($connect);
?>
Is either one better? What problems can I have if I dont close the connect with mysql_close?
Regarding using an if instead of an or, it doesn't matter. The or short-circuits, so if the connection worked, die(...) won't be executed. Using either is a matter of preference. If you want to use the or version while keeping the result of the mysql_connect() call, simply assign the whole expression to a variable:
$connection = mysql_connect(...) or die('Connection failed.');
mysql_close() should be used after you're done with all your database communication.
The other mysql_*() functions will use the connection created by the latest call to mysql_connect() or mysql_pconnect(). If for some reason you want more than one connection, trusting the implicit connection object in this manner will fail. Better is to store the connection object yourself and passing it in wherever you need it.
Also before starting to work with databases, you should be aware that mysql is not secure anymore and is deprecated, you should use mysqli. You can use it also as object oriented language.
more details : http://www.php.net/manual/en/book.mysqli.php
example :
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Categories