-1

This script is used with an Android app

The error : 2 Fatal error: Call to a member function bind_param() on a non-object in ** on line 36

There is three sql queries on my script the first one is running correctly but the two other one not.

I don't really understand why because I do exactly the same stuff ...


PHP


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

$con = mysqli_connect("******","******","******","******");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}
else {
    // If all the input are ok
    if(isset($_REQUEST['firstName']) AND isset($_REQUEST['lastName']) AND isset($_REQUEST['phone']) AND isset($_REQUEST['linkedPhone']) AND isset($_REQUEST['imei'])) {

        $firstname = $_REQUEST['firstName'];
        $lastName = $_REQUEST['lastName'];
        $phone = $_REQUEST['phone'];
        $linkedPhone = $_REQUEST['linkedPhone'];
        $imei = $_REQUEST['imei'];

        $req = $con->prepare('SELECT idUser FROM user WHERE phone = ?');
        $req->bind_param("s", $_REQUEST['linkedPhone']);
        $req->execute();
        $req -> bind_result($idFollowed);
        $result = $req->fetch();
        $idFollowed = "".$idFollowed;
        echo $idFollowed;
        // if linkedPhone doesn't exist
        if(!$result) {
            $return['result'] = "Linked phone doesn't exist";
        }
        else {
            $req2 = $con->prepare('INSERT INTO user (firstName, lastName, phone, idFollowed, imei) VALUES (?, ?, ?, ?, ?)');
            // Work using phpMyAdmin
            // INSERT INTO user (firstName, lastName, phone, idFollowed) VALUES ('Gael', 'Fontenelle', '01234', '1');
            $req2->bind_param("sssss", $_REQUEST['firstName'], $_REQUEST['lastName'], $_REQUEST['phone'], $idFollowed, $_REQUEST['imei']); // ERROR HERE
            $req2->execute();
            // Find the id of the new user
            $req3 = $con->prepare('SELECT idUser FROM user WHERE phone = ?');
            $req3->bind_param("s", $_REQUEST['phone']); // ERROR HERE
            $req3->execute();
            $req3 -> bind_result($idUser);
            $result = $req->fetch();
            if(!$result) {
                $return['result'] = "Error in the registration process";
            }
            else {
                $return['result'] = "".$idUser;
            }
        }
    }
    else {
        // Display the error
        $return['result'] = "Missing data!";
    }
    echo json_encode($return);
}   
mysqli_close($con);
?>

SQL


CREATE TABLE user (
    idUser int NOT NULL auto_increment,
    firstName varchar(50) NOT NULL,
    lastName varchar(50) NOT NULL,
    phone varchar(50) NOT NULL,
    idFollowed int default 0, -- 0 if it's the primary user
    validation boolean NOT NULL, default 0, -- 1 = VALIDATION OK 
    imei varchar(50) NOT NULL,
    PRIMARY KEY (idUser),
    FOREIGN KEY (idFollowed) REFERENCES idUser (user)
);

INSERT INTO user (firstName, lastName, phone, imei) VALUES
    ('Steve', 'Jobs', '0836656565651', '23456789765'),
    ('Steve', 'Wozniak', '0836656565652', '23456789765'),
    ('Bill', 'Gates', '0836656565653', '23456789765'),
    ('Steve', 'Balmer', '0836656565654', '23456789765'),
    ('Larry', 'Pagen', '0836656565655', '23456789765'),
    ('Serguei', 'Brin', '0836656565656', '23456789765');
NotGael
  • 66
  • 1
  • 9
  • 1
    possible duplicate of [Call to a member function bind\_param() on a non-object](http://stackoverflow.com/questions/4488035/call-to-a-member-function-bind-param-on-a-non-object) – CBroe Apr 02 '14 at 12:00
  • check first that is this execution reaches to your satisfied condition or not ? might possible it havn't reached yet, try echo - die() in your expected condition – SagarPPanchal Apr 02 '14 at 12:04

2 Answers2

0
$req2->bind_param("sssis",$_REQUEST['firstName'], $_REQUEST['lastName'], $_REQUEST['phone'], $idFollowed, $_REQUEST['imei']);

your fourth parameter is int Replaced 'sssss' to 'sssis'

0

That means that your sql query failed due to an error. Here you have a problem on how you set up the connection and consequently the $con object. Well try this

$con = new mysqli("myhost","myusrname","mypwd","mydb");

/*then check connection */

if($con->connection_error)
{

   die("error in connecting to database");
}
/*Proceed with the rest here */

You forgot the

 new

keyword

You may also have an sql error in the prepare field. You can check the error this way

$resq = $con->prepare("......");
if(!$resq)
{
    die("prepare failed ".$con->error);
}
Kimutai
  • 1,022
  • 11
  • 15
  • It doesnt change anything : 2 Fatal error: Call to a member function bind_param() on a non-object in **** on line 36 I think new mysqli() or mysqli_connect() ... like equivalent I don't know cause I use the same type of stuff to do many queries before and like you can see I try to display the idFollowed and it's working so if the first queries is working properly I don't think it's a problem with de $con :s – NotGael Apr 02 '14 at 12:30
  • Then if you're 100% certain that your connection is okay, than the sql query in the prepare() failed due to a query error. I've just edited my answer, see how to check the sql errors that occured – Kimutai Apr 02 '14 at 12:54