can some one please tell me if these's a problem in my code
$db->orderBy('date','asc');
$schedules = $db->get("schedules");
by having these 2 lines on my page, my page shows Error 500
and if i test it on my computer on Localhost it looks fine and works perfectly.
and don't know if the problem is my code or it is server side.
Thanks.
This is my code to connect to database
<?php
$db_host = "HOST";
$db_username = "USERNAME";
$db_password = "PASSWORD";
$db_name = "NAME";
$con = #mysqli_connect("$db_host","$db_username","$db_password","$db_name");
if(!$con)
{
echo"
<html>
<head>
<meta charset='UTF-8'>
</head>
<h1>Can't Connect to database</h1>
</html>";
exit;
}
?>
Related
This question already has answers here:
How to secure database passwords in PHP?
(17 answers)
Closed 2 years ago.
I have a main page on my site and I am trying to make a connection to the MySQL server and get some data from the database.
But the problem is that when I want to establish a connection I have to put the username and password in the page which doesn't seem wise to me.
I added a part of my code related to this problem.
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php
$host = "localhost";
$userName = "username";
$password = "12345678";
$dbName = "MyDB";
$connection = new mysqli($host , $userName , $password , $dbName);
if ($connection->connect_error) {
die("Connection failed due to this error : " . $connection->connect_error . '<br>');
}
else{
echo "Connected successfully";
{
</body>
</html>
Put your connection code into another file, like connection.php and in every page you want to connect you should require_once "connection.php", also you could use variables from that file if you connect it, also instead of require_once you can use just require, but look at the internet differences between them
Normal practice is to put the mysql data in a separate php file. For example:
dbmain.php
<?php
$host = "localhost";
$userName = "username";
$password = "12345678";
$dbName = "MyDB";
$connection = new mysqli($host , $userName , $password , $dbName);
?>
then you can include this file in all php files which require db connection.
<?php include_once("dbmain.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php
/// php commands, such as
//if ($result = $mysqli -> query("SELECT * FROM dbtable1")) {
// echo "Returned rows are: " . $result -> num_rows;
// Free result set
// $result -> free_result();
//}
//$mysqli -> close();
?>
</body>
</html>
I have a problem with providing infromation from a mysql database through php. I am working with brackets and always when I try to connect with my database (opening my connection, giving information about my server, port and the name of the dtabase -> shown in the code below) nothing happens. I can not administrate my database with phpmyadmin I know that would be easy. I also have xamp and a apache server working, but then I would have to use phpmyadmin to connect.
But is there a way to connect the database (created with mysql workbench running on localhost 127.0.0.1 and port 3306) only like that with my php file. Or is the problem that I have a mistake in my code?
Code:
File one: db_connection.php
<!DOCTYPE html>
<html>
<header>
<center> <h1>My New Website </h1> </center>
</header>
<body>
<?php
//die echo Funktion, ist dafür da, dass etwas in den Browser ausgegeben wird (same as Syso)
//echo "Hello World!";
function OpenConnection()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "dml_final";
$connection = new mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname)
or die ("Connection failed: %s\n". $connection -> error);
return connection;
}
function CloseConnection()
{
$connection -> close();
}
?>
</body>
</html>
File two: index.php
<?php requires 'db_connection.php'; ?>
<!DOCTYPE html>
<html>
<header>
<center> <h1>My New Website </h1> </center>
</header>
<body>
<?php
$connection = openConnection();
echo "Connection successfully!";
CloseConnection($connection);
?>
</body>
</html>
If you want to use your current way, change function to public function so that it is globally available across your website, I recommend the following method instead:
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "dml_final";
$connection = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
if(!$connection) {
die("Connection failed:".mysqli_connect_error());
}
And you include this in all of your PHP files. To close the connection, simply run a mysqli_close($connection);. I don't know about you, but I find this way more simple.
I'm creating a signup page, when I add required fields to establish database connections in the beginning of the body of my signup page(splash.php) i encounter a 404 error upon page loading on localhost when I open a preview window in Adobe Dreamweaver. I additionally encounter these 2 errors when manually opening splash.php through the url bar:
1. Warning: require_once(/start/db.php): failed to open stream:
2. Fatal error: require_once(): Failed opening required '/start/db.php' (include_path='.;C:\php\pear')
I've looked into the folders containing the files, those 2 php files seem to open fine and are present in the "start" folder. I cannot figure out why requiring these files causes a 404 error & the other 2.
Here is the beginning html doc for splash.php: the 2 require_once codes are causing the problem.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" href="/ink/post_creation.css">
</head>
<body>
<?php require_once("/start/db.php"); ?>
<?php require_once("/start/signup.php"); ?>
Here is the beginning of the database:
<!doctype html>
<?php
$dbhost = 'localhost';
$dbname = 'start';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($connection->connect_error) die("Fatal Error");
And beginning of signup.php:
<!doctype html>
<?php
if (isset($_POST['signup']))
{
require_once('/start/db.php');
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['pwd'];
I'd hoped it would simply load the page, any help would be fantastic!
I recently switched to CPanel, and ever since then, I've had issues. I'm having an issue connecting to a database. I created it in CPanel, with the hypothetical username 'root' and password '123'. I have a file /scripts/dbh.php and a file /index.php. Here are the contents.
dbh.php:
<?php
// Database Handler
$db_name = 'hughchalmers';
$servername = "localhost";
$username = "root";
$password = "123";
$table_name = 'accounts';
$conn = mysqli_connect($servername, $username, $password, $db_name) or die('Cannot connect to database. Contact an admin');
?>
(hughchalmers is a database, accounts is a table)
index.php:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/memedictionary/scripts/header.php');
include($_SERVER['DOCUMENT_ROOT']. '/memedictionary/scripts/dbh.php')
?>
<head>
<title>Login</title>
</head>
<body>
</body>
What's the issue here? It just dies with that message I set (Cannot connect to database. Contact an admin). Apologies, I'm very new to MySQL and CPanel
Check your last parameter for mysqli_connect(). Looks like you're referring to a table when instead it must be the database name or is it just a variable name but you're passing a database name actually?
I am using Ubuntu 14.04 and I am using lamp. I had a more complex issue but I figured out what is causing the error and made a small php-mysql connection snippet. When the connection file is included into the php/html file the whole thing displays a blank page.
My connect.php code:
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "ecalendar";
$conn = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if(mysqli_connect_errno(){
echo "Error".mysqli_connect_errno();
}
?>
and my web page with connection file included:
<?php
include "connect.php";
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Testing</p>
<?php
$sql="SELECT * FROM events WHERE eventid=1";
$result = mysqli_query($sql);
?>
<ul>
<li><?php $row = mysqli_fetch_array($result);
echo $result;
?>
</li>
</body>
</html>
<?php
mysqli_close($conn);
?>
The following line in connect.php:
if(mysqli_connect_errno(){
Should be:
if(mysqli_connect_errno()){ // note the extra ')'
You were not properly enclosing your condition with ().