Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

php - mysqli_prepare() expects parameter 1 to be mysqli

Having trouble with procedural use of mysqli

Here's the function:

db.php

<?php
define("DB_HOST", "host");
define("DB_LOGIN", "login");
define("DB_PASSWORD", "password");
define("DB_NAME", "myDb");

$link = mysqli_connect(DB_HOST, DB_LOGIN, DB_PASSWORD, DB_NAME);
if(!$link) {
    echo 'ERROR: ' . mysqli_connect_errno() . ': ' . mysqli_connect_error();
}
?>

function.php

<?php
function addItemToCatalog($var1, $var2, $var3, $var4) {
    $sql = 'INSERT INTO catalog (var1, var2, var3, var4) 
            VALUES (?, ?, ?, ?)';
    if (!$stmt = mysqli_prepare($link, $sql)){
        return false;
    }
    mysqli_stmt_bind_param($stmt, "ssii", $var1, $var2, $var3, $var4);
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_close($stmt); 
    return true;
}
?>

page.php

<?php
require_once ("db.php");
require_once ("function.php");

$var1 = $_POST['var1']; //showing without filtering methods
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
$var4 = $_POST['var4'];

if(!addItemToCatalog($var1, $var2, $var3, $var4)){ 
    echo 'some error text';
}
else { 
    header("Location: success.php"); 
    exit;
}
?>

After using this there's

WARNING: mysqli_prepare() expects parameter 1 to be mysqli, null given in function.php on line 5.

Anyone?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since $link is being define in db.php probably you should

require db.php;

in function.php file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...