php - connect db into function -
this question has answer here:
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
Post a Comment