在mysqli_query(arg1,arg2)中提供连接参数。 dbConnection位于单独的文件中

时间:2016-04-01 06:32:11

标签: php mysqli

我是mysqli的新手,并且仍在研究语法的变化,与旧的编码风格相比,其中大多数编码仅以mysql为前缀而不是mysqli。 所以我的问题是如何在mysqli_query()方法中提供连接参数。

语法说明使用此函数时需要提供connectionquery。 语法:

mysqli_query(connection,query,resultmode);

如果我为我的dbconnection维护一个.php文件,我通过使用include()函数将其包含在其他php文件中,我应该在mysqli_query(arg1,arg2,arg3);中作为连接参数放置什么}

这是我的代码。

<?php include('dbConnection.php') ?> # this, I added to avoid retyping
<?php
    if( isset($_POST['searchkeyword']) ){
        $var = $_POST['searchkeyword'];
        $query = "SELECT * FROM student_info WHERE (STUD_NO LIKE '%$var%' OR NAME LIKE '%$var%' )";
        $resultSet = mysqli_query($query); # gets the result set returned by SELECT statement

        while($rsArray = mysqli_fetch_assoc($resultSet)){
            $studno = $rsArray['stud_no'];
            $lastName = $rsArray['lname'];
            $firstName = $rsArray['fname'];
            $middleName = $rsArray['mname'];
            $address = $rsArray['address'];
            $city = $rsArray['city'];
            $gender = $rsArray['gender'];
            $enrolled = $rsArray['enrollment_stat']; 
            echo $studno."<br/>";
            echo $lastName.", ".$firstName." ".$middleName."<br/>";
            echo $address."<br/>";
            echo $city."<br/>";
            echo $gender."<br/>";
            echo $enrolled."<br/>";
        }      
    } 
?>

这是dbConnection.php的代码

<?php
# this it the connection php file to include
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpassword = "";
    $dbname = "stud_data";
    $connection = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbname); # connection to mysql

    if(mysqli_connect_errno() > 0){
        echo "<script>window.alert('Failed To Connect To Database')</script>";
    }


?>

我收到了这个警告。

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\...on line 7

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\...on line 9

我很感激任何建议。 感谢。

1 个答案:

答案 0 :(得分:-1)

希望下面的片段会给你一个想法 -

mysqli_query($connection,"Your query here");
<?php include('dbConnection.php') ?> # this, I added to avoid retyping
<?php
    if( isset($_POST['searchkeyword']) ){
        $var = $_POST['searchkeyword'];
        $query = "SELECT * FROM student_info WHERE (STUD_NO LIKE '%$var%' OR NAME LIKE '%$var%' )";

        $resultSet = mysqli_query($connection , $query); # gets the result set returned by SELECT statement

        while($rsArray = mysqli_fetch_assoc($resultSet)){
            $studno = $rsArray['stud_no'];
            $lastName = $rsArray['lname'];
            $firstName = $rsArray['fname'];
            $middleName = $rsArray['mname'];
            $address = $rsArray['address'];
            $city = $rsArray['city'];
            $gender = $rsArray['gender'];
            $enrolled = $rsArray['enrollment_stat']; 
            echo $studno."<br/>";
            echo $lastName.", ".$firstName." ".$middleName."<br/>";
            echo $address."<br/>";
            echo $city."<br/>";
            echo $gender."<br/>";
            echo $enrolled."<br/>";
        }      
    } 
?>