MishieMoo Posted December 25, 2008 Share Posted December 25, 2008 Hello all! I'm trying to figure out if there's a way to access a mysqli connection inside a function without reconnecting inside the function, without using globals. Here's a code example if what I'm trying to get working without globals: <?php $connect=@mysqli_connect("localhost", $dbuser, $dbpword, $dbname) or trigger_error("There was an error: ".mysqli_connect_error()); function checkOne($query){ $run = mysqli_query($connect, $query) or trigger_error(mysqli_error($connect)); $found = mysqli_num_rows($run); if($found<1) return false; else return true; } ?> It's very simple but I'm looking to save some time/space with a few of these and I'd love to be able to use that connection in my functions. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/138400-solved-using-mysqli-in-a-php-function/ Share on other sites More sharing options...
tomfmason Posted December 25, 2008 Share Posted December 25, 2008 I would do it like this: <?php class Foo { private $connect; private $dbuser = ""; private $dbpword = ""; private $dbname = "": function __construct(){ $this->connect = @mysqli_connect("localhost", $this->dbuser, $this->dbpword, $this->dbname) } function checkOne($query){ $run = mysqli_query($this->connect, $query) or trigger_error(mysqli_error($this->connect)); $found = mysqli_num_rows($run); if($found<1) return false; else return true; } } ?> Alternatively you could use global but, imho, that is messy. Link to comment https://forums.phpfreaks.com/topic/138400-solved-using-mysqli-in-a-php-function/#findComment-723631 Share on other sites More sharing options...
MishieMoo Posted December 25, 2008 Author Share Posted December 25, 2008 Okay three things. Firstly, is there a way to do that that isn't OOP? My OOP skills aren't very good and I'm trying to make everything consistent. Secondly, if I call that function outside of the class would it still work? Thirdly, that connection is actually established on a different, included page. I'm trying to only have one connection and not create another to use specifically for the functions. I guess I didn't clarify what I was trying to do before. Thanks so much =) Link to comment https://forums.phpfreaks.com/topic/138400-solved-using-mysqli-in-a-php-function/#findComment-723633 Share on other sites More sharing options...
tomfmason Posted December 25, 2008 Share Posted December 25, 2008 Firstly, is there a way to do that that isn't OOP? My OOP skills aren't very good and I'm trying to make everything consistent. I would suggest that you learn more about OOP. It isn't really all that difficult, as long as you don't over complicate it, and it should drastically cut down on repetitive/redundant code. Secondly, if I call that function outside of the class would it still work? You could easily do something like this <?php class Foo { public $connect; private $dbuser = ""; private $dbpword = ""; private $dbname = "": function __construct(){ $this->connect = @mysqli_connect("localhost", $this->dbuser, $this->dbpword, $this->dbname) } function checkOne($query){ $run = mysqli_query($this->connect, $query) or trigger_error(mysqli_error($this->connect)); $found = mysqli_num_rows($run); if($found<1) return false; else return true; } } $foo = new Foo(); $foo->connect; //your database connection ?> When I am not using a framework with an ORM I use a modified version of http://www.tgreer.com/class_db_php.html for database interaction. Thirdly, that connection is actually established on a different, included page. I'm trying to only have one connection and not create another to use specifically for the functions. I guess I didn't clarify what I was trying to do before. That is why I suggest that you look more into OOP. It may seem like a more difficult route to go now but in the long run it will save you a lot of time and headaches. Link to comment https://forums.phpfreaks.com/topic/138400-solved-using-mysqli-in-a-php-function/#findComment-723642 Share on other sites More sharing options...
corbin Posted December 25, 2008 Share Posted December 25, 2008 Firstly, is there a way to do that that isn't OOP? My OOP skills aren't very good and I'm trying to make everything consistent. I would suggest that you learn more about OOP. It isn't really all that difficult, as long as you don't over complicate it, and it should drastically cut down on repetitive/redundant code. Secondly, if I call that function outside of the class would it still work? You could easily do something like this <?php class Foo { public $connect; private $dbuser = ""; private $dbpword = ""; private $dbname = "": function __construct(){ $this->connect = @mysqli_connect("localhost", $this->dbuser, $this->dbpword, $this->dbname) } function checkOne($query){ $run = mysqli_query($this->connect, $query) or trigger_error(mysqli_error($this->connect)); $found = mysqli_num_rows($run); if($found<1) return false; else return true; } } $foo = new Foo(); $foo->connect; //your database connection ?> When I am not using a framework with an ORM I use a modified version of http://www.tgreer.com/class_db_php.html for database interaction. Thirdly, that connection is actually established on a different, included page. I'm trying to only have one connection and not create another to use specifically for the functions. I guess I didn't clarify what I was trying to do before. That is why I suggest that you look more into OOP. It may seem like a more difficult route to go now but in the long run it will save you a lot of time and headaches. <offtopic degree="extremely"> Sorry for the off topic ness, but I had never thought of caching queries with a TTL before.... I had thought about caching before, but hadn't been able to think of how to know if the data were still current or not (without using MySQL caching, of course). I guess for some of my data, a TLL would work fine, so I think I may add that to my class later. Just wanted to say I like the class you linked to lol.... </offtopic> Link to comment https://forums.phpfreaks.com/topic/138400-solved-using-mysqli-in-a-php-function/#findComment-723650 Share on other sites More sharing options...
MishieMoo Posted December 25, 2008 Author Share Posted December 25, 2008 Thanks! I'll look into OOP for my next project. This one is too close to completion for me to implement it now. Link to comment https://forums.phpfreaks.com/topic/138400-solved-using-mysqli-in-a-php-function/#findComment-723660 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.