Undefined variable $db_hostname - php

I'm working with mysql and I'm trying to make a simple login to the database, but whenever I try to do it I get an error
Notice: Undefined variable: db_hostname in D:\xampp\htdocs\index.php on line 3
Notice: Undefined variable: db_username in D:\xampp\htdocs\index.php on line 3
Notice: Undefined variable: db_password in D:\xampp\htdocs\index.php on line 3
here is my code for the login.php
<?php//login.php
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'root';
$db_password = '';
?>
code for index.php
<?php
require_once('login.php') ;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server) die ("Unable to connect to MySQL: " . mysql_error());
?>
I have no idea what I'm doing wrong.

change this (remove commented line or give space before it)
<?php//login.php
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'root';
$db_password = '';
?>
to,
<?php //login.php
$db_hostname = 'localhost';
$db_database = 'publications';
$db_username = 'root';
$db_password = '';
?>

In PHP the <?php directive must be followed by whitespace, newline, or EOF (the file ends after the directive).
You normally get a fatal error and your script stops.
However you're using an outdated and unsupported version of PHP which just passes your "to be" PHP code directly to stdout. This can have security implications, like in your case, you've published the database credentials (if any).

Related

Unknown database error when connecting sql database to server (localhost)

What should I do to solve this error?
Error:
Fatal error: Uncaught mysqli_sql_exception: Unknown database 'testdb' in D:\xampp\htdocs\dashboard\project\connection.php:6 Stack trace: #0 D:\xampp\htdocs\dashboard\project\connection.php(6): mysqli->__construct('localhost', 'root', '#Geoinformatici...', 'testdb') #1 {main} thrown in D:\xampp\htdocs\dashboard\project\connection.php on line 6
Code in Bracket IDE
<?PHP
$server = 'localhost';
$username = 'root';
$password = '#Geoinformaticionno1';
$database = 'testdb';
$connection = new mysqli($server, $username, $password, $database) or die("not connected");
echo "connected"
?>
Let me know if any other thing is required to see to solve this error.
You just have to specify the server port in these brackets.
<?PHP
$server = 'localhost';
$username = 'root';
$password = '#Geoinformaticionno1';
$database = 'testdb';
$connection = new mysqli($server, $username, $password, $database, 3307) or die("not
connected");
echo "connected"
?>
our local system or localhost has defualt user is root & password is `` that means blank.
if, you using add password from your end, for accessing phpmyadmin, then you can add your setted password. But, default, value user is: root & password is blank.

How to clear the error in this code in php?

<?php
$phpContent = '<?php
session_start();
include("../conn.inc.php");
$id = $_SESSION["gameid"];
$select_content = mysql_query("select * from game_details where id=".$id);
$arr_content = mysql_fetch_array($select_content);
echo $arr_content["name"];exit;
?>
';
fwrite($phpFile, $phpContent);
fclose($phpFile);
?>
In this code, I am selecting the datas from the database of a particular value of id stored in $id. The code in $phpContent, I am writing it into a file,it shows this error,when I am opening that written file:
Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean
Can anyone say how to eliminate this error ?
conn.inc.php
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$db_host = "localhost";
$db_user = "root";
$db_name = "pixo";
$db_pwd = "mysql";
$connection=mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
Your issues with mysql_select_db you have forgot to pass $connection as second parameter please change your code with below code.
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$db_host = "localhost";
$db_user = "root";
$db_name = "pixo";
$db_pwd = "mysql";
$connection=mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name,$connection);
?>
More About mysql_select_db
But my advice is now days mysql_select_db is not secure please go with MySQLi Functions More About Mysqli
Hope above code will helps you. you forgot pass $connection parameter.
It seems like your mysql requête is returning FALSE, var_dump your $select_content var to see what it is returning exactly.

Wamp, PHP MySQL - Connecting error

I've installed a WAMP server. Now I want to connect to a MySQL database using php. I have one problem you need to get the username.
This is the code I use. Its from php.net http://php.net/manual/en/mysqli.quickstart.connections.php
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "password";
$db_name = "test";
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
I get the following error:
Warning: mysqli::mysqli(): in C:\wamp\www\Atletieker\dbconnect.php on line 7
Failed to connect to MySQL: (1045) Accès refusé pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
Warning: main(): Couldn't fetch mysqli in C:\wamp\www\Atletieker\dbconnect.php on line 11
How do I get the username of my database?
Try this...
$db_host = "127.0.0.1";
$db_username = "root";
$db_password = "";
$db_name = "test";
Obviously the $db_password should be empty, if you haven't changed any settings the default password should be blank.

Not able to connect to mysql php/xampp

my I am trying to make login registration page in php.
apache and mysql is up and running.
I am executing the following code
<?php
$db_host = "localhost:777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I am using localhost:777 because port 80 is being used by skype.
phpMyAdmin is up and running.
The output i am getting is nothing but the above code only.
can anyone help me on this issue?
Use MySQLi or PDO not MySQL.
You're passing the variables as a string using " " in the mysql_connect function
The syntax is as follows: mysqli_connect(host,username,password,dbname,port,socket);
<?php
$db_host = "localhost";
$db_port = "777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
$conn = mysqli_connect($db_host,$db_username,$db_pass,$db_name,$db_port);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try that and it should work. Please make sure you understand the code above and the error you made. Do not simply copy and paste please you will not learn from it.

can't connect to mysql database using host,user and pass

I keep getting this whenever I run the below code:
Fatal error: Call to undefined function mysql_connect() in C:\wamp\www\php-academy\113-connect-db.php
<?php
$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);
$mysql_db = 'a_database';
mysql_select_db($mysql_db) or die($conn_error);
echo 'Connected!';
?>
That probably means you don't have the MySQL extension loaded. You can test whether the extension is loaded with extension_loaded("mysql");.
I suspect that this is because the extension is not loaded. I would check your php_info(); to find out.
Once you do your code should work, although I prefer to assign the mysql_connect() to a variable and then call that variable in mysql_select_db() as I have below:
$defHost = 'localhost';
$defUsername = 'username';
$defPassword = 'password';
$defDatabase = 'database_name';
$connect = mysql_connect($defHost, $defUsername, $defPassword) or die();
mysql_select_db($defDatabase, $connect) or die();
The reasons for the error may be the following:
Incorrect access details to server database are specified in configuration file.
There are no read permissions for configuration file. Provide read permissions for the file.

Categories