473,221 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Connect & Disconnetcting functions

Hi there,

I'm building a DB-driven site, and i have one page (settings.php) that
is included on each viewable page. In settings.php i have a few
functions, so i do not have to type them all, or modify them all if
anything changes..

For the same reason i want to create a function to connect to a mySQL
DB.
What i have is:

###############################################
function doConnect(){
$db = mysql_connect("host", "user", "pass");
mysql_select_db("data",$db);
};

function doDisConnect(){
mysql_close($db);
};
###############################################

And then i want to run them from a page where settings.php is
included:

_______________________________________________
doConnect();

$get_user = mysql_query("SELECT id, customerID, hits FROM
customerID WHERE customerID='$user'",$db);

if ($data = mysql_fetch_array($get_user)) {
$sql = "UPDATE customerID SET hits = hits + 1 WHERE
customerID ='$user'";
}else{
$sql = "INSERT INTO customerID (customerID, hits) VALUES
('$user', 1)";
};
$addhit = mysql_query($sql);

doDisConnect();
_______________________________________________

this function is a counter, but that isn't really important. Why can't
i get this to work? Or should i do it totally different?

I'd like to find out now, before i have used it in a lot of pages...

Thanks in advance. Greetings knoakske
Jul 17 '05 #1
14 1664
knoak wrote:
Hi there,

I'm building a DB-driven site, and i have one page (settings.php) that
is included on each viewable page. In settings.php i have a few
functions, so i do not have to type them all, or modify them all if
anything changes..

For the same reason i want to create a function to connect to a mySQL
DB.
What i have is:

###############################################
function doConnect(){
$db = mysql_connect("host", "user", "pass");
mysql_select_db("data",$db);
};

function doDisConnect(){
mysql_close($db);
};
###############################################
the value of the variable $db is most likely local to the function
doConnect() and will therefore not be available to the mysql_query call.
you could either make $db global in scope, or make its value the return
value of doConnect().

sadara

And then i want to run them from a page where settings.php is
included:

_______________________________________________
doConnect();

$get_user = mysql_query("SELECT id, customerID, hits FROM
customerID WHERE customerID='$user'",$db);

if ($data = mysql_fetch_array($get_user)) {
$sql = "UPDATE customerID SET hits = hits + 1 WHERE
customerID ='$user'";
}else{
$sql = "INSERT INTO customerID (customerID, hits) VALUES
('$user', 1)";
};
$addhit = mysql_query($sql);

doDisConnect();
_______________________________________________

this function is a counter, but that isn't really important. Why can't
i get this to work? Or should i do it totally different?

I'd like to find out now, before i have used it in a lot of pages...

Thanks in advance. Greetings knoakske

Jul 17 '05 #2
hi knoak

your code??????
--------------------------------------------------------------
if ($data = mysql_fetch_array($get_user)) {
--------------------------------------------------------------
you may forget == to be
if ($data == mysql_fetch_array($get_user)) {
---------------------------------------------------------------

but i have to tell you about very practical method in your situation ,
i think if you have very large project or more than 5 PHP files you may
use the MySQL Class for connectivity and query purposes check this site
and you will find a lot of free MySQL Classes

http://www.weberdev.com/get_example-3733.html (better one i think)
http://www.weberdev.com/get_example-1505.html
http://www.weberdev.com/get_example-1645.html

tell me about the result.

Jul 17 '05 #3

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@posting.google.c om...
Hi there,

I'm building a DB-driven site, and i have one page (settings.php) that
is included on each viewable page. In settings.php i have a few
functions, so i do not have to type them all, or modify them all if
anything changes..

For the same reason i want to create a function to connect to a mySQL
DB.
What i have is:

###############################################
function doConnect(){
$db = mysql_connect("host", "user", "pass");
mysql_select_db("data",$db);
};

It's a good idea to implement some error checking in here. For example:

$db = mysql_connect("host", "user", "pass") or die(mysql_error());

Also, mysql_select_db() doesn't need the $db argument, since it defaults to
the last thing opened by mysql_connect().
function doDisConnect(){
mysql_close($db);
};
###############################################

And then i want to run them from a page where settings.php is
included:

_______________________________________________
doConnect();

$get_user = mysql_query("SELECT id, customerID, hits FROM
customerID WHERE customerID='$user'",$db);
You don't need to include the $db here if you are using mysql_select_db()
in doConnect. mysql_query() will default to that.
if ($data = mysql_fetch_array($get_user)) {
$sql = "UPDATE customerID SET hits = hits + 1 WHERE
customerID ='$user'";
}else{
$sql = "INSERT INTO customerID (customerID, hits) VALUES
('$user', 1)";
};
$addhit = mysql_query($sql);

doDisConnect();
_______________________________________________

this function is a counter, but that isn't really important. Why can't
i get this to work? Or should i do it totally different?

I'd like to find out now, before i have used it in a lot of pages...

Thanks in advance. Greetings knoakske


Hopefully that helps. If you still have problems, post what exactly is
going wrong so we can help diagnose the problem and propose more meaningful
solutions.
Jul 17 '05 #4
badr wrote:
hi knoak

your code??????
--------------------------------------------------------------
if ($data = mysql_fetch_array($get_user)) {
--------------------------------------------------------------
you may forget == to be
if ($data == mysql_fetch_array($get_user)) {
---------------------------------------------------------------


Normally your comment about = and == is useful. But this is not the
case. The original post wants to make the assignment and then test the
result, thus having the $data available inside the if {} block.

<snip>
Jul 17 '05 #5

"badr" <DW***@msn.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
hi knoak

your code??????
--------------------------------------------------------------
if ($data = mysql_fetch_array($get_user)) {
--------------------------------------------------------------
you may forget == to be
if ($data == mysql_fetch_array($get_user)) {
---------------------------------------------------------------
Badr, that's incorrect. What he had works correctly (on that line). The
idea is to call mysql_fetch_array, and put the return value in $data. If
the return value == TRUE (or, !== FALSE) then continue in the loop. It's a
shorthand way of saying:

$data = mysql_fetch_array($get_user);
if($data)
{
//do stuff
}
but i have to tell you about very practical method in your situation ,
i think if you have very large project or more than 5 PHP files you may
use the MySQL Class for connectivity and query purposes check this site
and you will find a lot of free MySQL Classes

http://www.weberdev.com/get_example-3733.html (better one i think)
http://www.weberdev.com/get_example-1505.html
http://www.weberdev.com/get_example-1645.html

tell me about the result.


A class is one way to do it, but not really necessary. If you want other
features (such as remembering queries, caching results, etc.) you may want
to go a bit more in-depth. However, with what you need (just a start
connection and end connection), you really don't need to make a class (or
use any of the ones linked to by Badr - they aren't all that helpful).

Jul 17 '05 #6
"Sadara" <sa*********@NOWAYdds.nl> wrote in message
news:41**********************@news.xs4all.nl...
knoak wrote:
Hi there,

I'm building a DB-driven site, and i have one page (settings.php) that
is included on each viewable page. In settings.php i have a few
functions, so i do not have to type them all, or modify them all if
anything changes..

For the same reason i want to create a function to connect to a mySQL
DB.
What i have is:

###############################################
function doConnect(){
$db = mysql_connect("host", "user", "pass");
mysql_select_db("data",$db);
};

function doDisConnect(){
mysql_close($db);
};
###############################################


the value of the variable $db is most likely local to the function
doConnect() and will therefore not be available to the mysql_query call.
you could either make $db global in scope, or make its value the return
value of doConnect().


Not "most likely." It IS local to the function.
Jul 17 '05 #7
Chung Leong wrote:
"Sadara" <sa*********@NOWAYdds.nl> wrote in message
news:41**********************@news.xs4all.nl...
knoak wrote:
Hi there,

I'm building a DB-driven site, and i have one page (settings.php) that
is included on each viewable page. In settings.php i have a few
functions, so i do not have to type them all, or modify them all if
anything changes..

For the same reason i want to create a function to connect to a mySQL
DB.
What i have is:

############################################# ##
function doConnect(){
$db = mysql_connect("host", "user", "pass");
mysql_select_db("data",$db);
};

function doDisConnect(){
mysql_close($db);
};
############################################# ##


the value of the variable $db is most likely local to the function
doConnect() and will therefore not be available to the mysql_query call.
you could either make $db global in scope, or make its value the return
value of doConnect().

Not "most likely." It IS local to the function.


it's not possible to see from the code quoted whether the variable has
been declared elsewhere with global scope.
Jul 17 '05 #8
Sadara wrote:
Chung Leong wrote:
"Sadara" <sa*********@NOWAYdds.nl> wrote in message
news:41**********************@news.xs4all.nl...
knoak wrote:

Hi there,

I'm building a DB-driven site, and i have one page (settings.php) that
is included on each viewable page. In settings.php i have a few
functions, so i do not have to type them all, or modify them all if
anything changes..

For the same reason i want to create a function to connect to a mySQL
DB.
What i have is:

###############################################
function doConnect(){
$db = mysql_connect("host", "user", "pass");
mysql_select_db("data",$db);
};

function doDisConnect(){
mysql_close($db);
};
###############################################
the value of the variable $db is most likely local to the function
doConnect() and will therefore not be available to the mysql_query call.
you could either make $db global in scope, or make its value the return
value of doConnect().


Not "most likely." It IS local to the function.

it's not possible to see from the code quoted whether the variable has
been declared elsewhere with global scope.


In that case the variable $db in the doConnect() function still has
nothing to do with the global $db. If you want global variables in your
functions you need to explicitly declare them as global in every
function where you want to use them:

$db = something;

function a() {
global $db;
... $db ... // global $db
}

function b() {
global $db;
... $db .... // global $db
}

function c() {
... $db ... // local $db
}

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #9
Jan Pieter Kunst wrote:
Sadara wrote:
Chung Leong wrote:
"Sadara" <sa*********@NOWAYdds.nl> wrote in message
news:41**********************@news.xs4all.nl...

knoak wrote:

> Hi there,
>
> I'm building a DB-driven site, and i have one page (settings.php) that
> is included on each viewable page. In settings.php i have a few
> functions, so i do not have to type them all, or modify them all if
> anything changes..
>
> For the same reason i want to create a function to connect to a mySQL
> DB.
> What i have is:
>
> ###############################################
> function doConnect(){
> $db = mysql_connect("host", "user", "pass");
> mysql_select_db("data",$db);
> };
>
> function doDisConnect(){
> mysql_close($db);
> };
> ###############################################

the value of the variable $db is most likely local to the function
doConnect() and will therefore not be available to the mysql_query
call.
you could either make $db global in scope, or make its value the return
value of doConnect().


Not "most likely." It IS local to the function.


it's not possible to see from the code quoted whether the variable has
been declared elsewhere with global scope.

In that case the variable $db in the doConnect() function still has
nothing to do with the global $db. If you want global variables in your
functions you need to explicitly declare them as global in every
function where you want to use them:

$db = something;

function a() {
global $db;
... $db ... // global $db
}

function b() {
global $db;
... $db .... // global $db
}

function c() {
... $db ... // local $db
}

JP


thanks for correcting me.
Jul 17 '05 #10
I thought globals were taboo?

Jul 17 '05 #11
.oO(iMedia)
I thought globals were taboo?


Why?

Micha
Jul 17 '05 #12
function dbConn(){
$db = @mysql_connect("user","pass","host");
if(!$db){
echo "Mysql Connect error";
exit;
}
if(!@mysql_select_db("db_name")){
echo "DB select Error";
exit;
}
return $db;
}

function dbQuery($query){
// you can put many security checks into your code easily
// by making it a new function, along with easy error checking
$db = dbConn();
if($db){
$s =@ mysql_query($query,$db);
if(!$s){
echo "Error on query";
exit;
}
return mysql_result($s);
}
}

this is the setup i use.. its quite easy and i find it faster to type
dbQuery instead of mysql_query.. plus this adds better security on
your query functions, and better error checking. you obiously should
add your own modifications to this code, like maybe line checking
(__LINE__) or file (__FILE__) or somthing.. but this causes the db
connection to be returned.

This might, probably does acctually.. make your connections keep
opening, so what you can do is just use the disconnect code like you
have after all querys are done.

Jul 17 '05 #13
I wish I could say exactly. I'm fairly new to PHP. I read a few months
ago that globals are now considered insecure and bad practice. Is this
true?

Jul 17 '05 #14
.oO(iMedia)
I wish I could say exactly. I'm fairly new to PHP. I read a few months
ago that globals are now considered insecure and bad practice. Is this
true?


You're probably talking about register_globals, which indeed can become
a security risk if not used properly.

Micha
Jul 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mr Dygi | last post by:
Hi, PHP 4.3.4 installed manually from package *.zip and Apache 2. I have a problem with this simple code: <?php $link = mysql_connect("127.0.0.1","","") or die("Could not connect: " ....
0
by: Michael Reetz | last post by:
My Web server is running on Suse with apache2 and PHP 5.0.2. The msession server (Version 1.21) works perfectly locally using the provided tools (/usr/local/phoenix/bin/). For the browser test I...
2
by: Gazchurchend | last post by:
I am trying to connect to a legacy system running on VMS using the Attunity Connect ODBC driver from within PHP. I know the System DSN works because SQL Server has been using it successfully for...
5
by: T. Wintershoven | last post by:
Hello, Although i write programs in VB for a while now, i've allways used the datacontrol and MS-Access DBs for data storage and never (and i mean never) used SQL. I lookt in many sample...
5
by: Edward K. Ream | last post by:
Am I reading pep 277 correctly? On Windows NT/XP, should filenames always be converted to Unicode using the mbcs encoding? For example, myFile = unicode(__file__, "mbcs", "strict") This...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
5
by: Kona | last post by:
Hello, Is it right to tell that DB2 Connect has the same function that Oracle Net ? If I have an ODBC application is it also right to tell that I have 2 possibilities on my client workstation to...
2
by: Geoff | last post by:
In testing our app, we connected and disconnected like 1000 times and noticed a 5 meg memory leak. The sequence of connecting and disconnecting is as follows: 1. OdbcSQLAllocHandle 2. ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.