php page can't load MySQL's database - php

I'm having some problems while trying to connect a .php with mysql.
here's the connection.php code
<?php
$db_host =$_POST['localhost'];
$db_user =$_POST['root'];
$db_password = "";
$db_name = "prtcl";
?>
and this is the page where I actually use the connection
<?
include("connection.php");
?>
...
<?php
$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if (!$db)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($db);
?>
<body>
...
that's what I get when I try to load it ( line 29 is this one:
$db = mysql_connect($db_host,
$db_user, $db_password);
)
Notice: Undefined variable: db_host in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Notice: Undefined variable: db_user in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Notice: Undefined variable:
db_password in C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Warning: mysql_connect()
[function.mysql-connect]: [2002] A
connection attempt failed because the
connected party did not (trying to
connect via tcp://localhost:3306) in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Warning: mysql_connect()
[function.mysql-connect]: A connection
attempt failed because the connected
party did not properly respond after a
period of time, or established
connection failed because connected
host has failed to respond. in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Fatal error: Maximum execution time of
30 seconds exceeded in C:\Program
Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
as you can see I'm using EasyPHP and, since this very code used to work before (with a different db, while using manually configured apache/mysql), may be that the reason? Other infos: I made the db using phpmyadmin and I have win7
thank you

Don't ever take your db credentials from $_POST without validation. This is such a terrible idea and this is what's causing your errors, as these keys are not being defined in $_POST, but they could be and the results could be disastrous!
try putting this in connect.php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "prtcl";
$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if(!$db) {
die('Could not connect: ' . mysql_error());
}
then in your other pages use:
require_once("path/to/connect.php");
// ... whatever else you do on this page...

I would find out why the $db_host variable is not getting defined. That might be a problem with your form input. Check to see that the name of that input field really is 'localhost'. Also, as Mark Baker mentioned above, you really want to do some input cleansing to guard against sql injection attacks. Plus, is there any reason you can't hardcode the db host info into a configuration file?

Related

how to connect to MySQL databases using php in zend server

I am trying to connect to my MySQL version 8.0.11 database in zend server version 7.2.10 using php but I could not connect it
Warning: mysqli::__construct(): Unexpected server respose while doing caching_sha2 auth: 109 in C:\Program Files (x86)\Zend\Apache24\htdocs\connectdatabase.php on line 7
Warning: mysqli::__construct(): MySQL server has gone away in C:\Program Files (x86)\Zend\Apache24\htdocs\connectdatabase.php on line 7
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in C:\Program Files (x86)\Zend\Apache24\htdocs\connectdatabase.php on line 7
Connection failed :MySQL server has gone away
I am getting the following warnings when i try to run my code.
i have searched and tried ALTER USER 'username'#'hostname' IDENTIFIED WITH mysql_native_password BY "userpassword" command but it does not work for me
<?php
$servername = "localhost";
$username = "root";
$password = "hello";
$conn = new mysqli($servername,$username,$password);
if(mysqli_connect_error())
{
die("Connection failed :" . mysqli_connect_error());
}
echo "CONNECTED SUCCESSFULLY";
?>
Try with caching_sha2_password:
ALTER USER 'username'#'hostname'IDENTIFIED WITH caching_sha2_password BY 'userpassword';

mysqli::__construct(): MySQL server has gone away

I can't connect to my server. Here are the mistakes it gives me:
Warning: mysqli::__construct(): MySQL server has gone away in
C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on line 3
Warning: mysqli::__construct(): Error while reading greeting packet.
PID=8632 in C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on
line 3
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone
away in C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on
line 3
Fatal error: Maximum execution time of 30 seconds exceeded in
C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on line 3
This is my connection file:
define("HOST", "localhost:81");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "bulitfactory_person_cv");
$db_connection = new mysqli(HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($db_connection->connect_error) {
die("Connection failed. ".$db_connection->connect_error);
}
Please, help me!
try it to increase max_allowed_packet and restart the server
Add log_warnings=2 within [mysqld]section to your .ini/.cfg file, shutdown restart mysql and you should have maximum connection failure information in your error.log.

How to fix failed to open stream?

So i'm getting this warning and i have no clue how to fix it. Its for my connect file to my database.
Warning: require(includes/connect.inc.php): failed to open stream: No such file or directory in /customers/5/e/6/gixter.dk/httpd.www/index.php on line 4 Fatal error: require(): Failed opening required 'includes/connect.inc.php' (include_path='.:/usr/share/php') in /customers/5/e/6/gixter.dk/httpd.www/index.php on line 4
My connect file look like this:
<?php
$conn_error = 'Cound not connect to database.';
$mysql_host = 'xxxxx';
$mysql_user = 'xxxxx';
$mysql_pass = 'xxxxx';
$mysql_db = 'xxxxx';
if (!#mysql_connect($mysql_host, $mysql_user, $mysql_pass) or !#mysql_select_db($mysql_db)){
die($conn_error);
}
?>
Can anyone help?
The path you specified is incorrect. The file doesn't exists or can not be found in the folder. It might be on a higher level. (../)
The mysql extension is no longer supported, please take a look at the mysql_i extension or PDO. Mysql is deprecated and vulnerable to SQL-injection.
Mysql_i
PDO

How to get the socket for MySQLi to Oracle DB connection?

I am new with web development. I have difficulty in connecting to an oracle DB. This code was patterned from the Oracle documentation.
I am sure with the connection details except the socket.Is the socket on default? or How do I get the socket detail from the Oracle DB interface?
$host="site.com";
$port=1404;
$socket="/tmp/mysql.sock";
$user="admin";
$password="admin123";
$dbname="admin_table";
/* create a connection object which is not connected */
$conn = new mysqli();
$conn -> init();
/* set connection options */
$conn -> options (MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0");
$conn -> options (MYSQLI_OPT_CONNECT_TIMEOUT, 200);
/* connect to MySQL server */
$conn -> real_connect ($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server : ' . $conn -> error . '\n');
I also encounter these errors:
Warning: mysqli::real_connect() [mysqli.real-connect]: MySQL server has gone away in C:\wamp\www\Lot_Verification\pages\DB_Connect.php on line 19
Warning: mysqli::real_connect() [mysqli.real-connect]: Error while reading greeting packet. PID=6572 in C:\wamp\www\Lot_Verification\pages\DB_Connect.php on line 19
Warning: mysqli::real_connect() [mysqli.real-connect]: (HY000/2006): MySQL server has gone away in C:\wamp\www\Lot_Verification\pages\DB_Connect.php on line 19
Fatal error: Maximum execution time of 60 seconds exceeded in C:\wamp\www\Lot_Verification\pages\DB_Connect.php on line 20

Connection issue with mysqli

$db= mysqli_connect('localhost', "root", "root");
if(!$db) die("Error connecting to MySQL database.");
mysqli_select_db("onlineform", $db);
As I try to connect to the database like shown above, I am getting the following error:
Warning: mysqli_connect(): [2002] No such file or directory (trying to connect
via unix:///var/mysql/mysql.sock) in - on line 3 Warning: mysqli_connect():
(HY000/2002): No such file or directory in - on line 3 Error connecting
to MySQL database.
It's my first time using mysqli, I thought it would be a big improvement over mysql. I checked with MAMP if it's enabled and it is.
Any suggestions?
Check if your socket is correct. If not, override the default socket. I guess you have to try something like this in MAMP:
$link = mysql_connect(
':/Applications/MAMP/tmp/mysql/mysql.sock',
'root',
'root'
);

Categories