|
Posted by Jerry Stuckle on 11/15/75 11:27
news@celticbear.com wrote:
> Jerry Stuckle wrote:
>
>>news@celticbear.com wrote:
>>
>>Since you're using a class (hooray!), you could open the connection in
>>the constructor to your class and store it in a member variable of the
>>class.
>>
>
> I tried that, evidently incorrectly, because each time the functions
> within the class would run, they couldn't "see" that established
> connection and the queries wouldn't work.
> I tried assigning those three lines to fariables...but that so doesn't
> work. Another person mentioned making it into a function...which sounds
> like a great idea! I just need to figure it out.
>
It's pretty simple - all you need to do is return the connection
variable from the function.
PHP Functions are generally their own little blocks of code. That is,
if you define $dbconn in one function, no one outside of that function
can see it (unless you make it global - which is another story). This
is good, because it means you don't have to worry about two functions
having two different variables with the same name.
You could do something like this:
function connect() {
$connection = mysql_connect(......);
$db = mysql_select_db(....);
return $connection;
}
To call:
$myconn = connect();
Of course, you will want to add error handling, you might want to pass a
variable (the db name) to the function, or whatever. But hopefully it
will get you on the right track.
>
> I do that for all my PHP pages, have those three database est. lines at
> the top. But, and I guess I'm just inexperienced enough to not know how
> a SQL querey inside a function can "see" the database connection
> outside it, because every time I try, I get SQL errors.
> That's why I'm calling the include twice, once in each function,
> because I can't seem to make it fisible to the inside of the function
> otherwise.
>
You can also pass the connection as a parameter to a function.
You need to read up on functions more. They are at the heart of the PHP
language. Basically, you can pass any number of parameters to a
function, and use them inside of that function. You can return one
parameter from the function and use it outside of the function.
(Actually, this isn't *quite* correct - you can use references when
passing parameters to the function and effectively get more than one
value returned. But for now don't worry about that end - just log it in
the back of your mind and come back to it later).
>
>
> So, how would that work?
> Something like:
>
> function db_connect() {
> include('../Connections/userDBconnect.php');
> $db = mysql_select_db("printing", $connection) or $error .=
> mysql_error();
> $dbconn = $connection;
> }
> function test_sql() {
> $sql_Fi = "SELECT email FROM tbl_users WHERE dept = '5' AND
> active='1'";
> $result_Fi = @mysql_query($sql_Fi, db_connect());
> }
>
> No, I know that won't work. OH! Like:
>
> include('../Connections/userDBconnect.php');
> $db = mysql_select_db("printing", $connection) or $error .=
> mysql_error();
> $dbconn = $connection;
>
> function db_connect($sql,$dbconn) {
> return @mysql_query($sql, $dbconn);
> }
> function test_sql() {
> $sql_Fi = "SELECT email FROM tbl_users WHERE dept = '5' AND
> active='1'";
> $result = db_connect($sql_Fi,$dbconn);
> while ($row = mysql_fetch_array($result)) {
> ... etc
> }
> }
>
Not quite. Something like:
(See the connect() function above)
function test_sql($conn) {
$sql_Fi = "....";
$result = mysql_query($sql_Fi, $conn);
while ($row = mysql_fetch_array($result)) {
...
}
$dbconn = connect();
test_sql($dbconn);
Remember - code in a function is not executed until you call the function.
Here, connect() opens the connection and returns the result
($connection) to the caller. test_sql() takes one parameter which will
be the connection. It makes the call to mysql and does whatever.
After the functions themselves is the main code. The first line calls
connect() to make the connection and stores the result in $dbconn. The
main code then passes $dbconn to test_sql, which uses the value to
perform the mysql query.
Now - in connect(), this value is known as $connection. In the main
code it is $dbconn, and in test_sql it's $conn. But they are all three
the same thing - just under different names. I normally don't do it
this way (I use some standard naming conventions I've developed for my
code) - but here I did it to show you that names between functions and
your main code are *not* important. They're just a way of referring to
the value *in that part of the code*. And in connect(), $connection has
no relationship to *any* other variable of the same name *anywhere* else
in the code. The only relationship is the one created by returning
$connection from the function and the statement:
$dbconn = connect();
> I'll try that... but, $dbconn, to be used in test_sql() to be sent to
> db_connect, needs to be sent TO test_sql()....
> OK, brain melting.
> If you have any good web sites that help in this manner, I'd appreciate
> it. =)
> Thanks for the reply!
> Liam
>
It's kinda complicated until you get the hang of it. Just think of a
function as being closed box with a little guy and an adding machine
inside. You can push a slip with a bunch of numbers into the box. When
the little guy is done, he can pass back out a slip with a single number
(the total) back out to you. But you can't have any interaction with
him other than that slip of paper, and vice versa.
This is how a function works. You can pass any number of values
(parameters) into the function, which the function can then use. And
the function can return exactly one value back for you to use.
Sorry, I don't know of any good websites - haven't looked for one for a
while. But you should be able to find something by googling for PHP
tutorial.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|