我有来自同一个表的两个查询,并选择相同的信息,但将结果发送到两个名称。存储结果有两个变量,但如何存储查询中的其他信息?
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to server, line 20.');
$query = "SELECT COUNT(id) FROM artwork"; //find total number of id's
$result = mysqli_query ($dbc, $query) or die("query error, line 22");
$row = mysqli_fetch_array ($result, MYSQL_NUM);
find_pic(); //get the two id numbers
while ($count1 = $count2 or name = ""){ //problem line. check if counts = each other or either name = nothing
find_pic(); //if true, run function again
}
while ($count1=37 or $count2=37){ //if either count = 37
find_pic(); //if true, run function again
}
show_pic();
function find_pic(){
$count1 = rand(0,$row[0]);
$count2 = rand(0,$row[0]);
$query1 = "SELECT * FROM artwork WHERE id = $count1";
$query2 = "SELECT * FROM artwork WHERE id = $count2";
$result1 = mysqli_query ($dbc, $query1) or die("query error, line 38");
$result2 = mysqli_query ($dbc, $query2) or die("query error, line 39");
}
答案 0 :(得分:1)
试试这个(它不是你问题的完整解决方案,唯一的帮助):
$result1 = $dbc->prepare("SELECT * FROM artwork WHERE id = $count1");
$result2 = $dbc->prepare("SELECT * FROM artwork WHERE id = $count2");
$result1->execute();
$result2->execute();
$result1->bind_result($column1, $column2); //all columns from table artwork
$result2->bind_result($column1, $column2); //...
while ($result1->fetch())
{
echo $column1; //here u have your result data
echo $column2;
};
了解如何使用预备声明!!!! http://php.net/manual/en/mysqli-stmt.bind-param.php
答案 1 :(得分:0)
<强> 1。您要分配而不是比较:
while ($count1 = $count2 or name = ""){ //problem line. check if counts = each other or either name = nothing
find_pic(); //if true, run function again
}
while ($count1=37 or $count2=37){ //if either count = 37
find_pic(); //if true, run function again
}
注意单=
个?你想要两个(或三个):
while ($count1 == $count2 or name == ""){ //problem line. check if counts = each other or either name = nothing
find_pic(); //if true, run function again
}
while ($count1==37 or $count2==37){ //if either count = 37
find_pic(); //if true, run function again
}
由于这个原因,我更喜欢'' == $name
而不是$name == ''
因为'' = $name
会导致解析错误,这比正在运行但已损坏的脚本更好。
<强> 2。您没有将$row
传递给find_pic()
:
大多数变量都受范围限制。 $row
函数中不存在find_pic
,除非您将其作为参数传递:
function find_pic($row){
$count1 = rand(0,$row[0]);
$count2 = rand(0,$row[0]);
$query1 = "SELECT * FROM artwork WHERE id = $count1";
$query2 = "SELECT * FROM artwork WHERE id = $count2";
$result1 = mysqli_query ($dbc, $query1) or die("query error, line 38");
$result2 = mysqli_query ($dbc, $query2) or die("query error, line 39");
}
find_pic($row);
第3。 $count1
和$count2
未定义:
您正在find_pic
函数中设置它们,但它们不在该范围之外。您可能想要使用引用:
function find_pic($row, &$count1, &$count2){
$count1 = rand(0,$row[0]);
$count2 = rand(0,$row[0]);
$query1 = "SELECT * FROM artwork WHERE id = $count1";
$query2 = "SELECT * FROM artwork WHERE id = $count2";
$result1 = mysqli_query ($dbc, $query1) or die("query error, line 38");
$result2 = mysqli_query ($dbc, $query2) or die("query error, line 39");
}
find_pic($row, $count1, $count2);
答案 2 :(得分:0)
如果要获得两张随机图片,为什么不这样做?
SELECT
*
FROM
artwork
WHERE
id != 37 AND
name != ''
ORDER BY
RAND()
LIMIT 2