-1

Help me with this code: I get this error.

Notice: Undefined variable: dbCon in C:\xampp\htdocs\Project\core\functions\general.php on line 5

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Project\core\functions\general.php on line 5

Notice: Undefined variable: dbCon in C:\xampp\htdocs\Project\core\functions\Users.php on line 5

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Project\core\functions\Users.php on line 5

Fatal error: Uncaught Error: Call to undefined function mysqli_result() in C:\xampp\htdocs\Project\core\functions\Users.php:8 Stack trace: #0 C:\xampp\htdocs\Project\login.php(4): user_exists(NULL) #1 {main} thrown in C:\xampp\htdocs\Project\core\functions\Users.php on line 8

Main folder(name -> PHP) has Index.php:

    <?php 
include 'core/init.php';
include 'includes/overall/header.php';
?>

PHP folder has Login.php:

 <?php
include 'core/init.php';

if(user_exists('sudin') === true)
{
    echo 'exists';
}
?>

under php/core folder init.php:

<?php
session_start();

require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';
?>

under php/core/database connect.php:

<?php
$dbCon=mysqli_connect('localhost','root','','project_point');

?>

under php/core/functions Users.php:

<?php
function user_exists($username)
{
    $username = sanitize($username);
    $query = mysqli_query($dbCon,"SELECT COUNT('Login_ID')FROM 'login' WHERE 'Username' = '$username'");
    return(mysqli_result($query,0)==1) ? true : false;
}
 ?>

under php/core/functions general.php:

<?php

function sanitize($data)
{
    return mysqli_real_escape_string($dbCon,$data);
}
?>[enter image description here][1]

Sorry for the messy way, here is the treeview:

Thankful for the help

sebenalern
  • 2,515
  • 3
  • 26
  • 36
Sudin
  • 45
  • 1
  • 9

4 Answers4

2

There are a few things wrong here.

Firstly, you have a variable scope issue and you're also using the wrong identifier qualifiers in your query being regular quotes:

So, remove the quotes for this:

SELECT COUNT('Login_ID')FROM 'login' WHERE 'Username'
             ^        ^      ^     ^       ^        ^ Remove those

or use ticks:

SELECT COUNT(`Login_ID`) FROM `login` WHERE `Username`

Checking for errors on it using mysqli_error($dbCon) http://php.net/manual/en/mysqli.error.php would have thrown you something about it once it would have fired.

Make your connection global in your user_exists() function

global $dbCon;

Read up on both:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

The connection variable is not defined in the function scope. The hotfix is to add it with the "global" construct.

function user_exists($username)
{
global $dbCon;
...
Agnis
  • 131
  • 1
  • 2
0

you are trying to use a local variable $dbCon inside the function user_exists.

you can just change that variable to be global

ex:

under php/core/database connect.php:

<?php
global $dbCon;
$dbCon=mysqli_connect('localhost','root','','project_point');

?>

under php/core/functions Users.php:

<?php
function user_exists($username)
{
    global $dbCon;
    $username = sanitize($username);
    $query = mysqli_query($dbCon,"SELECT COUNT('Login_ID')FROM 'login' WHERE 'Username' = '$username'");
    return(mysqli_result($query,0)==1) ? true : false;
}
 ?>
0

The issue is that $dbCon is not defined in the scope of any function. You could do 3 things:

function user_exists($username){
  $query = mysqli_query($GLOBALS['dbCon'] ... 

function user_exists($username){
  global $dbCon;
  $query = mysqli_query($dbCon ...

However, using globals in functions like this is usually a bad coding style. I would recommend doing something like the following:

function getconn(){
  static $conn;
  if(!isset($conn)){
    $conn = new mysqli_connect(...);
  } 

  return $conn;
}

function user_exists($username){
  $query = mysqli_query(getconn() ... 

I was just about to add the issues with your SQL syntax, but Fred beat me to it. I think you want backticks ` instead, not '.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • Great help. what about the Fatal error? Fatal error: Uncaught Error: Call to undefined function mysqli_result() in C:\xampp\htdocs\Project\core\functions\Users.php:8 Stack trace: #0 C:\xampp\htdocs\Project\login.php(4): user_exists(NULL) #1 {main} thrown in C:\xampp\htdocs\Project\core\functions\Users.php on line 8 – Sudin Mar 23 '16 at 20:39
  • There is no such function in mysqli, however you could [add](http://stackoverflow.com/a/20961496/4982088) the function that works like the old mysql_result(). – Xorifelse Mar 23 '16 at 20:48
  • Same for that also: Call to undefined function mysql_result() in C:\xampp\htdocs\Project\core\functions\Users.php:9 Stack trace: #0 C:\xampp\htdocs\Project\login.php(4): user_exists('sudin') #1 {main} thrown in C:\xampp\htdocs\Project\core\functions\Users.php on line 9 – Sudin Mar 23 '16 at 20:59