Displaying blank page when including php-mysql connection file - php

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 ().

Related

How to connect MySQL server without putting user/pass in the main page [duplicate]

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>

having issue with my php code getting Error 500

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;
}
?>

how to provide information from a mysql database on a website using php

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.

PHP MSQL server syntax error

I get this error when trying to get this details page for a project to work. Its for school and I dont really understand PHP that well yet.
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
Here is the code for that page.
<?php
require_once('connection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = $recordID";
$Shoe_Details = mysqli_query($conn, $query_Shoe_Details) or die(mysqli_error(($conn)));
$row_Shoe_Details= mysqli_fetch_assoc($Shoe_Details);
$totalRows_Shoe_Details = mysqli_num_rows($Shoe_Details);
?>
<!DOCTYPE html>
<html>
<head>
<title>details</title><?php include 'connection.php';?>
</head>
<body>
<p>Product Name: <?php echo $row_Shoe_Details['Product_Name']; ?></p>
<p><img src=
"images/%3C?php%20echo%20$row_Shoe_Details['Image_Name'];%20?%3E"></p>
<p>Description: <?php echo $row_Shoe_Details['Product_Description']; ?></p>
<p>Price: $<?php echo $row_Shoe_Details['Product_Price']; ?></p><?php
mysqli_free_result($Shoe_Details);
?>
</body>
</html>
change your query as, use single quotes
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = '$recordID'";
Also remove this <?php include 'connection.php';?>, No need to include again
Pl. prepare connection.php file in followng way like
$dbHost = '';
$dbUser = '';
$dbPass = '';
$dbName = '';
// setting up the web root and server root for
// this shopping cart application
$con=mysql_connect('','','');
if(!$con)
{
die('connection failed');
}
$db=mysql_select_db('',$con);
if(!$db)
{
die('db is not selected');
}
pass proper value in this syntax save it and in your code remove second line and test then and give feedback

Using Template on PHP

i create a template file for my site... it's like:
<!-- template.php -->
<?php function showheader() { ?>
<head><body>
<!-- some of style files and menus -->
<div class="content">
<?php } ?>
<?php function showfooter() { ?>
</div></body></html>
<?php } ?>
i use this file as a template like this:
<?php include_once("template.php"); showheader(); ?>
content text or photo or ... etc.
<?php showfooter(); ?>
that's all... but if i try to use a connection on template file, it screw up!
i used an external file like:
<?php
//
// include_once connection file
// query strings goes here
//
do {
echo $row_table['id']; //example
} while ($row_table = mysql_fetch_assoc($table));
?>
and i use this file as include_once("filename.php"); on template file... at this point it gives errors... like what is this connection variable, what is this connection string... etc. it cannot reach connection strings...
by the way, i use another external connection like:
<?php
global $hostname_conn,$database_conn,$username_conn,$password_conn,$conn;
$hostname_conn = "localhost";
$database_conn = "test";
$username_conn = "****";
$password_conn = "****";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET NAMES 'utf8'");
?>
i'm gonna cry! what's the problem... and do you know another way to use template...
thanks much...
PS: i change variables on conn.php as global (and it didnt work) and i change include, include_once, require, require_once where i include files but it didnt give anything.
This separates the page into two PHP files: (1) the first obtains the data, and (2) the second displays the data.
While getting data, not a single character should be printed out.
If some errors occurred, display an error page.
Once you get all your data with no errors - it's time to include a template. The template has two PHP files as well: the template for the page itself and the template that is shared in common by all the pages in the site.
By sorting things this way you'll solve all your present and future templating problems.
A typical script may look like
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>
where template.php is your main site template, including common parts, like header, footer, menu etc:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
and links.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
easy, clean and maintainable.
settings.php contains all common settings:
<?php
$hostname_conn,$database_conn,$username_conn,$password_conn,$conn;
$hostname_conn = "localhost";
$database_conn = "test";
$username_conn = "****";
$password_conn = "****";
$conn = mysql_connect($hostname_conn, $username_conn, $password_conn)
or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET NAMES 'utf8'") or trigger_error(mysql_error(),E_USER_ERROR);
$tpl = "default.php";
$pagetitle = "";
function dbgetarr(){
$a = array();
$args = func_get_args();
$query = array_shift($args);
$query = str_replace("%s","'%s'",$query);
foreach ($args as $key => $val) {
$args[$key] = mysql_real_escape_string($val);
}
$query = vsprintf($query, $args);
$res = mysql_query($query);
if (!$res) {
trigger_error("dbget: ".mysql_error()." in ".$query);
} else {
while($row = mysql_fetch_assoc($res)) $a[]=$row;
}
return $a;
}
?>

Categories