need help traying to access my database with mysql - php

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"

Related

how to set mysql database connection name

I'm trying to figure out how can I put two differents MYSQL database connections name on the same page, one is for local PC and one is for hosting server.
Does is possible to have two different databases servers name on the same page so that way not to change connection database name in a script before upload.
I have code like this for local PC
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
Can can i add another connection name in the server in same page without changing the connection database!
alidad
Yes, a single page may query as many different servers as needed:
<?php
$server1 = new mysqli("server1.example.com", "user1", "password1", "database1");
$server2 = new mysqli("server2.example.com", "user2", "password2", "database2");
$result = $server1->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
$result = $server2->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
The way is to have a config.ini file in your localhost, and a different one in your production environment. This way you will parse this file and get your credentials.
A better way to do that is using this library: https://github.com/vlucas/phpdotenv
It works basically on the same way, but is easy to maintain.

Error message: Database Query failed

For some reason, the following code inside the query works in my MySQL command console, yet when I try to run it as a Query in PHP, something keeps going wrong and I'm not sure what. Here is the code I've done so far.
//2. Perform database query
$query = "SELECT skills.element_id, content_model_reference.element_id, element_name FROM skills, content_model_reference WHERE (skills.element_id = content_model_reference.element_id)";
$result = mysql_query($query);
//Tests if there was a query error
if(!$result){
die("Database query failed.");
}
Is there something preventing the code that worked in MySQL (The line with SELECT) from working, or is my syntax somehow wrong?
EDIT: So it's saying I didn't select a database. Yet I thought I had. Here is the code above it:
//1. Create a database connection
$dbhost = "host"; //Host: Can be either an IP address, or a domain (like google.com).
$dbuser = "user";//User: The user that is connecting to the database.
$dbpass = "pass";//Password: This is the password that the user is using.
$dbname = "db";//Name: This is the name of the database.
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);//The value, 'handle,' is the connection.
//Test if connection occurred. Die ends the program/php, and in this case, also prints a message
if(mysqli_connect_errno()){
die("Database connection failed: ".
mysqli_connect_error().
" (". mysqli_connect_errno() . ")"
);
}
Like I said, the error message I am getting is pertaining only to the query, the server is fine with my database connection.
You're using mysqli_* for the connection, but you're using mysql_* for the QUERY... don't think you can do that, has to be one or the other (MYSQLI_ preffered). Also the query should be:
$result = mysqli_query($connection,$query);

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

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