MYSQLi支持:mysqli_query()需要至少2个参数,给定1个

时间:2015-08-19 06:00:22

标签: php mysql mysqli

您好,我的代码中存在

问题
  

警告:mysqli_query()需要至少2个参数,1在

中给出

我尝试将所有信息都放在数据库中 我不知道为什么会发生这种情况,我使用mysql并且对mysqli了解不多,所以我需要知道我需要在代码中做些什么改变才能使它工作。给我问题的线是这个

的第32行
$sql = mysqli_query("INSERT INTO alumn VALUES('','$name','$age','$work')");

我已经接受了某人的建议,这是我在此代码中遇到的第一个问题 没有提出连接到DB的要求所以我这样做但仍然存在错误

require 'mysql.php';

有问题的整个代码就是这个

<?php 
require 'mysql.php';
$name = $_POST['name'];
$age = $_POST ['age'];
$work = $_POST ['work'];
$reqlen = strlen($name) * strlen($age) * strlen($work);
if ($reqlen > 0) {
        require ("mysql.php");
$connection = mysqli_connect("mysql.hostinger.mx","u487984755_root","password","u487984755_alumn")
$sql = mysqli_query("INSERT INTO alumn VALUES('','$name','$age','$work')");
mysqli_query;
        echo 'Data complete!';
    }else {
        echo 'Please end the formulary';
    }

&GT;

4 个答案:

答案 0 :(得分:1)

Mysqli是php mysql函数的改进版本,允许使用对象表示法和预处理语句等等。

你的情况下缺少的是连接,mysqli_query接受2个参数意味着它将第一个参数作为连接对象作为数据库,第二个参数是你的查询,所以一定要提供你​​的连接作为第一个参数..

以下是示例代码:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform queries 
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 
VALUES ('Glenn','Quagmire',33)");

mysqli_close($con);
?>

答案 1 :(得分:1)

您需要连接到mysqli_query($ connection,$ sql)

中的数据库
$connection = mysqli_connect('localhost','username','password','dbname') or die('error in mysql')

$sql = $sql = "('INSERT INTO alumn (FirstName,LastName,Age) VALUES('" . $name . "','" . $age . "','" . $work . "')')";
mysqli_query($connection,$sql) or die('error in sql');

答案 2 :(得分:1)

May be you miss one parameters while running the code with database connection.

将波纹管代码设为示例

$servername = "localhost";
$username = "root";
$password = "";
$db = "sample";
$conn = mysqli_connect($servername, $username, $password, $db);

$sql = "select * from jobs where job_id = 1";   //write your query here 

$result = mysqli_query($conn, $sql);

$result stores the result.

答案 3 :(得分:0)

您需要将连接作为第一个参数传递,如下所示定义: $conection = mysqli_connect("host","username","password","dbname");