By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

Connect & Disconnetcting functions

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
I thought globals were taboo?

Jul 17 '05 #11

P: n/a
.oO(iMedia)
I thought globals were taboo?


Why?

Micha
Jul 17 '05 #12

P: n/a
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

P: n/a
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

P: n/a
.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 discussion thread is closed

Replies have been disabled for this discussion.