Hey all,
First time poster, several time owner (my bookshelf sags under the VisualQuickPro section)
Trying to port over some code from mysql to mysqli, but ran into a snag (actually I just can't get my head around it...)
I am used to, on an "includes()" function file (functions.inc), including my db connection scripts and several db related functions.
For example, I have a section at the beginning of the functions.inc file:
(note the code here is stripped and cleaned to be clearer)
$connect = mysql_connect($tbl_hostname, $tbl_username, $tbl_password) ;
$selectdb = mysql_select_db($tbl_database, $connect) ;
Further down I call a function:
function user_exists($user)
{
$total = mysql_query("SELECT COUNT(`user_id`) FROM `user` WHERE `user_name` = '{$user}'");
return (mysql_result($total, 0) == '1') ? true : false;
}
and it worked fine, however when I try to use mysqli I run into problems.
Re-working the functions.inc file it now includes:
$connect = mysqli_connect('localhost', 'root', 'password', 'users_db') ;
and the function looks like:
function user_exists($user)
{
$total = mysqli_query($connect, "SELECT COUNT(`user_id`) FROM `user` WHERE `user_name` = '{$user}'");
$row= mysqli_fetch_array($total);
return ($row['COUNT(`user_id`)'] == '1') ? true : false;
}
But I keep getting a "Undefined variable: connect in the file... etc, etc"
My question (to which I couldn't find a clear answer) - Do I need to include the database connector line inside every function calling to the db?
function user_exists($user)
{
$connect = mysqli_connect('localhost', 'root', 'password', 'users_db');
$total = mysqli_query($connect, "SELECT COUNT(`user_id`) FROM `user` WHERE `user_name` = '{$user}'");
$row= mysqli_fetch_array($total);
return ($row['COUNT(`user_id`)'] == '1') ? true : false;
}
Thanks for any pointers.
-t