mysqli_query不接受我的mysqli_connect并正确使用mysqli_query的参数

时间:2014-04-01 22:13:12

标签: php mysqli

我已将configdb.php设置在另一个页面上,并将其包含在我的其他php页面上。

这是我的configdb.php

<?php
$hostname ="localhost";
$username ="root";
$password ="";
$db ="practicedb";
$connect = mysqli_connect($hostname,$username,$password) or die("cannot connect to server");
mysqli_select_db($connect,$db) or die("database not found!");
?>

这些是我得到的错误:

Notice: Undefined variable: configdb in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 14

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 14

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 15

Notice: Undefined variable: configdb in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 28

这是我的add.php,我从上一个php页面的$ _POST方法将项目插入数据库..

<?php
include "configdb.php";



$studid=$_POST['studid'];
$lastname=mysql_real_escape_string($_POST['lastname']);
$firstname= mysql_real_escape_string($_POST['firstname']);
$middlename= mysql_real_escape_string($_POST['middlename']);
$email=$_POST['email'];


$check = "SELECT * from studinfo where stud_id = '".$studid."'";
$qry = mysqli_query($configdb,$check);
$num_rows = mysqli_num_rows($qry); 


if($num_rows > 0){
// Here we are checking if username is already exist or not.

    echo "The person you have entered is already existing. Please try again.";
    echo '<a href="index.php">Try Again</a>';
    exit;
}

$query = "INSERT INTO studinfo (stud_id,lastname,firstname,middlename,email) VALUES ('".$studid."','".$lastname."','".$firstname."','".$middlename."','".$email."');";
//echo $query;
mysqli_query($configdb, $query);
echo "Thank You for Registration.";
echo '<a href="index.php">Click Here</a> to login you account.';
exit;
?>

我不知道,我不知道在mysqli_query的第一个参数上放什么.. 我尝试将此代码$con=mysqli_connect("localhost","root","","practicedb");设置为有效,但是在我应该连接到数据库的每个php页面上都不实用...

1 个答案:

答案 0 :(得分:1)

另一个关于愚蠢错字的问题......

$ connect = mysqli_connect(...
与 $ qry = mysqli_query( $ configdb ,$ check);

因此错误消息清楚地表明:未定义的变量:configdb