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;
Related
I am using 000 webhost to store a sql database.
Every time I want to connect to it I recieve this error message
Warning: mysqli::__construct(): (HY000/2002): Connection refused in /storage/h10/804/1186804/public_html/Handy_Help_PHP_files/conexiune.php on line 7
Connection failed: Connection refused
This is the php I use to connect
<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Can somebody help me to find the error?
with mysqli need add database name too
<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";
// Create connection
$conn = new mysqli($servername, $username, $password,$yourdbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
such:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Remove port from host parameter and put in this order:
mysqli_connect(host,username,password,dbname,port,socket);
I have read a bunch of the other posts about similar issues but I am very new to this so I am likely missing something. Most of the other questions I found were a lot more complicated than mine. I am trying to follow the w3schools tutorial for this and am testing locally using XAMPP. Right now I am just trying to get this to work successfully and eventually I am planning to submit data into a mySQL database from a web form.
<?php
$servername="localhost";
$dbname="mysql";
// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO owner_table (ownerFirst, ownerLast, mobile)
VALUES ('John', 'Doe', '1111111111')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
NOTE:
The connection is not failing from what I can tell. Removing everything after the first if statement, and adding else { echo "success" } outputs success. Again though, I am new to PHP.
If someone knows that the answer has been given in another post PLEASE PLEASE comment here and ill close this out.
http://www.w3schools.com/php/php_mysql_insert.asp
You should follow the proper syntax:
$conn = mysqli_connect(<host Name>, <username>, <password>, <database name>);
Reference: http://php.net/manual/en/function.mysqli-connect.php#refsect1-function.mysqli-connect-examples
Add two more parameters- username and password here:
$username = "root"; // Default values
$password = ""; // Default values
$conn = mysqli_connect($servername, $username, $password, $dbname);
I am building a website for a friend and he would like some statistics on the website. The statistics are on a dedicated MySQL server, and the website is on another. I have been trying to keep linking the database to the website but it keeps failing. I am using PHP to attempt this.
The code I have been trying to use is here:
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
The database name is the issue. Any ideas about this?
mysqli_connect takes 4 arguments, not 3 as you are trying. The fourth one is database name. Documentation is here.
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
Edit: The argument is optional, but if it's is a shared hosting, your credentials are probably working for the one specific database, not the whole server. So specifying the database could help you with this issue.
mysqli_connect needs database name to execute
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
$dbname ="x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
use
$dbname='yourdbname';
mysqli_select_db($conn,$dbname);
to select the database
Hello I am new at php and mysql and I don't know what is wrong.
I cant show the results from query and the connection with mysql is successfully connected.
I don't use wampserver I just install php,mysql and Apache separately.
Thanks in advance.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};
?>
its only show me "query cannot execute"
You need to pass fourth parameter database name in mysqli_connect()
It would be
$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");
Read http://php.net/manual/en/mysqli.error.php to check error in query.
Read http://php.net/manual/en/mysqli-result.fetch-array.php
To fetch data from query result
I am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '#localhost' to database 'userinfo'
userinfo is the database.
my script is this
<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
mysql_select_db($database) or die(mysql_error());
echo "connected successfully to db:" . $database . "<br>";
?>
you are connecting using
mysqli_
function
and selecting data with
mysql_
avoid using both at the same time. since they're incompatible.
use the mysqli_ alternative instead
mysqli_select_db($conn, $database);
and
mysqli_error($conn)
Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.
<?php
$servername = "localhost";
$username = "root";
$password = "mm";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
http://www.w3schools.com/php/php_mysql_connect.asp
To select data from the database
http://www.w3schools.com/php/php_mysql_select.asp
It appeared you where combining the old mysql in php with the new mysqli