1

我有来自同一个表的两个查询并选择相同的信息,但将结果发送给两个名称。有两个变量来存储结果,但是如何存储来自查询的其他信息?

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");
}
4

3 回答 3

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

于 2013-03-26T22:24:13.370 回答
0

如果重点是获得两张随机图片,为什么不这样做呢?

SELECT
    *
FROM
    artwork
WHERE 
    id != 37 AND
    name != ''
ORDER BY 
    RAND()
LIMIT 2
于 2013-03-26T22:23:33.143 回答
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
}

注意单=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
}

正是出于这个原因,我更喜欢这样做,而'' == $name不是导致解析错误,这比正在运行但损坏的脚本更好。$name == '''' = $name

2. 你没有传递$rowfind_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);
于 2013-03-26T22:22:36.817 回答