I have two queries from the same table and selecting the same info but sending results to two names. There are two variables to store the result, but how do I store other information from the queries?
我从同一个表中有两个查询并选择相同的信息,但将结果发送到两个名称。存储结果有两个变量,但如何存储查询中的其他信息?
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");
}
3 个解决方案
#1
0
If the whole point is to get two random pictures why not do this?
如果要获得两张随机图片,为什么不这样做呢?
SELECT
*
FROM
artwork
WHERE
id != 37 AND
name != ''
ORDER BY
RAND()
LIMIT 2
#2
1
Try this (it isnt complete solution for your problem, its only help):
试试这个(它不是你的问题的完整解决方案,它唯一的帮助):
$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;
};
Learn how to use PREPARED STATEMENTS!!!! http://php.net/manual/en/mysqli-stmt.bind-param.php
学习如何使用准备好的声明!!!! http://php.net/manual/en/mysqli-stmt.bind-param.php
#3
0
1. You're assigning rather than comparing:
你在分配而不是比较:
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
}
Notice the single =
s? You want two (or three):
注意单个= s?你想要两个(或三个):
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
}
It's for this reason that I prefer to do '' == $name
rather than $name == ''
as '' = $name
will result in a parse error which is better than a running but broken script.
出于这个原因,我更喜欢做''== $ name而不是$ name ==''作为''= $ name将导致解析错误,这比运行但破坏的脚本更好。
2. You're not passing $row
to find_pic()
:
2.你没有将$ row传递给find_pic():
Most variables are subject to scope. $row
doesn't exist inside the find_pic
function unless you pass it as a parameter:
大多数变量都受范围限制。除非您将其作为参数传递,否则find_pic函数中不存在$ row:
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
and $count2
are not defined:
3. $ count1和$ count2未定义:
You're setting them inside the find_pic
function but they don't exist outside that scope. You might want to use references:
您在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);
#1
0
If the whole point is to get two random pictures why not do this?
如果要获得两张随机图片,为什么不这样做呢?
SELECT
*
FROM
artwork
WHERE
id != 37 AND
name != ''
ORDER BY
RAND()
LIMIT 2
#2
1
Try this (it isnt complete solution for your problem, its only help):
试试这个(它不是你的问题的完整解决方案,它唯一的帮助):
$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;
};
Learn how to use PREPARED STATEMENTS!!!! http://php.net/manual/en/mysqli-stmt.bind-param.php
学习如何使用准备好的声明!!!! http://php.net/manual/en/mysqli-stmt.bind-param.php
#3
0
1. You're assigning rather than comparing:
你在分配而不是比较:
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
}
Notice the single =
s? You want two (or three):
注意单个= s?你想要两个(或三个):
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
}
It's for this reason that I prefer to do '' == $name
rather than $name == ''
as '' = $name
will result in a parse error which is better than a running but broken script.
出于这个原因,我更喜欢做''== $ name而不是$ name ==''作为''= $ name将导致解析错误,这比运行但破坏的脚本更好。
2. You're not passing $row
to find_pic()
:
2.你没有将$ row传递给find_pic():
Most variables are subject to scope. $row
doesn't exist inside the find_pic
function unless you pass it as a parameter:
大多数变量都受范围限制。除非您将其作为参数传递,否则find_pic函数中不存在$ row:
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
and $count2
are not defined:
3. $ count1和$ count2未定义:
You're setting them inside the find_pic
function but they don't exist outside that scope. You might want to use references:
您在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);