I have connected to database link below

$db_host     = "localhost";
 $db_username = "root";
 $db_password = "";
 $db_name     = "db"; 


$sql = new mysqli($db_host,$db_username,$db_password,$db_name);

And I refered to it on a different page like:

function user_exists($username){
     $username = sanitize($username);
     $query_user = $sql->query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
     return (mysqli_result($query_run,0)==1)? true:false;
 }

But I still receive:
undefined variable sql,
What am I missing?

Recommended Answers

All 4 Replies

Member Avatar for diafol

WHy are you creating a mysqli object and then ignoring it with mysqli_result? WHere is $query set?

Alternatively, I did something like below, I had the same error: undefined variable sql

$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'db';

$sql = mysqli_connect($db_host,$db_username,$db_password,$db_name);

$query = mysqli_query($sql,"SELECT");

return (mysqli_result($query,0)===1)?true:false;

Using the Object

$db = new mysqli($db_host,$db_username,$db_password,$db_name);
$query = $db->query("SELECT things FROM table WHERE something = '$something'");
return ($query->result(0)===1)?true:false;

Is this correct?

Member Avatar for diafol

return should only be used inside a function or class method. Is this in a function or class method?

What does $query->result try to do? Are you looking for num_rows?

One problem that you have is that your object naming is ambiguous.

$query suggests a statement, whereas you're actually storing the resultset in it. It would probably be best to call it $result.

In addition, you are injecting variables directly into the SQL. While this may be OK for trusted values, why not take advantage of prepared statements?

Some of the queries are only to validate the availability of a user in the database, and you're right, these only check the num_rows to see if users is available or. Other queries are what I want to echo to the users which demands the mysql_fetch_assoc

I want to connect to database uisng mysqli instead of mysql, which means the queries and functions must use the mysqli.

my main problem is converting mysql_results, eg below

$db_host     = "localhost";
 $db_username = "username";
 $db_password = "password";
 $db_name     = "db_name";

mysqli_connect($db_host,$db_username,$db_password);
mysql_select_db($db_name);

function email_exists($email){
  $email =sanitize($email)
   $query = mysql_query(SELECT user_id FROM users WHERE email = '$email'");
    return (mysql_result($query,0)===1)?true:false;// here
 }

Connecting to database using mysqli procedural.

$mysqli = mysqli_connect($db_host,$db_username,$db_password,$db_name);

function email_exists($email){
$email = sanitize($email);
 $query = mysqli_query($sql,"SELECT COUNT(user_id) FROM table WHERE email = '$email'");
 return(//here is the problem since there isn't mysqli_result function);
}

Connecting using Object

$mysqli = new mysqli($db_host,$db_username,$db_password,$db_name);

function email_exists($email){
 $email = sanitize($email);
$query = $mysqli->query("SELECT COUNT(user_id) FROM users WHERE email = '$email'");
return ($query->????????) // here is the problem. I have been scouring the net for solution but to no avail.
}

Thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.