在同一行的不同列中插入2次随机名称

时间:2012-09-22 15:48:16

标签: php mysql phpmyadmin

这是代码:

$sql = "SELECT * FROM blad1 order by RAND() LIMIT 6";
$result = mysql_query($sql) ; 
$row = mysql_fetch_array($result);
$Vnaam = $row["Voornamen"]; 

$sql1 = "SELECT * FROM blad1 order by RAND() LIMIT 6";
$result = mysql_query($sql) ; 
$row = mysql_fetch_array($result);
$Anaam = $row["Achternamen"]; 

$sql2 = "INSERT INTO duivengebruiker (Duif1) VALUES ('$Vnaam $Anaam')";
mysql_query($sql2);

$sql = "SELECT * FROM blad1 order by RAND() LIMIT 6";
$result=mysql_query($sql) ; 
$row=mysql_fetch_array($result);
$Vnaam = $row["Voornamen"]; 

$sql1 = "SELECT * FROM blad1 order by RAND() LIMIT 6";
$result = mysql_query($sql) ; 
$row = mysql_fetch_array($result);
$Anaam = $row["Achternamen"]; 

$sql2 = "INSERT INTO duivengebruiker (Duif2) VALUES ('$Vnaam $Anaam')";
mysql_query($sql2);

这将创建2行并将第1个结果放在第1行的第1列中,并将第2个结果放在第2行的第2列中。我只需要填充两列的一行。我该怎么办?可能很容易。

1 个答案:

答案 0 :(得分:0)

如果我理解你想要做的是在执行插入之前选择两个随机名称。这样,您可以将它们包含在一个查询中。例如,您可以尝试这样的事情:

$sql = "SELECT * FROM blad1 order by RAND() LIMIT 6";

$result = mysql_query($sql) ; 
$row = mysql_fetch_array($result);
$Vnaam1 = $row["Voornamen"]; 

$result = mysql_query($sql) ; 
$row = mysql_fetch_array($result);
$Anaam1 = $row["Achternamen"]; 

$result = mysql_query($sql) ; 
$row = mysql_fetch_array($result);
$Vnaam2 = $row["Voornamen"]; 

$result = mysql_query($sql) ; 
$row = mysql_fetch_array($result);
$Anaam2 = $row["Achternamen"]; 

$insert_sql = "INSERT INTO duivengebruiker (Duif1, Duif2) VALUES ('$Vnaam1 $Anaam1','$Vnaam2 $Anaam2')";
mysql_query($insert_sql);