PHP页面上是否可以有两种类型的MySQL连接?
目前在 PHP 中有一个$link = mysql_connect
和一个$mysqli = new mysqli
通过两个单独的文件访问的连接。include
它们都从 MySQL 数据库中提取数据,但如果它们都在同一个 PHP 页面中,则第二个连接不起作用。
我错过了一些非常明显的东西吗?
mysql
$link = mysql_connect("localhost", "root", "root", "abc");
if(!$link)
{
die('There was a problem connection to the database. Please contact your survey administrator.');
}
mysql_select_db("root");
$query = "SELECT * FROM tresults";
$result = mysql_query($query);
$total = mysql_num_rows($result);
$query1 = "SELECT * FROM trespondent";
$result1 = mysql_query($query1);
$total1 = mysql_num_rows($result1) - 1;
$percent = number_format(($total * 100) / $total1);
mysql_close($link);
}
mysqli
$mysqli = new mysqli("localhost", "root", "root", "abc");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$a = 'q';
$aa = 1;
$r = "$a$aa";
$q = 55;
while($aa <= $q){
$query .= "SELECT COUNT(". $r .") as Responses, (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = -1 ) as NA, (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 1 ) as SD, (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 2 ) as D, (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 3 ) as A, (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 4 ) as SA, ((SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 3 ) + (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 4)) as Pos, ((SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 1 ) + (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 2)) as Neg, (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." >= 1) AS Total, ( ((SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 3 ) + (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." = 4)) / (SELECT COUNT(". $r .") FROM tresults WHERE ". $r ." >= -1) ) *100 AS percentage FROM tresults;";
$aa = $aa + 1;
$r = "$a$aa";
注意:其余代码和关闭在 PHP 页面中使用:$mysqli->close();
if ($mysqli->multi_query($query)) {
$n = 0;
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
$i = 1;
$p = 1;
while ($row = $result->fetch_row()) {
// print_r($row);
$n++;