Forum Articles
  Welcome back Join CF
You are here You are here: Home | Forum | searchweb2 - unable to remove!!

You are currently viewing our boards as a guest which gives you limited access to view most of the discussions, articles and other free features. By joining our Virgin Media community you will have full access to all discussions, be able to view and post threads, communicate privately with other members (PM), respond to polls, upload your own images/photos, and access many other special features. Registration is fast, simple and absolutely free so please join our community today.


Welcome to Cable Forum
Go Back   Cable Forum > Computers & IT > General IT Discussion
Register FAQ Community Calendar

Small PHP query
Reply
 
Thread Tools
Old 28-10-2004, 22:30   #1
Graham M
Inactive
 
Graham M's Avatar
 
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+ Tivo V-Box VM 60MBit
Posts: 13,365
Graham M has a pair of shiny starsGraham M has a pair of shiny stars
Graham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny stars
Send a message via MSN to Graham M Send a message via Yahoo to Graham M
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!
Graham M is offline   Reply With Quote
Advertisement
Old 28-10-2004, 22:47   #2
punky
Inactive
 
Join Date: Jun 2003
Age: 45
Posts: 14,750
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
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
punky is offline   Reply With Quote
Old 28-10-2004, 22:49   #3
Graham M
Inactive
 
Graham M's Avatar
 
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+ Tivo V-Box VM 60MBit
Posts: 13,365
Graham M has a pair of shiny starsGraham M has a pair of shiny stars
Graham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny stars
Send a message via MSN to Graham M Send a message via Yahoo to Graham M
Re: Small PHP query

OOoo Excellent thanx very much Punky, one rep coming your way
Graham M is offline   Reply With Quote
Old 28-10-2004, 23:04   #4
Graham M
Inactive
 
Graham M's Avatar
 
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+ Tivo V-Box VM 60MBit
Posts: 13,365
Graham M has a pair of shiny starsGraham M has a pair of shiny stars
Graham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny stars
Send a message via MSN to Graham M Send a message via Yahoo to Graham M
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?
Graham M is offline   Reply With Quote
Old 28-10-2004, 23:08   #5
Richard M
Inactive
 
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 47
Posts: 6,343
Richard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze array
Richard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze array
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>'; 
Richard M is offline   Reply With Quote
Old 28-10-2004, 23:12   #6
Graham M
Inactive
 
Graham M's Avatar
 
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+ Tivo V-Box VM 60MBit
Posts: 13,365
Graham M has a pair of shiny starsGraham M has a pair of shiny stars
Graham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny stars
Send a message via MSN to Graham M Send a message via Yahoo to Graham M
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 is offline   Reply With Quote
Old 28-10-2004, 23:22   #7
Graham M
Inactive
 
Graham M's Avatar
 
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+ Tivo V-Box VM 60MBit
Posts: 13,365
Graham M has a pair of shiny starsGraham M has a pair of shiny stars
Graham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny stars
Send a message via MSN to Graham M Send a message via Yahoo to Graham M
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
Graham M is offline   Reply With Quote
Old 29-10-2004, 00:01   #8
homealone
Guest
 
Posts: n/a
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??
  Reply With Quote
Old 29-10-2004, 00:05   #9
Richard M
Inactive
 
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 47
Posts: 6,343
Richard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze array
Richard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze arrayRichard M has a bronze array
Re: Small PHP query

Weird that, I run 1280x1024 and it's fine for me.
Richard M is offline   Reply With Quote
Old 29-10-2004, 09:34   #10
Graham M
Inactive
 
Graham M's Avatar
 
Join Date: Jul 2003
Location: Poole, Dorset
Age: 41
Services: FreeSat+ Tivo V-Box VM 60MBit
Posts: 13,365
Graham M has a pair of shiny starsGraham M has a pair of shiny stars
Graham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny starsGraham M has a pair of shiny stars
Send a message via MSN to Graham M Send a message via Yahoo to Graham M
Re: Small PHP query

im running 1280x1024 and theyre too wide for the Window, what browsers are we using?

IE here.
Graham M is offline   Reply With Quote
Old 29-10-2004, 12:49   #11
altis
Inactive
 
altis's Avatar
 
Join Date: Jun 2003
Location: Warrington ntl:81304 Altitude: 12m (and falling)
Posts: 4,499
altis has a nice shiny staraltis has a nice shiny star
altis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny staraltis has a nice shiny star
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
altis is offline   Reply With Quote
Old 29-10-2004, 13:03   #12
Paul
Dr Pepper Addict
Cable Forum Admin
 
Paul's Avatar
 
Join Date: Oct 2003
Location: Nottingham
Age: 63
Services: Zen & YouFibre FTTP, Sky Q TV, Sky Mobile, Flextel SIP
Posts: 31,387
Paul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered stars
Paul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered stars
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.
Paul is offline   Reply With Quote
Old 29-10-2004, 13:13   #13
homealone
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 29-10-2004, 13:23   #14
Paul
Dr Pepper Addict
Cable Forum Admin
 
Paul's Avatar
 
Join Date: Oct 2003
Location: Nottingham
Age: 63
Services: Zen & YouFibre FTTP, Sky Q TV, Sky Mobile, Flextel SIP
Posts: 31,387
Paul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered stars
Paul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered starsPaul is seeing silvered stars
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.
Paul is offline   Reply With Quote
Old 29-10-2004, 18:23   #15
homealone
Guest
 
Posts: n/a
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
  Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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