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
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"
I am trying to connect to a simple database on XAMPP using php- I know the database exists as I can see it on PHPMyAdmin and have created a table called students and added some data.
I have tested that I can run a simple test.php file ( from the htdocs folder on the XAMPP drive) and get a response. I cannot spot what is stopping me connecting to my database- can anyone help?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name ="localhost";
$con=mysql_connect($host_name,$user_name,$password);
mysql_select_db($database);
//check connection
echo "Connection opened";
mysql_close($con);
?>
Could you please try the following code if it works?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name = "localhost";
$con = mysqli_connect($host_name ,$user_name ,$password,$database) or die("Error " . mysqli_error($con));
//check connection
echo "Connection opened";
mysql_close($con);
?>
mysql commands are not going to be supported in future releases, so it would be best perhaps to use mysqli or PDO connections.
Also PDO uses parameters (the syntax might take a bit to make sense), so it is great to reduce risk from SQL Injections.
Mysqli: http://php.net/manual/en/function.mysqli-connect.php
PDO: http://php.net/manual/en/class.pdo.php
The code above should work. Maybe try mysql_select_db($database, $con);
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);
?>
I am a complete database newbie. So far, I know that I can connect to MySQL using PHP's mysql_connect() command, but apart from that, I really don't see how to take that data and put it onto a web page.
a) are there ways other than mysql_connect()
b) lets say I had a table of data in mysql and all I wanted was for that table (for example: list of names and telephone numbers) to now appear on my web page. I can't for the life of me find a tutorial for this.
<?
$database_name = "dbname";
$mysql_host = "localhost"; //almost always 'localhost'
$database_user = "dbuser";
$database_pwd = "dbpass";
$dbc = mysql_connect($mysql_host, $database_user, $database_pwd);
if(!$dbc)
{
die("We are currently experiencing very heavy traffic to our site, please be patient and try again shortly.");
}
$db = mysql_select_db($database_name);
if(!$db)
{
die("Failed to connect to database - check your database name.");
}
$sql = "SELECT * FROM `table` WHERE `field`= 'value'";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
// code here
// access variables like the following:
echo $row['field'].'<br />';
echo $row['field2'];
}
?>
Check out mysql_fetch_assoc mysql_fetch_array and mysql_fetch_object
This is the very basics, you will want to search for tutorials. There are many about.