Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I connect to a MySQL database in PHP?
Database name: NewDB1
Username : Mark
Password : secret
I have googled a few solutions, but none of them seem to work.
<?php
$servername = "localhost";
$username = "Mark";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
With PDO -
<?php
// turn on error reporting and display
error_reporting(E_ALL);
ini_set('display_errors', 1);
// define the user
define('USER', 'Mark');
define('PASS', 'secret');
// establish database connection
try {
$dbh = new PDO('mysql:host=localhost;dbname=NewDB1', USER, PASS);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // make sure to turn on error reporting
}
catch(PDOException $e) {
echo $e->getMessage(); // show any error messages.
$errorCode = $e->getCode();
}
?>
This comes from a more detailed tutorial on how to understand and simplify PDO.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to connect to databse on my server by using this code:
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But code after $conn = new mysqli($servername, $username, $password); is not executing. When i try to echo anything after it I do not get output. Code before that line works as expected. I do not get any errors from php.
I am not sure what is problem. Could it be something server related? I did try to add:
ini_set('display_errors',1);
error_reporting(E_ALL);
but it didn't help, no errors have been displayed.
I have been trying many things from stackoverflow (and other places) but they didnt help, some examples:
Connecting to a mysql database from php, error but no error shown?
Connection of MySQL with PHP not working
There are multiple ways to handle errors
Simplest of all, use try catch while connecting
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
try {
$conn = new mysqli($servername, $username, $password); } catch(\Exception $e) { var_dump ('oopss... this is the error', $e)}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected
This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 5 years ago.
I wonder if someone can help. I gave up web programming about 3 years ago. Since returning to programming about a week ago I realize I have forgotten quite a lot and so much has changed.
I signed on to a deal with php7 but my code is php5 and when I came to running my scripts nothing really works.
For example I couldn't even connect to the database. My database connection file for php5 was
<?php
$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "db_username_here";
// Place the password for the MySQL database here
$db_pass = "db_password_here";
// Place the name for the MySQL database here
$db_name = "name_of_database_here";
// Run the actual connection here
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I spoke to the server company and they told me everything has changed and to get my code compatible with php7. They then helped me get my database connected by giving me a sample file of how it should be which is this code.
<?
/* DATEBASE CONFIGURATION */
$dbHost = "ip Address";
$dbName = "name_of_database_here"; // Database name
$dbUser = "db_username_here"; // Database user
$dbPasswd = "db_password_here"; // Database password
function dbConnect(){
global $dbHost, $dbUser, $dbPasswd, $dbName;
mysql_connect( $dbHost, $dbUser, $dbPasswd ) or error( mysql_error() );
mysql_select_db( $dbName );
}
?>
I have always run my database $dbHost from localhost so I have guessed that because they haven't done the connection as localhost and done it as gobal with an ip address I have figured that the database is not on the same server as the website.
I then came to my database scripts for running from the browser to phpmyadmin and they also didnt work. Here is my database script php5.
<?php
require "connect_to_mysql.php";
$sqlCommand = "CREATE TABLE admin (
id int(11) NOT NULL auto_increment,
username varchar(24) NOT NULL,
password varchar(24) NOT NULL,
last_log_date date NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username)
) ";
if (mysql_query($sqlCommand)){
echo "Your admin table has been created successfully!";
} else {
echo "CRITICAL ERROR: admin table has not been created.";
}
?>
I was wondering if someone could give me some advice on why the script isn't running
Many thanks, Gary
mysql_connect is removed from php7.
instead of that you can use either mysqli or pdo
example with mysqli
<?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);
}
echo "Connected successfully";
example with pdo
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
For more reffer details https://www.w3schools.com/php/php_mysql_connect.asp
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I've installed xampp and it seems to be up and running, I have tested php by executing the phpinfo() function and it works. I can create databases and manipulate them in phpmyadmin, and the localhost server works too
How ever when I attempt to actually connect through php....
<?php
$conn = mysqli_connect('localhost', 'root', '');
mysql_select_db("testskills");
if(!$conn) {
die("Connection Failed: ". mysqli_connect_error());
}
..... I don't get any errors but the code breaks and the browser just shows me the actual code from the file the form action called
I'm stumped
Lori
Try this
<?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);
}
echo "Connected successfully";
?>
<?php
$host_name = "localhost";
$u_name = "root";
$u_pass = "";
try {
$dbh = new PDO("mysql:host=$host_name;dbname=personal_db", $u_name, $u_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex) {
echo "Connection failed: " . $ex->getMessage();
}
?>
I would suggest that you start learning to use PDO man.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I can access localhost/phpmyadmin/ through my browser and create tables, insert tuples, etc but I can't connect to it in PHP code. Here's what I got:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$conn = new mysqli('localhost', 'root', '');
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success';
$mysqli->close();
?>
and i have an HTML form somewhere else with an action attribute that runs the above code. But whenever I test my code, it returns an Error 500. I won't tell me anything more specific, even though I included error reporting! Why?
You have error in creating connection:
mysqli is not a function, you need to use mysqli_connect
$conn = new mysqli_connect('localhost', 'root', ''); // here is change
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success'
$conn->close(); // close connection using connection resource
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want my earth.php file to find the users id when a user enter mysite.com/earth.php. I want this because I want the id to be placed in a sql code that will be sent to the database when the user enters mysite.com/earth.php. And I think I have to start a session when a user logs in to help the earth.php to find the user id? Can someone help me with that because I don't know how to do it.
This is my earth.php file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
$id = $_SESSION['id'];
session_write_close();
$verbindung = mysql_connect("localhost","root","******");
mysql_select_db("lan");
if(isset($_SESSION['id'])) {
mysql_query("UPDATE users SET ally='3' WHERE id='$id'");
}
?>
And this is the value I want to be changed for the user to 3 when they enters mysite.com/earth.php
This is my login.php file
Please help, I've been trying to solve this problem for a long time.
You could do something like this
<?php
define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'dbname');
define('DB_USER', 'root');
define('DB_PASS', 'password');
session_start();
try {
// create a new instance of a PDO connection
$db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
// if the connection fails, display an error message
echo 'ERROR: ' . $e->getMessage();
}
if(isset($_POST['username']) && !empty($_POST['username'])) {
$username = $_POST['username'];
$sql = 'SELECT userid, username FROM users WHERE username = :username';
$stmt = $db->prepare($sql);
$stmt->bindValue('username', $username);
$stmt->execute();
$result = $stmt->fetchAll();
$_SESSION['id'] = $result[0]['userid'];
var_dump($_SESSION['id']); //this will show the the session
}
?>
As I see from your code, the $_SESSION['id'] is empty. Set a value for it. I see you edited your post. Then you just have to include your file after starting the session like
session_start();
include_once 'path/to/login/php/login.php;