tags:

views:

85

answers:

2

I feel like a total n00b for not understanding what I'm doing wrong, but here goes.

I'm writing a simple web form that's storing information in a MySQL database. I'm getting this error:

mysqli_stmt_init() expects parameter 1 to be mysqli, null given in /myfile.php

Here's my code:

$server = 'localhost';
$username = 'myusername';
$password = 'mypassword';
$db = 'mydb';
$link = mysqli_connect($server, $username, $password, $db);
.
.
. 
$stmt = mysqli_stmt_init($link); /*This line throws the error*/

Any ideas? Thanks in advance.

+3  A: 

Are you 100% sure you are successfully connecting to the database?

Add at the top of your script:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Also add right below your connection line:

if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());
    exit;
}
Paolo Bergantino
Yes, I ran a simple check before the $stmt declaration if ($link) { $stmt = mysqli_stmt_init($link); }That should work, right?
mclaughlinj
Never mind, you're right... Variable scope bit me. Thanks!
mclaughlinj
A: 

If this is your dev server you should probably set the following lines in your php.ini file rather than in each script.

error_reporting = E_ALL display_errors = on

if that doesn't solve you problem than you should print our $link to see what it is.

Andrew Clark