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);
?>
Related
I'm relatively new to any type of programming or coding. I'm not quite understanding why no matter what adjustments I make to my php file I can't seem to pull any data from a table.
Here is a link to the table: https://i.gyazo.com/4ad5e860895014c49dbe0539c38cdec2.png
Above is the test table I have been trying to use. From what I can understand I'm connecting to the database okay, but all of my problems come after the connection. Also, I'm using php 7.0 so a lot of the information I'm finding online has not been helpful.
If there is something glaringly wrong with my table or in my code, please let me know.
Here is my code:
'''
//Set Variables
$serverName = "localhost";
$userName = "root";
$password = "";
$databaseName = "test";
//Create Connection
$connection = mysqli_connect($serverName,$userName,$password,$databaseName);
//Check Connection
if(!$connection){
die("Connection failed: ".mysqli_connect_error());
}
echo "Connected successfully <br>";
//Fetch Data
$query = "SELECT * from table1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ($row[1], $row[2]);
mysqli_free_result($result);
mysqli_close($connection);
I figured out the issue a few hours ago. The code I had posted would have worked perfectly fine if I was connecting to the port for MySQL rather than MariaDB. Didn't realize that the port that MariaDB was connected to was the default.
MariaDB by default was port 3306, but MySQL was 3308. After specifying 'localhost:3308' I was able to start properly pulling rows from my tables.
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"
Forgive me if this is a dumb question but this is really frustrating.
Im having issues trying to connect to my MySQL database. The details are correct as i can login via phpmyadmin and terminal, the database was created with the user whose details i used in this script but i keep getting 'Cannot connect to database' Its been bugging me and frustrating me for hours now lol.
<?php
$host = "localhost";
$username = "root";
$pass = "password";
$name = "database";
$connect = mysqli_connect($host,$user,$pass);
if ($connect){
$select = mysqli_select_db($name);
if ($select){
echo "Connected successfully.";
} else {
echo "Could not connect to database";
}
} else {
echo "Could not connect to MySQL";
}
?>
The script this is for is working but its not progressing because it cant insert the information to the database to continue with the rest of the script.
I have looked at other posts on here and none of the solutions provided worked, so i am posting this.
Please help!
you are missing a parameter in mysqli_select_db it takes 2 parameters like this mysqli_select_db($connect,$name);
http://php.net/manual/en/mysqli.select-db.php
it can't work because it's not correct drop the little (i) of mysqli_connect
and make sure details are correct and you have a database named "database"
and try again
googluck
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
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');