php - connect db into function -


i can't connect db function: db connection lost function. error : mysqli_query() expects parameter 1 mysqli, null given

$dbcon = mysqli_connect($db_server, $db_user, $db_passwd);  /* check connection */ if ($dbcon->connect_errno) {   printf("connect failed: %s\n", $dbcon->connect_error);   exit(); }  mysqli_select_db($dbcon,$db_name);  function news() {      $numposts = mysqli_query($dbcon, 'select count(*) total ' . $db_prefix . 'news'); } 

thank in advance

you can rewrite function access $dbcon global var:

function news() {      global $dbcon;      $numposts = mysqli_query($dbcon, 'select count(*) total ' . $db_prefix . 'news');  } 

or pass argument news():

function news($dbcon) {     $numposts = mysqli_query($dbcon, 'select count(*) total ' . $db_prefix . 'news');  } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -