include() function in php is not working - php

i'm currently working on a website that has three folders namely admin,images and includes.
The includes section has the file 'connect.php' that tells about my database and the code for that is as follows..
<?php
$db_username ="root";
$db_password ="*****";
$db_host ="localhost";
$db_name= "ecommerce";
$db=mysqli_connect($db_host,$db_username,$db_password,$db_name) or die("something went
wrong");
if(!$db)
{
echo "failed".mysqli_error();
}
echo "succesfull";
?>
i just want to include this file to "dum.php" which is present in the folder admin,for which i have written the includes function.here is the code for "dum.php"
<?php include_once('includes/connect.php');?>
so,when i'm running it on browser,it has to print either "an sql failure" or a "succesful" message. but it does neither of that?
Any help would be highly appreciated

If includes is at the same level as admin, it should be:
include_once('../includes/connect.php');
You should probably use require instead of include so you get an error immediately if it fails.

Related

Constant variable already defined Xampp error

I am troubleshooting a site for someone and one of the error goes like this:
Constant serverusername already defined in C:\xampp\htdocs\employee\inc\db.php on line 5
The contents of db.php is as follows:
<?php
//Database Connection Settings
define ('hostnameorservername','localhost'); //Your server name or hostname goes in here
define ('serverusername','root'); //Your database username goes in here
define ('serverpassword',''); //Your database password goes in here
define ('databasenamed','asset'); //Your database name goes in here
global $connection;
$connection = #mysql_connect(hostnameorservername,serverusername,serverpassword) or die('Connection could not be made to the SQL Server. Please report this system error at <font color="blue">info#servername.com</font>');
#mysql_select_db(databasenamed,$connection) or die('Connection could not be made to the database. Please report this system error at <font color="blue">info#servername.com</font>');
?>
When I search the "serverusername" on the entire directory, I can't find it anywhere else other than on
:\xampp\htdocs\employee\inc\db.php on line 5
However the line:
include 'inc/db.php';
exists on numerous files.
What seems to be the problem? How can I resolve it? Thank you in advance
This problem may be occurred due to include of db.php file many times on same page.
Try this :
include_once 'inc/db.php';
Replace your code with this:
defined('hostnameorservername') or define('hostnameorservername', 'localhost');
defined('serverusername') or define('serverusername', 'root');
defined('serverpassword') or define('serverpassword', '');
defined('databasenamed') or define('databasenamed', 'asset');

How to connect php application to database

I have created register form using php and validation done by using jquery.
I have files are index.php, submit.php and functions.php.
So i need to create config.php to connect database.
I have done to create table in phpmyadmin and created config.php.
Here is my config.php:
<?php
// configuration
$dbhost = "localhost";
$dbname = "crop";
$dbuser = "root";
$dbpass = "";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = "INSERT INTO crop (fname,lname,email,pass,phone,img,sex-select,dob) VALUES (:sas,:asas,:asafs)";
$q = $conn->prepare($sql);
?>
I am not sure this code correct or not? And i don't know how to include in existing my php files.
I am new to php and now i am learning, can you please help me out to fix this?
Thanks in advance.
To include the connection in other files you simply need to add the following near the top of the other file, before you need any database connection:
require_once 'config.php';
require generates a fatal error if the file is not found and require_once guarantees that the file will only be included once even though you call it multiple times. Since your application will probably not work without a database connection you want the fatal error to let you know that the file wasn't found and it's highly unlikely that you'll want more than one database connection.
There are also include and include_once but in this case you'll want to avoid these because they don't stop the script if the config file isn't found.
PHP documentation:
http://php.net/manual/en/function.require-once.php
http://php.net/manual/en/function.include.php
You have to print the results of the query that you ran.
Check Here on PDO: Prepared Statement
Try this connection code in your web application, your config.php look like
<?php
$Db_host = "localhost";
$Db_user = "root"; //DB User name
$Db_name = "crop"; //DB name
$Db_password = ""; //DB password if you have
$cnn=mysql_pconnect($Db_host,$Db_user,$Db_password) or die(mysql_error());
?>
and after that you can call this file with the help of include or require function in your page.
your submit.php looks like this
<?php require_once('config.php'); ?>

PHP Include DB Conn - Password not accessible

I have a strange include file problem. I'm hoping its a simple fix due to being a 2-day-old, PHP noob.
I'm simply trying to include DB credentials in to my parent script, like this:
db-conn.php
<?php
$dbUsername = "xxx";
$dbPassword = "xxx";
$dbHostname = "localhost";
?>
parent.php
<?php
include("c:/inetpub/vhosts/mydomain.net/php-private/db-conn.php");
//echo $dbPassword;
//echo $dbUsername;
$dbHandle = mysql_connect($dbHostname, $dbUsername, $dbPassword) or die("Unable to load page content due to a connection fault.");
unset ($dbHostname, $dbUsername, $dbPassword);
echo "Connected to MySQL<br><br>";
?>
But I'm getting this error:
Access denied for user 'xxx'#'localhost' (using password: NO)
What is strange about this (for me) is that I can echo the username and hostname successfully but I cannot echo the password, it's empty. Yet if I go to my include file, copy the password from 'dbPassword' and paste it inline on my parent page, it works. This proves my password is correct, the username and hostname ARE included, I just can't access my password variable!
Can somebody pleeeease put me straight here. I've already pulled my hair out tonight with open_basedir and permissions for this include file!! _
*UPDATE - *
Problem solved, please see my answer below.
If db-conn.php and parent.php are in the same folder, you can directly use
include ('db-conn.php');
And I'm not sure about the use of unset ($dbHostname, $dbUsername, $dbPassword);
Also it's saying that no password has been set. So maybe you should use empty string for the $dbPassword variable $dbpassword = '';
Access denied for user 'xxx'#'localhost' (using password: NO)
is simply telling you that you are not giving password to mysql_connect()
You can 1. Check if you are including the correct db-conn.php file.
2. Add this to the top of your db-conn.php
global $dbUsername, $dbPassword, $dbHostname;
Try using 127.0.0.1 as a hostname. The problem is that sometimes PHP doesn't accept localhost as host, because of a missing line in the host.ini file.
Sorry for wasting time guys. I found the problem. I didn't give FTP users write access to the \php-private\ folder I created before the root, so when I was changing the file and uploading it, if I actually checked the FTP logs I would have seen that it failed with 'Access Denied'. I changed permissions and kapow, my CURRENT include file works fine! What a pleb!

Connecting to database via php code

I am programming a game in PHP and have the following code to connect to a database
//$sqldb=mysql_connect('godaddy.hostedresource.com', 'godaddyUserName', 'godaddyPassword') OR die ('Unable to connect to database! Please try again later.');
$sqldb=mysql_connect('localhost', 'root', 'mypassword') OR die ('Unable to connect to database! Please try again later.');
The trick here is that if I am on the production server I comment out the godaddy database; when I upload the code to the server I then comment out the localhost code instead.
Unfortunately the ineveitable has happened and I uploaded the code with the wrong connection commented out; this led to 24 hours of locked out customers! :(
Is there a way to have the code to tell if it is on the localhost server, and if it isn't it then looks for the godaddy connection?
you can try this to identify if its on live or localhost
if($_SERVER["SERVER_NAME"] == "localhost"
&&
$_SERVER["SERVER_ADDR"] == "127.0.0.1"){
// in localhost
$hostname = "localhost";
$username = "localuser";
$password = "localpassword";
}else{
// not in localhost
$hostname = "livehost";
$username = "liveuser";
$password = "livepassword";
}
and fail if couldn't connect to database but save the error into a file.
if(!mysql_connect($hostname,$username,$password)){
file_put_contents("mysql_connect.error",mysql_error(),FILE_APPEND);
die("Couldn't connect to database");
}
a suggestion, try not to use mysql_* anymore, switch to PDO or mysqli ..
if ($_SERVER['SERVER_NAME'] == 'the.name.of.your.devel.server') {
$host = 'localhost';
} else {
$host = 'name.of.godaddy.server';
}
$sqldb = mysql_connect($host, ....);
i normally use a method of obtaining the URL / domain of the site? This can work in certain situations and setups. Otherwise if your operating with a fixed IP than you can also use this method
Have a look over the methods using $_SERVER
PHP $_SERVER
One way would be for you to check your external IP address and see where you are. A solution should present itself by looking at the properties inside the $_SERVER global variable.
I have a good suggestion : You coding a game , game is a big program, you don't use mysql* function directly in big program , because yourself should handling them, such as error handling.i suggest you use a DB-Handler. please google for : DB-Handler PHP
As has been mentioned by other people, you can obtain the current site your script is running on using the $_SERVER variable. However, I would like to provide an alternative solution.
You could make a folder in your website (both local and production), something like config, then store a configuration file in it, for example config.php, with the following:
<?php
// Local
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'mypassword';
?>
And for production:
<?php
// Production
$db_host = 'godaddy.hostedresource.com';
$db_username = 'godaddyUserName';
$db_password = 'godaddyPassword';
?>
and disallow access to the directory with a .htaccess file in the directory, something like:
deny from all
Then, in your PHP code, do the following:
<?php
require_once($_SERVER["DOCUMENT_ROOT"] . "/config/config.php");
$sqldb=mysql_connect($db_host, $db_username, $db_password) OR die ('Unable to connect to database! Please try again later.');
?>
Now, simply leave the different configuration files where they're at and upload everything else, so your code will access different configuration files whenever it runs.
Also, the .htaccess file should prevent anyone from accessing the file via HTTP, and having the file contents in PHP tags, as well as a .php extension should prevent anyone from seeing any contents if they were able to access the file (PHP would parse the file before it is rendered, and would output nothing).

PHP - Using a function outside the file it has been created in

I have 3 php files. The first (connexion.php) one contains a function I use to create a new PDO connexion:
<?php
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set('Europe/Paris');
function connexion($host, $user, $pass, $db){
$db_host = $host;
$db_user = $user;
$db_password = $pass;
$db_database = $db;
return $connexion = new PDO("mysql:host=$db_host;dbname=$db_database;charset=utf8", $db_user, $db_password);
}
?>
The second file (my_function.php) contains a function that contains only one function that connects to a db and then is echoing some information.
<?php
include(connexion.php);
function my_first_function(){
try{
$connexion = connexion('localhost', 'user', 'user', 'mydb');
$connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//some code that echoes information
}
catch(...){...}
}
?>
My problem is when I try to include that last file into my third php script in order to be abble to use the function. The script is not even launching. I just have 'internal error'. If I remove the include(my_function.php) my file is loaded properly. I don't get it. Hope someone can help me understand. Thanks in advance. Cheers. Marc. Here below the third file:
<?php
include('connexion.php');
include('my_function.php');
//some code
my_first_function();
?>
You should not use include('connexion.php'); in the third file as it will also be included already automatically when you do include('my_function.php');
As it contains a function declaration, that will lead to an error because that function has already been declared.
"Internal error" is not a PHP thing, you may want to check your .htaccess or other apache settings.
If you are trying to include the file more than once, you will get errors because PHP won't allow you to redeclare a function with the same name. To get around that, use:
include_once("my_include_file.php");
You are either not providing the relative path to the include file or your include file has an error inside it. To test this, try the following code..
if( file_exists('the_file_where_the_function_is.php') )
{
echo "Found the file";
} else {
echo "File not found";
}
If it finds the file, then you most likely have a syntax error in the included file. Also I am guessing you are using IE and getting an "Internal Server Error" ??? If so go into your preferences for IE and turn off friendly error messages to see the actual PHP error.

Categories