[Solved] Connecting to MySQL with an include file.
Right now I'm building a control panel type application for a website, and I need almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
Code: Select all
<?php
function dbconnect() {
# Set the Connection Variables
$host = "e;localhost"e;;
$username = "e;dbusername"e;;
$password = "e;dbpassword"e;;
$database = "e;dbname"e;;
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}Code: Select all
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;Code: Select all
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
[client 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.phpI'd appreciate any answer!
Thanks in advance,
Abs
P.S.: Much of the connection code is from a book that I'm learning from, so I'm sorry if it seems a bit sloppy.g that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
Code: Select all
<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}Code: Select all
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;Code: Select all
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.phpI'd appreciate any answer!
Thanks in advance,
Abs
P.S.: Much of the connection code is from a book that I'm learning from, so I'm sorry if it seems a bit sloppy.n from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
Here is the error from the Apache log:
Code: Select all
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
[client 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.phpI'd appreciate any answer!
Thanks in advance,
Abs
P.S.: Much of the connection code is from a book that I'm learning from, so I'm sorry if it seems a bit sloppy.lding a control panel type application for a website, and I need almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
Code: Select all
<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}Code: Select all
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;Code: Select all
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.phpI'd appreciate any answer!
Thanks in advance,
Abs
P.S.: Much of the connection code is from a book that I'm learning from, so I' include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
Code: Select all
<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}Code: Select all
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;Code: Select all
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.phpHere is what I have in my include file:
Code: Select all
<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}Code: Select all
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;Code: Select all
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Ath MySQL. I joined here hoping to learn more as I went along.
Right now I'm building a control panel type application for a website, and I need almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
[php]<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/php]
Here is the error from the Apache log:
[code]
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php[/code]
I've tried searching on Google for solutions, and It I'm pretty new to PHP, and that I only have limited experience with MySQL. I joined here hoping to learn more as I went along.
Right now I'm building a control panel type application for a website, and I need almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
[php]<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/plong.
Right now I'm building a control panel type application for a website, and I need almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
[php]<?php
function dbconnect() {
# Set the Connection Variables
$host = "e;localhost"e;;
$username = "e;dbusername"e;;
$password = "e;dbpassword"e;;
$database = "e;dbname"e;;
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/php]
Here is the error from the Apache log:
[code]
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php[/code]
I've tried searching on Google for solutions, and I've found alternates td almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
[php]<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/php]
Here is the error from the Apache log:
[code]
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php[/code]
I've tried searching on Google for solutions, and I've found alternates to what I am doing...However I'm posting here because I'd really went along.
Right now I'm building a control panel type application for a website, and I need almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
[php]<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/php]
Here is the error from the Apache log:
[code]
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel along.
Right now I'm building a control panel type application for a website, and I need almost every page to connect to a database. I had the idea of putting the connection details into a function in an include file, and then just calling that function. Sadly, that doesn't work. When I check my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
[php]<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/php]
Here is the error from the Apache log:
[code]
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php[/code]
I've tried searching on Google for solutions, and I've found alternates to what I am doing...However I'm posting here because I'd really just like to learn why what I'm doing is wrong.
I'd appreciate any answerk my error logs in Apache I am told that $dbh does not exist.
Here is what I have in my include file:
[php]<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/php]
Here is the error from the Apache log:
[code]
[Fri Jul 08 22:48:40 2005] [error] [client 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
[client 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
[client 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php[/code]
I've tried searching on Google for solutions, and I've found alternates to what I am doing...However I'm posting here because I'd really just like to learn why what I'm doing is wrong.
I'd appreciate any answer!
Thanks in advance,
Abs
P.S.: Much of the connection code is from a book that I'm learning from, so I'm sorry if it seems a bit sloppy.<?php
function dbconnect() {
# Set the Connection Variables
$host = "localhost";
$username = "dbusername";
$password = "dbpassword";
$database = "dbname";
# Open the database
@ $dbh = new mysqli($host, $username, $password, $database);
# Check to see if connection failed. If true, print error page then exit.
if (mysqli_connect_errno() ) {
$errorhandling;
exit;
}
}
[/php]
In the actual page that users see, here is the part where I try to connect to the database:
[php]
<?php
# Include file
include('./include.php');
# Run the function from the include file
dbconnect()
# Set the query and run it
$query = "SELECT * FROM $db WHERE user='$user'";
$result = $dbh->query($query);
$row = $result->fetch_object($result);
# Print the results
echo $row->username;
[/php]
Here is the error from the Apache log:
[code]
їFri Jul 08 22:48:40 2005] їerror] їclient 127.0.0.1] File does not exist: C:/Apache2/htdocs/favicon.ico
їclient 127.0.0.1] PHP Notice: Undefined variable: dbh in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.php
їclient 127.0.0.1] PHP Fatal error: Call to a member function query() on a non-object in C:\\Apache2\\htdocs\\controlpanel\\index.php on line 190, referer: http://localhost/controlpanel/index.phpI'd appreciate any answer!
Thanks in advance,
Abs
P.S.: Much of the connection code is from a book that I'm learning from, so I'm sorry if it seems a bit sloppy.