For Instance consider this standard database connection php file.
db_conn.php
<?php
$servername = "localhost";
$username = "root";
$password = "Yash123";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
If connected successfully it is shows that result onto the website.
What I'd need it to do is not show up on the html but be there in the file so I can test the file executing in the CLI(Command Line Interface) of PHP.
I am using require_once(); in the index file.
uncomment the echo line. or use php's error_log('Connected Successfully');. this would log that the connection was successful. This would hide output from your html and log the string passed as parameter to your error_log file
One could easily dispense with the echo statement and instead return the connection object upon success. This would entail revising the OP code so that it becomes the contents of a database connect function. This idea I gleaned from binpress.com and include suggestions from the Manual, too:
<?php
/* Assuming that user name,password and database name are
credentials that come from a file outside of the
document root for security. */
function db_connect() {
static $conn;
$servername = "localhost";
if ( !isset( $conn ) ) {
// load and parse file containing credentials:
$config = parse_ini_file('../config.ini');
// Create connection
$conn = new mysqli( $servername, $config['username'],$config['password'],
$config['dbname']);
// Check connection
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') '
. $conn->connect_error);
}
// Connected successfully
return $conn;
}
Related
I am getting SNAT Port issue. I don't know what to do I am using PHP 7.0 let me show my code
this is my db code
function db_connect() {
$server = 'P:servername'; // this may be an ip address instead
$user = 'username';
$pass = 'pasword';
$database = 'test'; // name of your database
// Create connection
$conn = mysqli_connect($server, $user, $pass, $database);
return $conn;
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
}
I am getting SNAT port issue what should I do?
Using Persistent Connections
If mysqli is used with mysqlnd, when a persistent connection is created it generates a COM_CHANGE_USER (mysql_change_user()) call on the server. This ensures that the re-authentication of the connection takes place.
https://www.php.net/manual/en/mysqlnd.persist.php
I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1
I am new in PHP and would need some explanation. Here is a code where we connect to MySQL with PHP. Can you please explain me where is the statement that makes the connection? I can see only that we define what the value of $conn is, but does it mean execution as well? The other thing is: where do we create the database? I can see that we give the string "CREATE DATABASE myDB" as a value to $sql and we have an if statement, but does the expression ($conn->query($sql) === TRUE) also evaluated? It is strange for me, can somebody please explain it to me?! :) Thanks!
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Here is a simple explanation of which lines do what. If you would like to know specifically what the individual parts of these mean, then please say which ones so they can be further explained to you. Or the correct links pointed to.
I notice that you are using the W3Schools example, as an almost exact copy and paste. Have you installed MySQL on your machine and created a username and password?
<?php
$servername = "localhost"; // This is the location of your server running MySQL
$username = "username"; // This is the username for MySQL
$password = "password"; // This is the password for MySQL
// Create connection
$conn = new mysqli($servername, $username, $password); // This is where you create a connection
// Check connection
if ($conn->connect_error) { // This checks if the connection happened
die("Connection failed: " . $conn->connect_error); // and produces an error message if not
} // otherwise we move on
// Create database
$sql = "CREATE DATABASE myDB"; // This is the SQL query which is sent to the MySQL server
if ($conn->query($sql) === TRUE) { // When the if statement begins here, it executes the query and test if it returns true
echo "Database created successfully"; // If it returns true then here is the message is returns
}
else {
echo "Error creating database: " . $conn->error; // Or if there was error with the query this is returned
}
$conn->close(); // Close the connection when it is no longer in use
?>
Although, your question does not belong here (This place is to help with your coding issues), but I will give you a bit explanation.
PHP reads each line and EXECUTES It. the create connection part opens a new connection using the "new" object and save it a variable ($conn),
($conn->connect_error) checks if the connection was successful with connect_error property. if it was connected, continue, or else through and error and stop.
If connection was successful, then create the database based on connection opened in variable ($conn).
I have a small page that I'm trying to building a journal with using PHP and My_SQL for both practice and recreation. I have a solid understanding of PHP but less so on My_SQL. Based on what I viewed it seemed that I can implement it almost like looking up a function and applying data to the corresponding areas. I tried doing the most basic thing and connect to my localhost server, but I cannot figure out why I am getting a 500 error. What is wrong with my server???
$servername = "http://127.0.0.1/";
$username = "root";
$password = "***";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else echo "Connected successfully";
`
I'm php begginer and i'm trying to connect database using XAMPP.
When i open my file there is not any error about connecting to database, but also there isn't "Connected successfully"; text, there is just blank page.
This is my code:
<html>
<head>
<title></title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "lala2";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($conn);
?>
</body>
</html>
You used a mix between mysqli and mysql ! Here is the exact method name you should use to connect to a database, and its connection status checks :
MYSQLI style :
/* database connection information */
$server = "";
$user = "";
$password = "";
$database = "";
/* error messages */
$messErr_connectionDatabaseFailed = "Error : connection failed. Please try later.";
$link = new mysqli($server, $user, $password, $database);
/* If connection failed */
if (!$link) {
printf($messErr_connectionDatabaseFailed);
printf("<br />");
}
/* If connection successed */
else {
/* everything is ok, go to next part of you algorithm */
}
MYSQL style (depreciated due to performance and security issues) :
/* database connection information */
$server = "";
$user = "";
$password = "";
$database = "";
/* error messages */
$messErr_connectionDatabaseFailed = "Error : connection failed. Please try later.";
$link = mysql_connect($server, $user, $password);
/* if connection failed */
if (!$link) {
printf($messErr_connectionDatabaseFailed);
printf("<br />");
}
else {
/* selecting the database */
mysql_select_db($database, $link);
/* guessing your select db doesn't failed, next part of you algorithm here */
}
Please use the PDO functions to connect to the database instead. It will be easier to scale your app if you do decide to use different database drivers. Also "binding parameters" is considerably easier using PDO. For a quick intro on PDO, please read http://php.net/manual/en/intro.pdo.php
As far as why you are getting a blank page, there could be many reasons for that. You want to look at your log files first. On Windows, check the Event Logs. On Linux, your logs will be on /var/log. Add the following lines at the beginning of your script:
ini_set('display_errors',1);
error_reporting(E_ALL);
Please also look at this thread from StackOverflow: PHP produces a completely white page, no errors, logs, or headers.