-1

I'm trying to create a function to check for duplicate entries in a mysql using mysqli (brand new to me) but I can't get my head around this error. I've got 3 php files: db.php - db connection functions.php with my function (requires db.php) and submit.php - the file that takes the input.

db.php:

<?php
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "user"); // The database username.
define("PASSWORD", "pass"); // The database password. 
define("DATABASE", "db_list"); // The database name.

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
?>

functions.php:

require 'db.php';
function check($str, $mysqli) {

$checkdupe = $mysqli->prepare("SELECT name FROM list WHERE str = ?"); //line giving me error
$checkdupe->bind_param("s", $str);
$checkdupe->execute();
$checkdupe->store_result();

    if ($checkdupe->num_rows > 0) {
        //dupe found
        return true;
    } else {
            //no dupe
            return false;
    }
}

submit.php

require 'functions.php';

if (isset($_POST['name'])) {
    if (check($_POST['name'], $mysqli) == true) { //added $mysqli parameter
        echo "success!";
    } else {
        echo "fail;
    }
} else {
  echo 'invalid post';
}
?>
user2480651
  • 111
  • 1
  • 1
  • 4
  • 1
    Show us how you call the function `check()`. – Tobias Golbs Jul 30 '13 at 14:25
  • That's not PDO... that's mysqli, and you should have better error handling in there. If `->prepare()` is causing the error, then `$mysqli` isn't a valid DB connection handle (period, or at least at that point in the script, e.g. scope issue), and probably your `connect` call failed. – Marc B Jul 30 '13 at 14:26
  • Can we see your `submit.php` file? This is necessary to determine where the error is. – Mattiavelli Jul 30 '13 at 14:28

4 Answers4

2

your function definition is

function check($str, $mysqli) {

but you call it with only the first parameter. As a result the second parameter is null.

check($_POST['name'])

so use,

check($_POST['name'], $mysqli)

Also you don't do any checks that the connection is indeed successfull in db.php. Add this at the end of the file:

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
Aris
  • 4,643
  • 1
  • 41
  • 38
  • To add on, in order for this to work you either need to declare `$mysqli` as a global (in both `db.php` and in `submit.php` before use), __OR__ you could `require 'db.php'` within `submit.php`. __ALSO__ there is a syntax error in OP's problem, `echo "fail;` should be `echo "fail";` – Mattiavelli Jul 30 '13 at 14:40
  • 1
    with the required() is enough. If the required() is before the function call, it's passed as a function parameter and no need for global. – Aris Jul 30 '13 at 14:43
  • just tried this, no dice – user2480651 Jul 30 '13 at 14:43
  • I didn't see the chain of requires, ignore my first point. What is the result @user2480651? – Mattiavelli Jul 30 '13 at 14:45
  • server error. in error.log "Call to a member function prepare() on a non-object in /var/www/functions.php on line 91." – user2480651 Jul 30 '13 at 14:46
  • 1
    Try putting a `;` after `require 'db.php'` – Mattiavelli Jul 30 '13 at 14:49
  • @user2480651 you must tell us the chain of requires. you probably require one file before the variable is ready... – Aris Jul 30 '13 at 14:53
  • nice catch. unfortunately i just messed up the formatting here. submit.php requires functions.php, functions.php requires db.php – user2480651 Jul 30 '13 at 14:54
0

Your function check expects 2 parameters. You only assign 1 property to it:

if (check($_POST['name']) == true)

should be

if (check($_POST['name'], $mysqli) == true)

Tobias Golbs
  • 4,586
  • 3
  • 28
  • 49
-1

db.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

define("HOST", "localhost"); // The host you want to connect to.
define("USER", "user"); // The database username.
define("PASSWORD", "pass"); // The database password. 
define("DATABASE", "db_list"); // The database name.

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
var_dump('db:',is_object($mysqli));

functions.php:

<?php
require 'db.php'
var_dump('functions:',is_object($mysqli));
function check($str, $mysqli) {
    var_dump('check:',is_object($mysqli));

    $checkdupe = $mysqli->prepare("SELECT name FROM list WHERE str = ?");
    $checkdupe->bind_param("s", $str);
    $checkdupe->execute();
    $checkdupe->store_result();
    return $checkdupe->num_rows;
}

submit.php

<?php
require 'functions.php';
var_dump('submit:',is_object($mysqli));


if (isset($_POST['name'])) {
    if (check($_POST['name'], $mysqli)) {
        echo "success!";
    } else {
        echo "fail";
    }
} else {
  echo 'invalid post';
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-2

Try :

global $mysqli;
check($mysqli, ...);
Virus721
  • 8,061
  • 12
  • 67
  • 123
  • Dislike! not a nice solution – steven Jul 30 '13 at 14:29
  • 1
    In this case, the `$mysqli` parameter in the `check()` function should be removed, as it wouldn't need to be passed. – Mattiavelli Jul 30 '13 at 14:30
  • @steven How about providing arguments and explainations instead of hate ? – Virus721 Jul 30 '13 at 14:31
  • I probably misread but i'd bet my head on the fact that his calling check without declaring $mmysqli as global before. – Virus721 Jul 30 '13 at 14:32
  • no hate. i didnt downvote at all. It should be clear why it is not a good solution. $mysqli is passed as an argument and i am not a friend of globalisation – steven Jul 30 '13 at 14:32
  • There is no other option than using the global keyword or $GLOBALS if you want to access a variable declared in global scope. – Virus721 Jul 30 '13 at 14:33
  • yes there is: passing it as an argument, but it needs to be declared before passing – steven Jul 30 '13 at 14:35
  • Yeah that's pretty much what i said. If you don't retrieve it using global or $GLOBALS, you can't pass it. – Virus721 Jul 30 '13 at 14:38
  • sorry but i think @Aris solution is better. You dont need to use global if you want to pass it. By the way, it is the second argument, not the first. – steven Jul 30 '13 at 14:42
  • I'm pretty convinced it depends if you're calling it from the file you first declared it in, or from another file requiring directly or indirectly this file. – Virus721 Jul 30 '13 at 14:47
  • Why not to run a simple test to verify your assumption? – Your Common Sense Jul 30 '13 at 15:06
  • I have, else i wouldn't bother using that ugly syntax. – Virus721 Jul 30 '13 at 15:14