php - mysqli_query not working when called from inside a function -


here code:

<?php $db_host        = 'localhost'; $db_user        = 'root'; $db_pass        = 'root'; $db_database    = 'drmahima_com'; $link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('unable establish db connection'); mysqli_query($link, "set names utf8"); function temp() {     $patient = mysqli_fetch_assoc(mysqli_query($link, "select name,dob patients id='69'"));     echo $patient['name']; } temp(); ?> 

when run this, query doesn't seem execute , nothing shows on page. however, if remove function , change code this:

<?php $db_host        = 'localhost'; $db_user        = 'root'; $db_pass        = 'root'; $db_database    = 'drmahima_com'; $link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('unable establish db connection'); mysqli_query($link, "set names utf8"); // function temp() {     $patient = mysqli_fetch_assoc(mysqli_query($link, "select name,dob patients id='69'"));     echo $patient['name']; // } // temp(); ?> 

it works, , patient's name displayed.

what going on here?

pass $link function parameter. try this:

<?php $db_host        = 'localhost'; $db_user        = 'root'; $db_pass        = 'root'; $db_database    = 'drmahima_com'; $link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('unable establish db connection'); mysqli_query($link, "set names utf8"); function temp($link) {     $patient = mysqli_fetch_assoc(mysqli_query($link, "select name,dob patients id='69'"));     echo $patient['name']; } temp($link); ?> 

Comments