connect HTML Form to MySQL Database with PHP - php

I have a block of code to submit my HTML form values to my Database using PHP via XAMPP. After construction and testing the record will saved in database successfully but I see a notice that says below:
Undefined variable: localhost in
C:\xampp\htdocs\30-11-2021\processhtml.php on line 19
Kindly see my line 19 below also:
$conn = new mysql ($localhost, $dbusername, $dbpassword, $dbname);

You want
$conn = new mysql ('localhost', $dbusername, $dbpassword, $dbname);
The first parameter is the hostname of the server running MySQL. In your case it's 'localhost'.
php warns you when you use an undefined variable, and gives the value null for that variable. In this context, a null hostname defaults to localhost, so that's why your code actually works.

Related

How can I create database in SQL inside PHP file

I already create a database in MySQL using
CREATE DATABASE register
And I set
$db= "CREATE DATABASE register";
$host="localhost";
$dbusername="root";
$dbpassword="";
$dbname= "register";
//create database connection
$conn = new mysqli($host,$dbusername,$dbpassword,$dbname);
but some error that becomes error
Warning: mysqli::__construct(): (HY000/1049): Unknown database 'register' in C:\xampp\htdocs\web\html\reg.php on line 19
How can I fix that
You don't have to specify a database:
$conn = new mysqli($host, $dbusername, $dbpassword);
You can then issue your query with $conn->real_query($db) (which is OK because you're just issuing a statement without any user data) and use $conn->select_db to make the database the active db after creating it.
Your assignment in the beginning ($db = ...) doesn't do anything else than assign the value to a variable - the query isn't ran before you actually send it to MySQL.

How to stop PHP MySQLi from displaying warning message if db connection is failed? [duplicate]

This question already has an answer here:
Should we ever check for mysqli_connect() errors manually?
(1 answer)
Closed 2 years ago.
How can we stop PHP MySQLi from displaying ugly warning or error messages if the database connection is failed? We just want to display our own custom message instead.
Here's our code to make connection with db:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$mysqli = new mysqli($hostname, $username, $password, $dbname);
if($mysqli->connect_error)
{
die("Database connection failed: " . $mysqli->connect_error);
}
$mysqli->close();
?>
If the database "db_test" doesn't exist, we get the following error:
( ! ) Warning: mysqli::mysqli(): (HY000/1049): Unknown database 'db_test' in...
We just want to see this:
Database connection failed: Unknown database 'db_test'
What editing do I need to do in my above code?
Note
I know the database db_test doesn't exist. I am deliberately using wrong db just in order to see the warning or error message if any. I just want to see my custom error message if db connection failed (if it also fails in real scenario by any reason too) and not the warning generated from PHP.
EDIT
This warning message is only produced when I use OOP approach in MySQLi, and not with procedural.
A simple approach to hide the warning message produced by PHP and to show only your own custom message is to use suppress operator (#).
Change line from
$mysqli = new mysqli($hostname, $username, $password, $dbname);
to
#$mysqli = new mysqli($hostname, $username, $password, $dbname);

mysqli_connect() causes fatal error - PHP

I have the following lines of code in a script;
include '../details.php';
$conn = false;
//$conn = mysqli_connect($servername, $username, $serverpassword, $dbname);
if(!$conn){
echo "no connection";
//header("location: //www.mattwoolford.co.uk/contact/?err=003");
}
The commenting out and $conn = false; is for debugging, as is the echo statement. Here's the deal:
When I uncomment $conn = mysqli_connect(), the page goes blank from an error. Tried or die() to no avail. Tested echoing the credentials from details.php, and they present themselves correctly and successfully.
What's happening and why?
UPDATE:
Error is shown to occur on a following line:
$service = mysqli_real_escape_string($conn, $service);
"Catchable fatal error: Object of class mysqli could not be converted to string in ("path") on line 81"
UPDATE 2: FIXED
An EOT; was indented, so it was trying to convert all the functions respectively as if it were included within <<<EOT
EOT;
There may be multiple things happen:
Confirm the file path, may be it's not getting the actual file path.
Try to connect with mysql connection setting, May server support earlier mysql version.
Final thing, check for the dbname, username, password and host url.
So there can be only these three possibility, Try to do it.
It should fix.
Thanks

Persistent connection between Android - PHP - Mysql

Hi all I have a question about PHP connection and Android.
I have got some PHP files, in everyone of this file I make a call for connect to the database
new mysqli($servername, $username, $password, $dbname);
and after, I will do a query to extract data from DB and return a JSON to Android into an AsyncTask. I have seen that make everytime a call to
new mysqli($servername, $username, $password, $dbname);
was very slow.
My question is, is there a way for speed up connection to DB or save the connection for the first time, into Android and after pass as parameter to PHP files?
Thanks in advance.
For the servername you can use "p:servername" for example:
new mysqli("p:db.test.com", "user", "pass", "db");
Apache will then create a persistent connection that gets reused.
If you want to use it in the same request you can always save it in a variable like this:
$db = new mysqli("db.test.com", "user", "pass", "db");
and call your functions to the $db variable
Read throught this: http://php.net/manual/en/mysqli.persistconns.php for further information

mySQL connection error using php codes

"Warning: 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:\xampp\htdocs\appraisal\database.php on line 5
No database selected"
Why does it show most of the time? But when I refresh the page by entering F5, the error will be gone and will show the output correctly. This doesn't really affects the system but it is quite bothering when our users see the error.
I have the codes here.
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal');
?>
Hoping for your response. Thanks!
Try adding the $conn parameter to the mysql_select_db function call
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal', $conn);
?>
Link to The Manual
If you are writing new PHP code you should really not be using the mysql_* extension as it has been deprecated i.e. it will be removed from PHP sometime soon.
Use either the mysqli_* extension or the PDO one.

Categories