简体   繁体   English

无法从查询获取数组

[英]Cant get array to work from query

I need the result from a query to become an array and use that array to pull data from the database on a second php mysqli query. 我需要查询的结果成为一个数组,并使用该数组在第二个php mysqli查询中从数据库中提取数据。

<?php
include"connection.php";
$pos = mysqli_query($not,"SELECT * FROM equipos");
$logos = array();
while($row= mysqli_fetch_assoc($pos)){
$logos[] = "<br>'".$row['abrv']."'=>"."'".$row['logo']."'";
}
$logos = implode(",", $logos);

$enjuego = mysqli_query($not,"SELECT * FROM partidos WHERE dprt='ftbls'");
while($part=mysqli_fetch_array($enjuego)){
$liga=$part['serie'];
$eq1= $part['eq1'];
$eq1s= strtoupper($eq1);
$eq2= $part['eq2'];
$eq2s= strtoupper($eq2);

echo $logos[$eq1].'<br>';
}
?>

It gives me the same error over and over again. 一遍又一遍地给我同样的错误。 This is the closest I came but just doesn't work. 这是我最接近的,但是不起作用。 Can someone tell me what am I doing wrong? 有人可以告诉我我在做什么错吗?

The error I get is: Warning: Illegal string offset 'gua' in line 18 我得到的错误是:警告:第18行中的字符串偏移量'gua'不合法

Here you convert your $logos array variable to a string type: 在这里,您将$logos数组变量转换为字符串类型:

$logos = implode(",", $logos);

and then later at the end you want to access it again as if it were an array: 然后在最后,您想再次访问它,就好像它是一个数组一样:

echo $logos[$eq1].'<br>';

that is the error you are getting. 那就是你得到的错误。

you have a number ordered array for $logos. 您有一个$ logos的数字顺序数组。 you cant implode it just by implode(",", $logos), this way you are trying to implode a comma seperated array, (it should look like $logos=array("foo","bar"); to implode as comma seperated array. 您不能只通过implode(“,”,$ logos)来内爆,这样,您将试图内插逗号分隔的数组(它看起来像$ logos = array(“ foo”,“ bar”);内爆为逗号分隔的数组。

$logo[] => this should look like; $ logo [] =>这应该看起来像; $logo=array("0"=>"foo", "1"=>"bar") try dumping your array, you will see what is wrong (var_dump($logo);) after first while loop. $ logo = array(“ 0” =>“ foo”,“ 1” =>“ bar”)尝试转储您的数组,在第一次while循环后,您会发现出了什么问题(var_dump($ logo);)。

First of, remove the implode as that will convert your array to a string. 首先,删除爆破,因为这会将您的数组转换为字符串。

Then you are trying to get the key "eq1" from table "partidos" in the $logos array, which might not exist (because the array will like this array( 0 => 'first', 1 => 'second', 2 => 'third' ) ). 然后,您尝试从$ logos数组中的表“ partidos”获取键“ eq1”,该键可能不存在(因为该数组将喜欢此数组(0 =>'first',1 =>'second',2 =>'third'))。

You must have something that is common between "equipos" and "partidos", lets say "abrv" is the same thing as eq1 the solution would be: 您必须在“ equipos”和“ partidos”之间有一些共同点,可以说“ abrv”与eq1是相同的,解决方案是:

<?php

include"connection.php";

$pos = mysqli_query($not, "SELECT * FROM equipos");

$logos = array();
while($row = mysqli_fetch_assoc($pos)){
    $logos[$row['abrv']] = "<br>'".$row['abrv']."'=>"."'".$row['logo']."'";
}

$enjuego = mysqli_query($not, "SELECT * FROM partidos WHERE dprt='ftbls'");

while($part = mysqli_fetch_array($enjuego)){
    $liga = $part['serie'];
    $eq1 = $part['eq1'];
    $eq1s = strtoupper($eq1);
    $eq2 = $part['eq2'];
    $eq2s = strtoupper($eq2);

    echo $logos[$eq1].'<br>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM