gpt4 book ai didi

php - PHP使用数组中的foreach定义变量

转载 作者:行者123 更新时间:2023-12-03 07:58:12 24 4
gpt4 key购买 nike

这是我的代码

<?php

$serverArray = [];
$con=mysqli_connect("--","--","--","--");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT ipaddress FROM gmodservers";
$result=mysqli_query($con,$sql);

while ($row=mysqli_fetch_array($result)) {
print_r($row);
array_push($serverArray,$row);
print_r($serverArray);
}


// Free result set
mysqli_free_result($result);

mysqli_close($con);

///ServerPull///

require __DIR__ . '/../SourceQuery/bootstrap.php';

use xPaw\SourceQuery\SourceQuery;

// For the sake of this example
Header( 'Content-Type: text/plain' );
Header( 'X-Content-Type-Options: nosniff' );
// Edit this ->
define( 'SQ_SERVER_PORT', 27015 );
define( 'SQ_TIMEOUT', 1 );
define( 'SQ_ENGINE', SourceQuery::SOURCE );
foreach ($serverArray as $value) {
define( 'SQ_SERVER_ADDR', $value );
// Edit this <-

$Query = new SourceQuery( );

try
{
$Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );

print_r( $Query->GetInfo( ) );
print_r( $Query->GetPlayers( ) );
print_r( $Query->GetRules( ) );
}
catch( Exception $e )
{
echo $e->getMessage( );
}
}

$Query->Disconnect( );

我在SQL数据库中有IP。我将IP移至阵列。然后,我想使用底部的功能来打印有关阵列中所有IP地址的所有信息。但是,我收到此错误:

Could not create socket: php_network_getaddresses: getaddrinfo failed: No such host is known.
Notice: Constant SQ_SERVER_ADDR already defined in G:\XAMPP\htdocs\PHP-Source-Query-master\Examples\list.php on line 40

最佳答案

由于您使用的是constant,因此无法重新声明,因此对于服务器地址,请使用变量。如下更改您的foreach循环

foreach ($serverArray as $value) {
//define( 'SQ_SERVER_ADDR', $value );
$server_address = $value;
// Edit this <-

$Query = new SourceQuery( );

try
{
$Query->Connect( $server_address, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );

print_r( $Query->GetInfo( ) );
print_r( $Query->GetPlayers( ) );
print_r( $Query->GetRules( ) );
}
catch( Exception $e )
{
echo $e->getMessage( );
}
}

编辑

在数据库结果的while循环中,您要将整行添加到数组中,只需要添加 ipaddress
while ($row=mysqli_fetch_array($result)) {
print_r($row);
array_push($serverArray,$row['ipaddress']);
print_r($serverArray);
}

关于php - PHP使用数组中的foreach定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47376363/

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