28-10-2004, 22:30
|
#1
|
|
Inactive
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
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! 
|
|
|
28-10-2004, 22:47
|
#2
|
|
Inactive
Join Date: Jun 2003
Age: 45
Posts: 14,750
|
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!  
|
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
|
|
|
28-10-2004, 22:49
|
#3
|
|
Inactive
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
Re: Small PHP query
OOoo Excellent thanx very much Punky, one rep coming your way
|
|
|
28-10-2004, 23:04
|
#4
|
|
Inactive
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
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?
|
|
|
28-10-2004, 23:08
|
#5
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 47
Posts: 6,343
|
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>';
|
|
|
28-10-2004, 23:12
|
#6
|
|
Inactive
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
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?
|
|
|
28-10-2004, 23:22
|
#7
|
|
Inactive
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
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
|
|
|
29-10-2004, 00:01
|
#8
|
|
Guest
|
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??
|
|
|
|
29-10-2004, 00:05
|
#9
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 47
Posts: 6,343
|
Re: Small PHP query
Weird that, I run 1280x1024 and it's fine for me.
|
|
|
29-10-2004, 09:34
|
#10
|
|
Inactive
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
Re: Small PHP query
im running 1280x1024 and theyre too wide for the Window, what browsers are we using?
IE here.
|
|
|
29-10-2004, 12:49
|
#11
|
|
Inactive
Join Date: Jun 2003
Location: Warrington ntl:81304 Altitude: 12m (and falling)
Posts: 4,499
|
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
|
|
|
29-10-2004, 13:03
|
#12
|
|
Dr Pepper Addict
Cable Forum Admin
Join Date: Oct 2003
Location: Nottingham
Age: 63
Services: Zen & YouFibre FTTP, Sky Q TV, Sky Mobile, Flextel SIP
Posts: 31,387
|
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.
__________________
Baby, I was born this way.
|
|
|
29-10-2004, 13:13
|
#13
|
|
Guest
|
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.
|
|
|
|
29-10-2004, 13:23
|
#14
|
|
Dr Pepper Addict
Cable Forum Admin
Join Date: Oct 2003
Location: Nottingham
Age: 63
Services: Zen & YouFibre FTTP, Sky Q TV, Sky Mobile, Flextel SIP
Posts: 31,387
|
Re: Small PHP query
Sadly, the fix I was thinking of isn't for this, and looks decidedly buggy anyway.
__________________
Baby, I was born this way.
|
|
|
29-10-2004, 18:23
|
#15
|
|
Guest
|
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
|
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 08:42.
|