• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Solved [PHP] foreach #help '-'

milbradt

New Member
Joined
Dec 25, 2011
Messages
177
Solutions
1
Reaction score
4
I'll cut to the chase.
Not Working:
PHP:
<?php
#CONFIGS
require("../var/www/config.php");
$connection = mysql_connect($config['database']['host'], $config['database']['login'], $config['database']['password']);
mysql_select_db($config['database']['database'], $connection);
#ENDCONFIGS

mysql_query('TRUNCATE TABLE  `top_guilds`', $connection);
foreach(mysql_query(' SELECT  `g`.`id` AS `id`, `g`.`name` AS `name`, SUM(`p`.`level`) AS `level`, COUNT(`p`.`name`) AS `count`, AVG(`p`.`level`) AS `average`, MIN(`p`.`level`) AS `min`, MAX(`p`.`level`) AS `max`
                    FROM `players` p LEFT JOIN `guild_ranks` gr ON `p`.`rank_id` = `gr`.`id` LEFT JOIN `guilds` g ON `gr`.`guild_id` = `g`.`id` WHERE `g`.`id` = `g`.`id` GROUP BY `name` ORDER BY `level` DESC LIMIT 5 ', $connection) as $guild)
    {
        $Points = $guild['level'] + $guild['count'] + round($guild['average']) + $guild['min'] + $guild['max'];
        $query = "INSERT INTO top_guilds (id, points, name) VALUES ('".$guild['id']."','".$Points."', '".$guild['name']."')";
        mysql_query($query, $connection);
    }
?>

error in foreach =\
Code:
PHP Warning:  Invalid argument supplied for foreach()


If use.. mysql_fetch_array()
$result = mysql_fetch_array($search);
foreach($result as $guild)
error:
Code:
PHP Warning:  Illegal string offset 'level' in /home/teste.php on line 15
PHP Warning:  Illegal string offset 'count' in /home/teste.php on line 15
PHP Warning:  Illegal string offset 'average' in /home/teste.php on line 15
PHP Warning:  Illegal string offset 'min' in /home/teste.php on line 15
PHP Warning:  Illegal string offset 'max' in /home/teste.php on line 15
PHP Warning:  Illegal string offset 'id' in /home/teste.php on line 16
PHP Warning:  Illegal string offset 'name' in /home/teste.php on line 16
Can someone help me?
 
Last edited:
Try this and post the results ;)

PHP:
<?php
phpinfo();<?php
#CONFIGS
require("../var/www/config.php");

$mysqli = new mysqli($config['database']['host'], $config['database']['login'], $config['database']['password']), $config['database']['database'];

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$clearTopGuildsQuery = "TRUNCATE TABLE  `top_guilds`";
//$mysqli->query($clearTopGuildsQuery);

$query = ' SELECT  `g`.`id` AS `id`, `g`.`name` AS `name`, SUM(`p`.`level`) AS `level`, COUNT(`p`.`name`) AS `count`, AVG(`p`.`level`) AS `average`, MIN(`p`.`level`) AS `min`, MAX(`p`.`level`) AS `max`
                    FROM `players` p LEFT JOIN `guild_ranks` gr ON `p`.`rank_id` = `gr`.`id` LEFT JOIN `guilds` g ON `gr`.`guild_id` = `g`.`id` WHERE `g`.`id` = `g`.`id` GROUP BY `name` ORDER BY `level` DESC LIMIT 5 ';

                   
if ($result = $mysqli->query($query)) {
$result->fetch_assoc();

var_dump($result);
}

/* close connection */
$mysqli->close();                   
?>
 
Try this and post the results ;)

PHP:
<?php

$mysqli = new mysqli('127.0.0.1', 'root', 'password', 'database');

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$clearTopGuildsQuery = "TRUNCATE TABLE  `top_guilds`";
//$mysqli->query($clearTopGuildsQuery);

