Forum Moderators: coopster

Message Too Old, No Replies

What is wrong with my database connections

         

Jamier101

11:26 am on May 29, 2011 (gmt 0)

10+ Year Member



Hi,

I seem to have a problem with connection to my database when using an include file, the basic setup is as below.


<?php

require_once("connections/connection.php"); // Connection to the server

$tbl_name="users"; // Define the table name

session_start();

$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userid && $password){
/////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM users WHERE username='$userid' and password='$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////
}

if ($rowAccount){

$_SESSION['id'] = $rowAccount['username'];

header("location:index.php");
exit;

}elseif($submitted){

echo "Incorrect username or password";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>

<body>
<body>
<div id="wrapper">
<div id="container">
<div id="header">
<div id="user_box">
<?php
if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
echo "<font color='white'>" . 'Logged in as '.$rowAccount['username'] . "</font>";
echo '<input type="button" name="logout" value="Logout" onclick="document.location.href=\'logout.php\'"/>';
}
?>
</div>
</div>
<div id="navigation">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="#">Search</a></li>

</ul>
</div>
<div id="spacer_horizontal"></div>
<div id="left_panel"><img src="images/panel.jpg" width="180" height="790" /></div>
<div id="centre_panel">
<?php

//Turn error reporting on, this will help loads when your developing!
error_reporting(E_ALL);

require_once("connections/connection.php"); // Connection to the server

//Catch the form being processed
if(isset($_POST['submit']) && ($_POST['submit'] == "Submit")){

//$host="localhost"; // Host name
//$username="root"; // Mysql username
//$password="password"; // Mysql password
//$db_name="database1"; // Database name
$tbl_name="table2"; // Table name

// Connect to server and select database.
$conn = mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");

...etc


Now, I've chopped out most of my code but the problem seems to lie within the second connection, if I used a require_once script and then declare my table name I get 'cannot connect' on load however if I take out the require once and use:
//$host="localhost"; // Host name 
//$username="root"; // Mysql username
//$password="password"; // Mysql password
//$db_name="database1"; // Database name

then everything works, why is this?

agent_x

5:05 pm on May 30, 2011 (gmt 0)

10+ Year Member



I'm going to hazard a guess here and say that connection.php contains the code to connect to your database. In which case, it looks like you're doing it two more times than you need to. You only need to connect to the database once, so the include at the top of your script should be enough (unless you are dealing with more than one database but I presume you're not).

Jamier101

10:27 pm on May 30, 2011 (gmt 0)

10+ Year Member



No, I am only using one database so I should use the connection script at the top and although I close the PHP tags I do not need to use the connection script when I open them again?

agent_x

11:10 pm on May 30, 2011 (gmt 0)

10+ Year Member



No, regardless of whether the text is PHP or HTML, the PHP parser is still processing it. It only closes the connection when you exit the script or it reaches the end of file, although it's good practice to close the connection yourself before the end or exit of your script.

Jamier101

11:29 pm on May 30, 2011 (gmt 0)

10+ Year Member



Okay, thanks. That seems to make sense, I will give it a go in the morning and see where I end up :-)

Matthew1980

4:23 pm on May 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello all!

I only have one thing to point out here and that is the use of error suppressing (@) try no to use this is it isn't the right thing to do when trying to achieve 'good practise' when programming php.

Surely when coding, you would like to know where your imperfections are occurring so that you can code them out and learn in the process.

In the context of your script, the connection reference should be in the include file, and then only referred to once after that because the natural way for mysql_ type functions is to 'piggy back' the last known connection instance OR try to create a new one.

An personally, I wouldn't advocate using mysql_close() at all, just design the software so that the natural closing of the connection happens when the execution of the script is finished. Though everyone is different.

I personally would look at what you are trying to get from this application and try to refine your logic a little - that will solve a few issues - and DATA SANITISING! I cannot emphasize that enough.

Anyway, I received your PM and am in the process of doing something for you. I shall drop you a line when I have something of use.

Cheers,
MRb