我用过
mysqli_connect("infos in here");
位于我的页面顶部,并尝试使用
mysqli_query("INSERT INTO and other info here");
当我这样做时,我收到了这个错误:
Warning: mysqli_query() expects at least 2 parameters, 1 given in (...)
但如果我改用
$con = mysqli_connect("infos in here");,
$mysqli_query($con,"INSERT INTO and other info here");
错误消失,我的脚本正常工作。
我的问题是我需要在我的页面中两次使用mysqli_query,并且我不想在它已经打开时再次打开它。
我该如何处理?
感谢。
答案 0 :(得分:1)
我的问题是我需要在两个不同的时间使用mysqli_query 我的页面,我不想再次打开连接 已经开放了。
怎么回事?打开一次查询多次然后关闭连接,例如:
<?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");
//one more
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
答案 1 :(得分:0)
无需连接两次。这里有一个例子..
$con = mysqli_connect("host", "user", "password", "db");
if(mysqli_connect_errno()){
die(mysqli_connect_error());
}
//Query one
$result1 = mysqli_query($con, 'Query String');
//Query Two
$result2 = mysqli_query($con, 'Query String'); //Used same $con variable
//After finishing all queries
mysqli_close($con);
如果您需要选择多个数据库,那么
$con = mysqli_connect("host", "user", "password");
//for DB bd_name1
mysqli_select_db($con, 'bd_name1');
$result1 = mysqli_query($con, 'Query String');
//for DB bd_name2
mysqli_select_db($con, 'bd_name2');
$result2 = mysqli_query($con, 'Query String');
但不要频繁连接。