4 Replies - 730 Views - Last Post: 04 January 2014 - 07:23 AM Rate Topic: -----

#1 chiba   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-December 13

Problem with prepared statements

Posted 04 January 2014 - 06:34 AM

function register($first_name,$last_name,$email,$password)
{

	$conn = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
	$stmt = $conn->prepare("insert into users values (null, ?,?,?,?,? NOW())");
	$level=0;
	$stmt->bind_param("ssssi",$first_name,$last_name,$email,$password,$level);


	$stmt->execute();
}



This is the database i'm trying to insert into, id is auo increment.

Type Collation
id int(10)
first_name varchar(45)
last_name varchar(45)
email varchar(80)
pass varchar(40)
level int(10)
date_joined datetime

I get this error
Fatal error: Call to a member function bind_param() on a non-object in /home/content/27/9433527/html/core/functions.php on line 27

How can I fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with prepared statements

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4192
  • View blog
  • Posts: 13,244
  • Joined: 08-June 10

Re: Problem with prepared statements

Posted 04 January 2014 - 06:51 AM

by using error handling. because preparing your statement failed (SQL syntax error).

btw, I strongly advise to list all the fields you want to update (this way you can leave out the supposedly auto_increment field, and there is no ambiguity of which value going where)
Was This Post Helpful? 0
  • +
  • -

#3 chiba   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-December 13

Re: Problem with prepared statements

Posted 04 January 2014 - 07:07 AM

I don't get it, the following works
<?php
include_once 'core/connect-live.php';
$testString='James';

$conn = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);


$stmt = $conn->prepare("insert into `test` (id,testString,date_now ) values (null, ?, now())");
$stmt->bind_param("s",$testString);

$stmt->execute();
?>



id testString date_now
Edit Delete 1 James 2014-01-04 07:06:26
Edit Delete 2 James 2014-01-04 07:06:27
Edit Delete 3 James 2014-01-04 07:06:27
Edit Delete 4 James 2014-01-04 07:06:28
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4192
  • View blog
  • Posts: 13,244
  • Joined: 08-June 10

Re: Problem with prepared statements

Posted 04 January 2014 - 07:13 AM

View Postchiba, on 04 January 2014 - 03:07 PM, said:

I don't get it, the following works

of course. it doesn’t contain any SQL errors.
Was This Post Helpful? 0
  • +
  • -

#5 chiba   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-December 13

Re: Problem with prepared statements

Posted 04 January 2014 - 07:23 AM

Thanks :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1