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');
Related
I have a login page,login_form, menu page, and download page , this has been running successfully on a server earlier but i want all this to run on my local host, hence, I copied it all and put it in /var/www/html folder and was able to run the login page but the rest like menu.php and all dont come up.
IT has also a sql database which i just added into my system using, mysql -u root -p exaample < cs303.sql
and it is able to process normal select statements as well
Anybody here happens to know what is happening?
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
define ('HOSTNAME', 'localhost');
define ('USERNAME', 'root');
define ('PASSWORD', 'merlin');
define ('DATABASE_NAME', 'wicap');
$db = mysql_connect('localhost', 'root', 'merlin') or die ('Cannot connect to MySQL.'); // connecting to database
//echo"l; km;l";
mysql_select_db('wicap.sql');
$v1=$_REQUEST["username"];
$v2=$_REQUEST["password"];
//echo "sdfdf";
//$v1="ritish";
//$v2="ritish_r";
//echo($user);
//echo($pass);
$q=mysql_query("SELECT * FROM login where pass='".$v2."' and user='".$v1."'");
while($r=mysql_fetch_array($q))
{
echo($r[0].$r[1]+"/n");
echo "mohit";
}
mysql_close();
?>
#MerlinSudar: for more clarity i am demonstrating the code here
<?php //first PHP opening tag
//put these lines
ini_set("error_reporting",1)
ini_set('display_errors',1);
. //you code
. //you code
?>
Hope this helps
Follow the below Step hope it will solve your problem...
1) if you are using a xammp the put all your files in the htdocs folder, and if you are using the wammp the put all you file in www folder.
2)Check the linking of file, give the proper link connection.
Enable error reporting
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
Add this code on top of your index file and check
Since mysql is deprecated, please dont use it anymore rather use the improved version
of mysql http://php.net/manual/de/book.mysqli.php
I have a large procedural style php/mysql website, which is splitted into many different files. Within each file, i include my dbconn.php, to ensure, that a db connection is available.
It looks something likes this:
index.php
<?php
include_once ('header.php');
include_once ('nav.php');
include_once ('content.php');
include_once ('sidebar.php');
include_once ('footer.php');
?>
and within each of these files i call several other files via include_once. So in total i come up with about 30 different files.
Every file starts with:
include_once($_SERVER['DOCUMENT_ROOT'].'/dbconn.php');
Since i'm using include_once, dbconn.php should not be loaded more then once, but recently i get following php warning.
PHP Warning: mysqli_connect(): (HY000/1226): User 'username' has exceeded the 'max_user_connections' resource (current value: 20) in /var/www/html/dbconn.php on line 10
My dbconn.php looks like this:
<?php
$host = "localhost";
$user = "username";
$pass = "password";
$db = "dbname";
if(!mysqli_thread_id($con)){
$con = mysqli_connect($host, $user, $pass, $db);
mysqli_set_charset($con, 'utf8');
}
unset($host,$user,$pass,$db);
?>
So there is a double check.
If there already is an open mysql connection, the file should do basicly nothing.
What am i doing wrong?
Why do i get this php warning and how do i reduce the amount of mysql connections?
Basically, it's not different calls from a single page that is leading to the problem. It's the fact that you have multiple OPEN connections at the moment.
Make sure you close you connections after they have served their purpose.
Also, try to avoid the kind of structure you currently have, have all the DB related stuff included in one file and then include it only once on your page.
I solved the problem by using a singleton pattern.
So i changed the mysqli prodecural code into mysqli objectoriented.
i got the pattern here:
http://www.davecomeau.net/blog/1/Single+Database+Object+in+PHP+5+Using+the+Singleton+Pattern
The code below was not working:
public function __construct($thread_id)
{
require_once('../private/mysqli_connect.php');
require_once('../php_classes/class_message.php');
$this->thread_id=$thread_id;
$q="SELECT *
FROM message_thread_name
WHERE thread_id=2";
if($r=#mysqli_query($dbc, $q))
{
while($row=mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$this->poster=$row['name'];
$this->subject=$row['thread_subject'];
}
}
$qm="SELECT message_id
FROM message
WHERE thread_id=$this->thread_id";
if($rm=#mysqli_query($dbc, $rm))
{
while($rowm=mysqli_fetch_array($rm, MYSQLI_ASSOC))
{
$message=new message($rowm['message_id']);
array_push($this->messages, $message);
}
}
}
I ran out of ideas for fixing it, so I changed require_once('../private/mysqli_connect.php'); to require('../private/mysqli_connect.php');, and much to my surprise, it worked. Any ideas as to why that may be?
Thanks.
When you use require_once PHP will check if the file has already been included, and if so, not include (require) it again. You can read it here.
So whats the problem here?
From your problem statement, I am assuming you have created another instance of this class before, or you have already included mysqli_connect.php somewhere else. And in your mysqli_connect.php you have created the connection link something like.
$dbc= mysqli_connect( ... );
So when you using require_once as the mysqli_connect.php is not loading again, your $dbc variable remain undefined. and your query does not work. But when you use require instead of require_once the file loaded again, and new connection created again, and your code works.
NB: Though changing to require, solved your problem, you should not use it like this. It will create new connection to your database, each time you create new instance of your class. For your thought: you can try creating something like this
my first question:-) exiting.....
i'm buizy with a new CMS and i'm getting this error.....
Notice: Undefined variable: config in C:\xampp\htdocs\global.php on line 13
I checked my code twice, but it seems like nothing is wrong to me? if someone of you sees it please answer the question...
Here is my global.php:
<?php
session_start();
// Config
require_once "inc/config.php";
// Classes
require_once "inc/classes/class.tpl.php";
// Database connectie via MySQLi
$db = new mysqli($config["database"]["host"], $config["database"]["user"], $config["database"]["pass"], $config["database"]["name"]);
if($db->connect_error){
die("Pik, zet je database eens goed?");
}
?>
Thanx for your time...
*EDIT:
My config.php
<?php
$_CONFIG['database']['host'] = 'localhost';
$_CONFIG['database']['username'] = 'root';
$_CONFIG['database']['pass'] = 'assembla1';
$_CONFIG['database']['database'] = 'data';
?>
The problem might not be with your global.php, but could also have to do with the two files you are using under require_once. Makes sure the array $config is defined/created in one of them. If it isn't, then you know why the error arises.
Now that we know you have $_CONFIG defined in your config.php file, you should probably try to access the variable in the right manner - using it through a constructor instead. Look at How to create global configuration file? and mario's answer for more detail:
P.S. I am not able to comment on the question. Hence, threw in an answer.
In your code you have
$config["database"]["user"]
and in your config it is
$_CONFIG['database']['username'] = 'root';
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.