Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to start mysql connection on every page 1

Status
Not open for further replies.

petersJazz

Programmer
Jan 28, 2002
222
EU
This code is the general include.php that I use on all pages:

Code:
...
session_start();
if (!isset($_SESSION['dbuser'])) init();
$_SESSION['dblink']=mysql_connect($_SESSION['dbserver'],$_SESSION['dbuser'],$_SESSION['dbpassword']); 
@mysql_select_db($_SESSION['database'],$_SESSION['dblink']) or die( "Unable to select database");

function init() {
 $_SESSION['dbuser']="*******";
 $_SESSION['dbpassword']="*******";
 $_SESSION['database']="*********";
 $_SESSION['dbserver']="**********";
}
...
Why do I need to make a new connection on every page, I tried to put mysql_connect and mysql_select_db in init() function but it dont work.

regards /Peter
 
instantiate the connection outside the function but below the call to init() (i.e. in the global scope).

make sure you have plenty of error logging.
 
Thanks for the answer but I dont understand it.

Isnt that what I do in the code. But that means that it is done for every call of include.php.
 
typically files like include.php are called only once per connection. usually with "require_once 'include.php';"

so, yes - the code would be performed each time the file was called.

and yes: you are doing what I suggested in your code... sorry.

i can't see the value of storing the database connection resource in a session variable. it cannot be serialised or maintained across sessions so why would you want to do this? personally, i never assign the connection resource to a variable unless I require access to more than one database server in a script. i leave the assignment as implicit and let php handle the rest.

you have not reported that you are getting any error messages so I assume that you are not. This means that either you have error reporting turned off, error display turned off or your code is working (in which case what is the "not working" behaviour?). if you are getting an error message, please post it here. for debugging please delete the @.
 
A more complete example of that I want to do:

include.php
Code:
<?
session_start();
if (!isset($_SESSION['dbuser'])) init();
function init() {
 $_SESSION['dbuser']="*****";
 $_SESSION['dbpassword']="******";
 $_SESSION['database']="*********";
 $_SESSION['dbserver']="**********";
 $_SESSION['dblink']=mysql_connect($_SESSION['dbserver'],$_SESSION['dbuser'],$_SESSION['dbpassword']); 
 mysql_select_db($_SESSION['database'],$_SESSION['dblink']) or die( "Unable to select database");
}
?>

test5.php
Code:
<? 
include("includetest3.php");
$query="SELECT * FROM rklimage";
$result=mysql_query($query,$_SESSION['dblink']);
$num=mysql_numrows($result);

echo("<html><head></head><body>");
while ($i < $num) {
  $picn=mysql_result($result,$i,"picname");
  echo("$picn<br>");
  $i++;
}
echo("</body></html>");
mysql_free_result($result);
?>

The error I get is:


Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /usr/home/rivieraklubben/public_html/rivieraklubben.com/html/test5.php on line 5

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /usr/home/rivieraklubben/public_html/rivieraklubben.com/html/test5.php on line 6

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /usr/home/rivieraklubben/public_html/rivieraklubben.com/html/test5.php on line 15
 
try these two files instead

include.php
Code:
<?
session_start();
if (!isset($_SESSION['dbuser'])) init();
function init() {
 $_SESSION['dbuser']="*****";
 $_SESSION['dbpassword']="******";
 $_SESSION['database']="*********";
 $_SESSION['dbserver']="**********";
} 
mysql_connect(    $_SESSION['dbserver'],
                    $_SESSION['dbuser'],
                    $_SESSION['dbpassword']  )
             or die (mysql_error()); 

mysql_select_db(  $_SESSION['database'],
                  $_SESSION['dblink']  ) 
             or die( "Unable to select database ".mysql_error());

?>

test.php
Code:
<?
require_once "include.php";
$query="SELECT * FROM rklimage";
$result=mysql_query($query) or die (mysql_error());

while ($row=mysql_fetch_assoc($result)){
  echo $row['picname']."<br/>";
}
mysql_free_result($result);
?>
 
Thank you jpadie, this is working just fine now for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top