<?php
$host = "localhost";
$username = "root";
$password = "";
$db = "mineforums";
$connect = mysqli_connect($host, $username, $password, $db) or die(mysqli_error($connect));
?>
That is my php code to connect to my database. Whenever I try to just connect though it doesnt do anything. The way I have my login-form setup is the action will be to this code and then doesnt do anything and when I return to my index it says database not selected because if you're logged in it says 'Welcome, {username}'
you could split the process
<?PHP
$host = "localhost";
$username = "root";
$password = "";
$db = "mineforums";
$connect = mysql_connect($host, $username, $password)OR DIE("Could Not Connect To Server". MySQL_Error());
if($connect) {
mysql_select_db($db)OR DIE("Could Not Select Databse". MySQL_Error();
}
?>
Related
When I run this code it shows parse error. Can anyone help me with this?
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
SELECT * FROM my_db;
$conn->close();
You should use php tags and remove those ">" also you should use mysqli_query to query the data from mysql
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
$query = mysqli_query($conn,"SELECT * FROM my_db");
$conn->close();
?>
I'm trying to connect to mysql db in phpstorm, but I don't succeed.
I will appreciate your help.
<?php
$servername = "http://localhost:8012";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
die("Connected successfully");
}
The error is:502 Bad Gateway
You can connect database like that:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "";
$port = "8012";
// Create connection
$conn = new mysqli($servername, $username, $password, $database, $port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
die("Connected successfully");
}
<?php
$servername = "localhost:8012";
$username = "root";
$password = "";
$database = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$database) or die(mysqli_error($conn);
echo "Connection Successful";
as you can see my code down below, the method I used to detect does mysql_select_db() return true or false. It does return true but I still ge the "No database selected" error.
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password) or die("Error");
if (!mysqli_select_db($con, "tryckstore")) {
die("Error selecting databse.");
} else {
echo "ok";
}
It used to work actually, then I suddenly get this error. Thanks in advance.
Use this one:
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password,$sql_db) or die("Error");
if (!mysqli_select_db($con, "tryckstore")) {
die("Error selecting databse.");
} else {
echo "ok";
}
//pass the $sql_db as your 4th argument in the mysqli_connect also
for simplier approach:
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password,$sql_db) or die("Error");
this is my first php code , i am trying to connect to a database with user name = "root" and password = "root"
i have a connection file called dbConnection.php
as following :
<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";
//connection to the database
$conn = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysql_select_db($db);
echo "db-connection : connection done";
// Check connection
echo "Connected successfully";
?>
and i call it in a file called what.php :
<?php
echo "Hello";
include "dbConnection.php";
echo "ohhhh";
?>
this returns status code 500 which is internal server error
but i want to know what is the error to fix it how can i get the error message ?
i tried
$conn = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
but it is not returning any thing.
can any one help me please ?
if you are having password on localhost then use password otherwise leave it blank
$con = mysqli_connect("localhost","root","yourpassword","yourdb");
You mysqli_connect() method.
<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";
//connection to the database
$conn = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysqli_select_db($conn,$db);
echo "db-connection : connection done";
// Check connection
echo "Connected successfully";
?>
First Thing is to Change your connection method to mysqli or my personal favorite PDO.
PDO Example:
class db extends pdo{
//Website Variables
public $sitedb = '';
public $siteconfig;
public $sitesettings = array(
'host' => 'localhost',
'database' => 'yourdb',
'username' => 'youruser',
'password' => 'yourpass',
);
public function __construct(){
$this->sitedb = new PDO(
"mysql:host={$this->sitesettings['host']};" .
"dbname={$this->sitesettings['database']};" .
"charset=utf8",
"{$this->sitesettings['username']}",
"{$this->sitesettings['password']}"
);
$this->sitedb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
}
$db = new db();
Then you can extend your PDO class to new classes after including the db.php page.
Example Select:
class yourclass extends db {
public function SelectUsers() {
global $db;
$query = <<<SQL
SELECT email
FROM users
WHERE active = :active
SQL;
$resource = $db->sitedb->prepare( $query );
$resource->execute( array (
':active' => 1,
));
$count = $resource->rowCount();
foreach($resource as $user){
$this->email = $user['email'];
}
}
}
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
// make the current db
$db_selected = mysqli_select_db ( $conn , $dbname );
if (!$db_selected) {
die ('Can\'t connect to Database : ' . mysql_error());
}
?>
I'm using a WAMP server and on it I created a database and a table. All the names are correct and the user has full access to everything. When I run the code, it prints out "Unable to select database". Thanks.
<?php
if(isset($_POST["Submit"])){
print_r ($_POST["nutrient"]);
}
session_start();
//establish connection
$server = "localhost";
$db_username = "root";
$db_password = "";
$database = "gainlife_cavin";
$table = "cavintable";
//connect PHP script to database
$connection = mysqli_connect($server, $db_username, $db_password, $database);
//select database to use
#mysql_select_db($database) or die( "Unable to select database");
//$query = "INSERT INTO $table VALUES("")"
//mysql_query($query)
mysql_close();
?>
<body>
</form>
Try something like the following.
<?php
//establish connection
$server = "localhost";
$db_username = "root";
$db_password = "";
$database = "gainlife_cavin";
$table = "cavintable";
//connect PHP script to database
$connection =mysqli_connect("$server","$db_username","$db_password","$database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Your query here
mysqli_close($connection);
?>
I use simple code 1 line. here is my code that i'm using.
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
You went from MySQLI to not using MySQLI in selecting the database as well as the rest of your code.
Try this. I have change mysqli_connect to mysql_connect and mysql_select_db variable.
//connect PHP script to database
$connection = mysql_connect($server, $db_username, $db_password, $database);
//select database to use
$select = mysql_select_db($connection) or die( "Unable to select database");