$query = ' SELECT  `g`.`id` AS `id`, `g`.`name` AS `name`, SUM(`p`.`level`) AS `level`, COUNT(`p`.`name`) AS `count`, AVG(`p`.`level`) AS `average`, MIN(`p`.`level`) AS `min`, MAX(`p`.`level`) AS `max`
                    FROM `players` p LEFT JOIN `guild_ranks` gr ON `p`.`rank_id` = `gr`.`id` LEFT JOIN `guilds` g ON `gr`.`guild_id` = `g`.`id` WHERE `g`.`id` = `g`.`id` GROUP BY `name` ORDER BY `level` DESC LIMIT 5 ';

               
if ($result = $mysqli->query($query)) {
$result->fetch_assoc();

var_dump($result);
}

/* close connection */
$mysqli->close();               
?>
Because some errors used direct information.

Result:
Code:
object(mysqli_result)#2 (5) {
  ["current_field"]=>
  int(0)
  ["field_count"]=>
  int(7)
  ["lengths"]=>
  array(7) {
    [0]=>
    int(4)
    [1]=>
    int(12)
    [2]=>
    int(6)
    [3]=>
    int(3)
    [4]=>
    int(8)
    [5]=>
    int(1)
    [6]=>
    int(3)
  }
  ["num_rows"]=>
  int(5)
  ["type"]=>
  int(0)
}
 
Last edited:
according to your result the final code should look like this:

PHP:
<?php
//CONFIGS
require("../var/www/config.php");

$mysqli = new mysqli($config['database']['host'], $config['database']['login'], $config['database']['password']), $config['database']['database'];

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$clearTopGuildsQuery = "TRUNCATE TABLE  `top_guilds`";
$mysqli->query($clearTopGuildsQuery);

$query = ' SELECT  `g`.`id` AS `id`, `g`.`name` AS `name`, SUM(`p`.`level`) AS `level`, COUNT(`p`.`name`) AS `count`, AVG(`p`.`level`) AS `average`, MIN(`p`.`level`) AS `min`, MAX(`p`.`level`) AS `max`
                    FROM `players` p LEFT JOIN `guild_ranks` gr ON `p`.`rank_id` = `gr`.`id` LEFT JOIN `guilds` g ON `gr`.`guild_id` = `g`.`id` WHERE `g`.`id` = `g`.`id` GROUP BY `name` ORDER BY `level` DESC LIMIT 5 ';

                 
if ($result = $mysqli->query($query)) {
$guilds = $result->fetch_assoc();

foreach ($guilds as $guild) {
    $Points = $guild['level'] + $guild['count'] + round($guild['average']) + $guild['min'] + $guild['max'];
    $createTopGuildsQuery = "INSERT INTO top_guilds (id, points, name) VALUES ('".$guild['id']."','".$Points."', '".$guild['name']."')";
    $mysqli->query($createTopGuildsQuery);
}

}

/* close connection */
$mysqli->close();                 
?>

in case this doesn't work please post the error(s) you're getting ;)
 
according to your result the final code should look like this:
in case this doesn't work please post the error(s) you're getting ;)

I had achieved through their code. =) Thanks!!!

It worked perfectly, looked like this:
PHP:
$mysqli = new mysqli($config['database']['host'], $config['database']['login'], $config['database']['password']), $config['database']['database'];

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$clearTopGuildsQuery = "TRUNCATE TABLE  `top_guilds`";
$mysqli->query($clearTopGuildsQuery);

$query = ' SELECT  `g`.`id` AS `id`, `g`.`name` AS `name`, SUM(`p`.`level`) AS `level`, COUNT(`p`.`name`) AS `count`, AVG(`p`.`level`) AS `average`, MIN(`p`.`level`) AS `min`, MAX(`p`.`level`) AS `max`
                    FROM `players` p LEFT JOIN `guild_ranks` gr ON `p`.`rank_id` = `gr`.`id` LEFT JOIN `guilds` g ON `gr`.`guild_id` = `g`.`id` WHERE `g`.`id` = `g`.`id` GROUP BY `name` ORDER BY `level` DESC LIMIT 5 ';

foreach($mysqli->query($query) as $guild) 
    {
        $Points = $guild['level'] + $guild['count'] + round($guild['average']) + $guild['min'] + $guild['max'];
        $mysqli->query("INSERT INTO top_guilds (id, points, name) VALUES ('".$guild['id']."','".$Points."', '".$guild['name']."')");
    }

/* close connection */
$mysqli->close();                  
?>
 
Back
Top