473,320 Members | 2,098 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,320 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 1670
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. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.