PHP/mysqli: Inside a function

With PHP, how can I use mysqli inside a function?

This works:
 $db = new mysqli($hostname, $username, $password, $database) or die(mysqli_error($db));
 $sql = "SELECT `user_name` FROM `users` WHERE `user_id` = '". mysqli_real_escape_string($db, 9 ) ."' LIMIT 1";
 $result = $db->query($sql);
 $row = $result->fetch_assoc();
 $result->free();
 return $row['user_name'];

Open in new window

But this does not:
 $db = new mysqli($hostname, $username, $password, $database) or die(mysqli_error($db));
 echo user_name(1);
 function user_name($id) {
  global $db;
  $sql = "SELECT `user_name` FROM `users` WHERE `user_id` = '". mysqli_real_escape_string($db, $id ) ."' LIMIT 1";
  $result = $db->query($sql);
  $row = $result->fetch_assoc();
  $result->free();
  return $row['user_name'];
 }

Open in new window

LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
I would rather pass the $db in as a parameter to the function.

Global variables are generally not a good idea

 $db = new mysqli($hostname, $username, $password, $database) or die(mysqli_error($db));
 echo user_name($db, 1);
 function user_name($db, $id) {
  $sql = "SELECT `user_name` FROM `users` WHERE `user_id` = '". $db->real_escape_string($db, $id ) ."' LIMIT 1";
  $result = $db->query($sql);
  $row = $result->fetch_assoc();
  $result->free();
  return $row['user_name'];
 }

Open in new window

0
Dave BaldwinFixer of ProblemsCommented:
What does return $row['user_name']; do in the first instance?
0
Julian HansenCommented:
I tested your code and it works fine on my side - what error are you getting?

Also, any reason you did not call the OOP version of real_escape_string?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

skijAuthor Commented:
I get these errors:
Notice: Undefined variable: crm_db 
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given 
Notice: Undefined variable: crm_db in /usr/home/global777/public_html/courseware/crm/includes/config.php on line 215
Fatal error: Call to a member function query() on a non-object

Open in new window

I think this is an OOP issue.  I don't know how to call the OOP version.
0
Ray PaseurCommented:
This article shows how to use the OOP version of MySQLi
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

You want to make an instance of the MySQLi object and assign the object to a variable.  You pass this variable into the functions (methods) you use in your script.  This is called dependency injection, because your script depends on the MySQLi object.  By injecting the dependency, instead of using global or hard-coding, you create a script that can use a mock object for automated testing.  This is virtually a requirement for professional PHP development these days.  Without such a dependency injection, you would be forced to test on your live data base - not a good idea at all!

Some more related info here:
http://www.experts-exchange.com/articles/18329/SOLID-Design-in-PHP-Applications.html
http://www.experts-exchange.com/articles/18210/Software-Design-Dependencies.html
0
Julian HansenCommented:
That error does not appear to be related to the code you posted - it refers to crm_db which is not in the listing you provide - you need to look where the error is pointing you to (config.php line 215)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
For some more background on this issue, you might want to see the question and the dialog here:
http://www.experts-exchange.com/questions/28707487/GLOBALS.html

It inspired this article, which may be helpful as you think about application design:
http://www.experts-exchange.com/articles/19999/PHP-Design-Avoiding-Globals-with-Dependency-Injection.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.