"; } e-6ren">
gpt4 book ai didi

php - 让两个脚本协同工作

转载 作者:行者123 更新时间:2023-11-30 23:33:42 27 4
gpt4 key购买 nike

我正在尝试使用以下脚本,以便第一个脚本的结果决定第二个脚本的输出。

<?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);

$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result))

if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
?>


<?php

$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);


$query2 = "SELECT * FROM comments";
$result2 = mysql_query($query2);

while ($row2 = mysql_fetch_assoc($result2))

if ( $row2['commentid'] % 2 ==0 )
{
echo $row2['name'];
echo "<br />";
}
else
{
echo $row2['name'];
}
?>

所以基本上,如果第一个脚本选择结果 1,我只想回显与该结果关联的名称。这些名称由 commentid 关联,其中奇数 commentid 将是结果 2,偶数 commentid 将是结果 1。有没有办法不使用 union 语句来做到这一点?

最佳答案

建议你创建一个函数然后调用它。像这样:

<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);

$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result))

if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
$names = get_names(1);
foreach ($names as $name) {
echo $name . "<br/>";
}
}
else
{
echo "<h4>Result 2</h4>";
$names = get_names(0);
foreach ($names as $name) {
echo $name . "<br/>";
}
}

Function get_names($pool_result)
{
$name_array = array();
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);

$query = "SELECT * FROM comments WHERE commentid % 2 = $pool_result";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{
array_push($name_array , $row['name']);
}
return $name_array;
}
?>

}

关于php - 让两个脚本协同工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com