A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearEnsuring MySQL connection works in PHP function - page 1
User InfoPosts
Ensuring MySQL connection works in PHP function#1
I have code with the following form:

<?php
function doSomething{
//Do stuff with MySQL
$con->tralalala();
}
$con = connectToDatabase;//This would actually be a line or two.
doSomething();
?>


This (type of) code doesn't work, because doSomething() doesn't have a connection to the database. Can anyone explain why not? I create the $con connection before I call doSomething(). So why does the function act as if there's no connection?

Is there any way to fix this, short of passing the connection into the function like doSomething($con)?

posted date: 2008-12-26 10:35:00


Re: Ensuring MySQL connection works in PHP function#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-26 10:35:01


Re: Ensuring MySQL connection works in PHP function#3
you probably need to tell it to look in the global scope: function doSomething() { global $con; $con->tralalala(); }

posted date: 2008-12-26 10:41:00


Re: Ensuring MySQL connection works in PHP function#4
Beautiful. Thanks.

posted date: 2008-12-26 10:45:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.