简体   繁体   中英

connection to database failed from php file to phpmyadmin?

I am trying to run an old project i had to use it again now. now, previously it worked fine. but now for some reason, its failing to connect to the localhost database of phpmyadmin.

All i want for now is for the connection to work. xampp has both APache and MySQL running on the default ports, so these are good to go. in fact, i know they are because i opened a different project and its able to connect fine.

basically this is the portion of my php file that attempts connection to the database:

 <?php //DataBase if ( !( $database = mysqli_connect( "localhost", "root", "" ) ) ) die( "<p>Connection to DataBase failed!</p>" ); ?>

when i run the file in chrome (using notepad++) i get this error first thing:

Connection to DataBase failed! " ); if ( !mysqli_select_db( $database,"courseregisteration" ) ) die( " Failed to open DataBase!

now of course the database wont open since the connection is not established!

so i have no idea why its not connecting. i dont have a password, so these parameters are the typical default parameters for this to work.

any help please?

try this, but do it on a separate .php connection

<?php
$hostname = "locahost";
$database = "your db";
$username = "root";
$password = "";

$db mysql_pconnect($hostname, $username, $password)or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database,$db);
session_start();

$qry = "YOUR QUERY HERE";
$result = mysql_query($qr);
while($qry = mysql_fetch_array($result))
}
//some code here
{
?>

Try This:

    <?php
 //create connection
 $connect=mysqli_connect('localhost','root','12345','company');

//check connection
 if(mysqli_connect_errno($connect))
 {
    echo 'Failed to connect to database: '.mysqli_connect_error();
}
else
    echo 'Connected Successfully!!';
?>

In the above code we are connecting the .php page to the database.

To create a connection mysqli_connect() method is used. Parameters passed to the mysqli_connect() functions are the hostname, phpmyadmin username, phpmyadmin password and the database name.

This connection string is stored in a variable named $connect. Next we first have to check whether we are connected to database successfully or not. For that we use if—else conditional statement. The function mysqli_connect_errno($connect) checks if there is any error in the connection string. If there is any error, it will return true otherwise false. If there is any error, it will be displayed by the echo statement in if block otherwise Connected Successfully!! Message will be displayed. The function mysqli_connect_error() function is used to display system error message. Just open the browser and run the connect.php. You will get the following output: "Connected Successfully".

Try changing your connection code to an Object Oriented Conn:

<?php
$mysqli = new mysqli("localhost","root","","my_db");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM