Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   General IT Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=19)
-   -   Small PHP query (https://www.cableforum.uk/board/showthread.php?t=19422)

Graham M 28-10-2004 22:30

Small PHP query
 
Im sure this will seem ever so simple to someone but I can't quite hack it for some reason.

I want to implement some functions in a script im using so that I can reuse the functions over and over, BUT, variables created outside of the function are not recognised within the function, is there any way to get around this? Here's an example:

PHP Code:

$host = 'localhost';
$user = 'dbuser';
$password = 'dbpassword';
$dbtouse = 'adatabase';
 
database_connect();
 
function 
database_connect()
{
 
mysql_connect($host,$user,$password);
 
mysql_select_db($dbtouse);
} 

Seems perfectly valid in my eyes but would fail on mysql_connect because it could not read the variables that were set outside of the function. Any ideas?

Thanks in advance! :D:dunce:

punky 28-10-2004 22:47

Re: Small PHP query
 
Quote:

Originally Posted by Zeph
Im sure this will seem ever so simple to someone but I can't quite hack it for some reason.

I want to implement some functions in a script im using so that I can reuse the functions over and over, BUT, variables created outside of the function are not recognised within the function, is there any way to get around this? Here's an example:

PHP Code:

$host = 'localhost';
$user = 'dbuser';
$password = 'dbpassword';
$dbtouse = 'adatabase';
 
database_connect();
 
function 
database_connect()
{
 
mysql_connect($host,$user,$password);
 
mysql_select_db($dbtouse);
} 

Seems perfectly valid in my eyes but would fail on mysql_connect because it could not read the variables that were set outside of the function. Any ideas?

Thanks in advance! :D:dunce:


This is called variable scope. You need to make those variables global to access them inside the function. To do that you need:

PHP Code:

$host = 'localhost';
$user = 'dbuser';
$password = 'dbpassword';
$dbtouse = 'adatabase';
 
database_connect();
 
function 
database_connect()
{

global 
$host, $user, $password, $dbtouse;

 
mysql_connect($host,$user,$password);
 
mysql_select_db($dbtouse);
} 

More can be found here: http://uk.php.net/language.variables.scope

HTH

Graham M 28-10-2004 22:49

Re: Small PHP query
 
OOoo Excellent thanx very much Punky, one rep coming your way :D

Graham M 28-10-2004 23:04

Re: Small PHP query
 
OK that's fixed most of it, what I have now is:

PHP Code:

<?php
function dbconnect()
{
global 
$config;
@
$link = mysql_connect($config['mysql']['host'],$config['mysql']['user'],$config['mysql']['password']) or die("Error: Could Not Connect to MySQL Server" . mysql_error($link));
mysql_select_db($config['mysql']['dbname'],$link);
}
function 
dbclose()
{
global 
$link;
mysql_close($link);
}
?>

And the same doesn't function with the MySQL Link Pointer?
Comes back with
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /var/www/html/includes/mysql.php on line 12


Is it possible to do something similar or not?

Richard M 28-10-2004 23:08

Re: Small PHP query
 
Quote:

Originally Posted by Zeph
OK that's fixed most of it, what I have now is:

PHP Code:

<?php
function dbconnect()
{
global 
$config;
@
$link = mysql_connect($config['mysql']['host'],$config['mysql']['user'],$config['mysql']['password']) or die("Error: Could Not Connect to MySQL Server" . mysql_error($link));
mysql_select_db($config['mysql']['dbname'],$link);
}
function 
dbclose()
{
global 
$link;
mysql_close($link);
}
?>

And the same doesn't function with the MySQL Link Pointer?
Comes back with
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /var/www/html/includes/mysql.php on line 12


Is it possible to do something similar or not?

I think you're doing too much in one statement.
Why not store the db details in a config file and read it in?

EDIT: Like so -

Contents of config.php
PHP Code:

$dbconnect = mysql_connect("localhost", "user", "pass");
$dbname = "database_name";
$table_name = "table_name"; 

To connect and disconnect:
PHP Code:

include_once ('./config.php');
if (!
$dbconnect) {
  echo(
'<p>Unable to connect to the database!</p>' );
  exit;
}
else {
echo 
'<p>Connected!</p>';
}
mysql_close($dbconnect);
echo 
'<p>Disconnected!</p>'; 


Graham M 28-10-2004 23:12

Re: Small PHP query
 
Hmm theres no dbname argument in mysql_close just one for a database link, anyway ive gotten around this by not specifying a link to close which should close the only link open if there's only one open, but I'd still like to know if it can be done?

Graham M 28-10-2004 23:22

Re: Small PHP query
 
I was hoping to make it a bit more modular but it seems that that will probably be the best way. When I finish my project (IF!?) I will post the working version here :)

homealone 29-10-2004 00:01

Re: Small PHP query
 
is there any way to get these script posts to align with horizontal resolution - or is it only me, I have 1280x768 and have to scroll sideways, what's it like for 800x600??

Richard M 29-10-2004 00:05

Re: Small PHP query
 
Weird that, I run 1280x1024 and it's fine for me. :confused:

Graham M 29-10-2004 09:34

Re: Small PHP query
 
im running 1280x1024 and theyre too wide for the Window, what browsers are we using?

IE here.

altis 29-10-2004 12:49

Re: Small PHP query
 
Quote:

Originally Posted by homealone
is there any way to get these script posts to align with horizontal resolution - or is it only me, I have 1280x768 and have to scroll sideways, what's it like for 800x600??

view / decrease text size ;)

Paul 29-10-2004 13:03

Re: Small PHP query
 
The widths are screwed in IE - firefox seems to display it ok still.

I think there is a fix for this - I'll check it out later if I get chance.

homealone 29-10-2004 13:13

Re: Small PHP query
 
Quote:

Originally Posted by Paul M
The widths are screwed in IE - firefox seems to display it ok still.

I think there is a fix for this - I'll check it out later if I get chance.

I didn't try it with Firefox, I will later, when I get home. Its not a massive problem, just a minor inconvenience to have to scroll right in order to read all the posts subsequent to the 'wide' one - and, of course, it resets at the next new page. :)

Paul 29-10-2004 13:23

Re: Small PHP query
 
Sadly, the fix I was thinking of isn't for this, and looks decidedly buggy anyway.

homealone 29-10-2004 18:23

Re: Small PHP query
 
Quote:

Originally Posted by Paul M
Sadly, the fix I was thinking of isn't for this, and looks decidedly buggy anyway.

No worries - it works fine with Firefox, thanks for looking :tu:


All times are GMT. The time now is 08:50.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum