in dbconect
<?php
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
echo " can't connect : " . mysqli_connect_error();
exit;
}
?>
and in dbconfig
<?php
define('DB_HOST', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_NAME', "carwash");
define('DB_PREFIX', "carwash_");
?>
and output
Warning: mysqli_connect() [function.mysqli-connect]: (42000/1049): Unknown database 'carwash' in C:\xampp\htdocs\carwash\includes\dbconnect.php on line 2
can't connect : Unknown database 'carwash'
I don't know what's wrong or what's happened, it happen only today but the day pass it didn't happen
Make sure you have a database named carwash.
Xampp comes with phpmyadmin,have a look for the databases you have there.
Related
so I've been trying to get this working for a few days now and I am completely lost. I've tried following a few of the answers on here but I can't seem to figure out what I need to change to adapt it to my database.
There have been complex and simple solutions I have seen, but what I am attempting at the minute is this:
...
<?php
echo "";
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_NAME', 'username_test');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die('Could not connect to MySQL: ' . mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');
?>
<?php
public function get_data()
{
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
$sql="SELECT Staff_Surname, Staff_Forename FROM users";
$result=$mysqli->query($sql) or die($mysqli->error);
while ($row=$result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value=\"".$row["Staff_Surname"]."\" selected>".$row["Staff_Forename"]."</option>";
}
}
?>
<html>
<body>
<select name="abc" id="xyz">
<?php get_data(); ?>
</select>
</body>
</html>
...
which just results in a bunch of errors when I try to run it in a browser. I am using Microsoft Expression Web 4 and XAMPP.
This is a screenshot of the SQL database I just need to pull the forename and surname into a dropdown box:
SQL database
Any help you guys can offer would be really appreciated. Thank you.
Sorry, in the preview it looked like it showed what the error was. When I try to run the code in a browser I get this: enter image description here
Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in index.php on line 17
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES) in index.php on line 17
Warning: mysqli::query(): Couldn't fetch mysqli in index.php on line 19
Warning: get_data(): Couldn't fetch mysqli in index.php on line 19
Change
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
to
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file in index.php on line 15
Change
public function get_data()
to
function get_data()
These changes should take care of all the errors you are receiving.
Code with #stealthyninja edits:
<?php
echo "";
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_NAME', 'username_test');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die('Could not connect to MySQL: ' . mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');
?>
<?php
function get_data()
{
$mysqli= new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die("Couldn't connect".mysqli_connect_error());
$sql= "SELECT Staff_Surname, Staff_Forename FROM users";
$result= $mysqli->query($sql) or die($mysqli->error);
while ($row=$result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value=\"".$row["Staff_Surname"]."\"selected>".$row["Staff_Forename"]."</option>";
}
}
?>
<html>
<body>
<select name="abc" id="xyz">
<?php get_data(); ?>
</select>
</body>
</html>
EDIT:
Issues connecting to MySQL. First the usernames and passwords are
here: login info
The XAMPP control panel: XAMPP
The testcode that worked previously: Registration
And the error it now presents: Registration Error
I don't think I have changed anything from when it worked before. I built the Registration page following a tutorial in the book "PHP and MySQL for Dynamic Web Sites fifth edition" by Larry Ullman.
I recently installed php7.4-fpm (FPM/FastCGI). But I'm not able to connect to MySQL database.
I'm using the following configuration. Note my database has no password. Tried 'localhost', '127.0.0.1', 'http://127.0.0.1:3306', '127.0.0.1:3306', in DB_SERVER setting but no luck.
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', '127.0.0.1');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'republic');
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($link === false){
die("Oops! Something went wrong. Please try again later.");
}
?>
Php Confiq file
<?php
define('DB_SERVER','127.0.0.1:3308');
define('DB_USER','root');
define('DB_PASS' ,'*****');
define('DB_NAME','table_license');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Error
Error Message
Using HeidiSQL- in Localhost database connection was successful but the server errors occurred?
Try "localhost" instead of "127...." for DB SERVER
Or try to connect without port, just ip
When defining the port for your mysqli connection, you have to use the fifth parameter (and not specify it in the host).
Also note that the default port is 3306, so if you're connecting to a shared host, its most likely on the default port (and not on 3308) - if that's the case, then you can omit specifying the port entirely.
<?php
define('DB_SERVER', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', '*****');
define('DB_NAME', 'table_license');
define('DB_PORT', 3308);
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME, DB_PORT);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli::connect documentation
You can try use:
define('DB_SERVER', '0.0.0.0');
Or
define('DB_SERVER', 'db-container'); // if use the docker
I am building a simple DataBase but i can't get a connection from apache to MySQL
Here is my code :
<?php
DEFINE ("DB_USER", "root");
DEFINE ("DB_PASS", "1234ninja");
DEFINE ("DB_HOST", "localhost");
DEFINE ("DB_NAME", "test3");
$mysql = #mysqli_connect("DB_HOST", "DB_USER", "DB_PASS", "DB_NAME")
OR die("Unable to connect ". mysqli_connect_error());
echo "Great!";
?>
Error message: Unable to connect php_network_getaddresses:
getaddrinfo failed:
Change this mysqli_connect line to: (Remove quotes around constants)
$mysql = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) OR die("Unable to connect ". mysqli_connect_error());
Remember: Using constants with quotes will let constants be printed as string instead of replacing the values.
I have a question that need all of you to help me. I got stuck with it several days. My problem is related to the connection to database using php. Both Mysqli and Mysql are enabled in phpmyadmin. I can connect to database through Mysqli, but i cannot connect through Mysql.
Here is code
Mysqli
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
define('PORT', '80');
define('SOCKET', '/var/lib/mysql/mysql.sock');
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE, PORT, SOCKET) or die("Error " . mysqli_error($connection));
mysqli_set_charset($connection, "utf8");
?>
Mysql
<?php
define('DB_SERVER', 'localhost:/var/lib/mysql/mysql.sock');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die("Error " . mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_set_charset($connection, "utf8");
?>
Can you help me to solve this problem?
Thank in advance!
which type of error you see???
try to use instead of
define('DB_SERVER', 'localhost');
define('DB_SERVER', 'ipOfTheHost');
// data db
$username = "username";
$pass = "pwd";
$database = "nameDB";
$host = "ipServer"; //my is 62.149.150.187
// connection
mysql_connect($host, $username, $pass);
#mysql_select_db($database) or die("Argh... it's impossible to connect to it");