简体   繁体   English

从 php 函数中获取变量

[英]getting a variable from a php function

I'm trying to set the image src from a variable which is defined in a function with a mysql query but it doesn't get the path.我正在尝试从一个变量中设置图像 src,该变量在具有 mysql 查询的函数中定义,但它没有获取路径。

Here is the function:这是函数:

function bilderAbfrage($richtigesArray) {
 echo "ayy";
 $zahl = $GLOBALS["zaehler"];
 echo "$zahl";
 $pdo = new PDO('mysql:host=localhost;dbname=datagame', 'root', 'root');
 $sql = "SELECT * FROM bilder WHERE DateIDFS = $richtigesArray[$zahl]";

    foreach ($pdo->query($sql) as $row) {
        echo $row['Path'];     // here it does print the right path of the image
        $bild1 = $row['Path'];
        $bild2 = $row['Path2'];
        $bild3 = $row['Path3'];
        $bild4 = $row['Path4'];

    }

}

Here are the images where it doesn't get the variables.这是它没有获取变量的图像。 I tried it with global but it didn't work:我用 global 试过了,但没有用:

<div class="row1">
        <img src="<?php global $bild1; echo $bild1; ?>" alt="<?php global $bild1; echo $bild1; ?>" id="pic1" class="pic1">
        <img src="<?php global $bild2; echo $bild2;?>" id="pic2" class="pic2">
        <?php echo "hey"; 
        ?>
      </div>
      <div class="row2">
        <img src="<?php global $bild3; echo $bild3; ?>" class="pic3">
        <img src="<?php global $bild4; echo $bild4; ?>" class="pic4">
      </div>

One little error I tried to make the bild1 - bild4 global after I gave them the path.在我给他们路径后,我试图使 bild1 - bild4 成为全局的一个小错误。

Correct:正确的:

function bilderAbfrage($richtigesArray){
echo "ayy";
$zahl = $GLOBALS["zaehler"];
echo "$zahl";
$pdo = new PDO('mysql:host=localhost;dbname=datagame', 'root', '5718orii');
$sql = "SELECT * FROM bilder WHERE DateIDFS = $richtigesArray[$zahl]";

    foreach ($pdo->query($sql) as $row) {
        global $bild1, $bild2, $bild3, $bild4;
        echo $row['Path'];
        $bild1 = $row['Path'];
        $bild2 = $row['Path2'];
        $bild3 = $row['Path3'];
        $bild4 = $row['Path4'];

    }

}

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

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