I want to create a single connection page that i could configure this page once each time and all of the pages that needs connection to mysql uses this page? like wordpress that it has just one configuration page.
if yes how can i do that ?
<php
$connection = new mysqli ('SERVER','DB_USERNAME','DB_PASS','DB');
$connection -> set_charset('UTF-8');
if ($connection->connect_errno)
{
printf ('Connect failed: %s\n', $connection->connect_error);
exit();
}
That's all you need in your connection.php. You can include it with
include('connection.php');
You can find many tutorials for mysqli!
You can simply make a connetion.php and in that page put your connection functions:
$connection = mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
//and any other config
then you can easily do this in anypage you want:
require_once("connection.php")
or
include("connection.php");
Related
I tried connecting the database in order to create chat by using:
https://youtu.be/k8DhWcdKanM
I would like to know what do I need to change in this code in order to connect it to db:
<?php
include 'dbh.php';
$uname= $_POST['username'];
$email=$_POST['email'];
$pass=$_POST['password'];
$sql="insert into signup(username,email,password)
values ('$username','$email','$password')";
$result=$conn->query($sql);
header("Location:index.php");
?>
Thank you.
Here is Connection Code.
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Did you check your variables names inside "values( )".
I believe it should be:
values ('$uname','$email','$pass')";
If you are trying to connect the DB from Another File you have to follow these steps
Step 1: db.php (Will provide two connectivity methods)
$host="localhost";
$user="phpmyadmin1";
$password="123456";// This can be empty in some cases
$db="XXX"; // XXX - DB Name
// This is First type of connection with returning
$con_1 = mysqli_connect($host,$user,$password,$db); // Here the $con will be acting as the connection variable
// This is Second type of connection with returing
$con_2 = new mysqli($host,$user, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $con_2; // This will return the connection variable to necessary pages that are needed.
Step 2: signup.php
In this we have to include the db.php file and then proceed with the connections.
<?php
include('db.php');
$uname= $_POST['username'];
$email=$_POST['email'];
$pass=mysqli_real_escape_string($con,$_POST['password']);
$sql="INSERT INTO `signup`(username,email,password) VALUES ('".$uname."','".$email."','".$pass."')";
$result=mysqli_query($con,$sql); // Here we have to add the connection variable
header("Location:index.php"); // Used for Header Relocation
?>
In the insert statement some of the variables like $uname,$pass are wrong. Hence the Query will fail and the insert operation will not be done. So kindly double check the variables and then proceed with the coding.
Hope this might be helpful for you. Thanks.
Try this code:
$conn = new mysqli($servername, $username, $password, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
return $conn;
everyone, here is the code, which I found from a youtube tutorial. I'm trying to make a login system for my website. In the second line, I should put my host name instead localhost, but i'm not sure what's my hostname. Can anyone help?? thanks!
<?php
$connection = mysqli_connect('localhost', 'rr_net_testuser', 'XXX');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'rarerock_net_test');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
I have some problem
code line
$servername="us--------03.cleardb.net";
$user="be9-------0";
$pass="f9-------";
$db="ad_50f-------fa6a";
$con = mysql_connect($servername, $user, $pass, $db);
mysql_select_db($db);
My Requirement
I implement this line code could not be working than how should be connect database in ibm bluemix!!
plz given me solution..
mysql_connect is deprecated.
Use this code as an example:
$mysqli = new mysqli($mysql_server_name, $mysql_username, $mysql_password, $mysql_database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
die();
}
Your composer.json needs to have:
{
"require": {
"ext-mysqli": "*"
}
}
https://github.com/IBM-Bluemix/php-mysql See: db.php and composer.json
To view the logs, use this command:
cf logs <yourappname> --recent
<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>
I'm trying to connect to a mysql database with php and myadmin. I've tried a lot of codes I could find online, but I just can't put this thing to work...
Can anyone please tell me what I might be doing wrong?
this is the php script I am using:
<?php
$useremail = $_POST["useremail"];
$password = $_POST["password"];
if($useremail && $password){
// open database
$connect = mysql_connect("localhost", "carlos", "nenem");
if(!$connect){
die ("Not able to connected to the database: " .mysql_error());
}
// select database
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$connect_db){
die("Not able to connect to the database: " .mysql_error());
}
mysql_close($connect);
} else {
die("Please enter useremail and password, or REGISTER if you are a new user!");
}
?>
Carefull, $select_db != $connect_db
The variable names are different, rewrite to:
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$select_db){
die("Not able to connect to the database: " .mysql_error());
}
note that you are closing your MySQL connection regardless of anything else before you ever use it for anything..
mysql_close($connect);
You likely want to separate your connection logic from your login logic. You likely need the DB connection to validate the password no matter what and your DB connection should be the first thing you do to make sure you can even DO anything with a given page...