-4

I'm trying to use echo in this code

<?php
                            
    require 'connect.php';
    $query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());
    while($fetch = mysqli_fetch_array($query)){
           $id= $fetch['id'];
?>
    <?php echo "
        <li class='col-lg-4 col-md-6 col-dm-12'>
        <div class='da-card box-shadow'>
        <div class='da-card-photo'>
            <img src=' echo $fetch['video_name']' alt=''>
            <div class='da-overlay'>
                <div class='da-social'> 
                    <h4 class='mb-10 color-white pd-20'></h4>
                    <ul class='clearfix'>
                        <li>
                            <a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
                        </li>
                        <li>
                            <a href='#'><i class='fa fa-link'></i></a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
        </li>
    "; ?>
    <?php
    }
?>

I'm getting this error Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in C:\xampp\htdocs\desk\gallery.php on line 572 Why this error? .................................................................

Barmar
  • 741,623
  • 53
  • 500
  • 612
Moaz
  • 23
  • 1
  • 6
  • `echo $fetch['video']` should be `{$fetch['video']}`. – Barmar Oct 17 '22 at 21:40
  • 1
    You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 17 '22 at 21:49

2 Answers2

-3

You can escape the string and concatenate the variable in the place your hoping to have it like this:

<img src='". $fetch['video_name'] ."' alt=''>

<?php
                            
    require 'connect.php';
    $query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());
    while($fetch = mysqli_fetch_array($query)){
           $id= $fetch['id'];
?>
    <?php echo "
        <li class='col-lg-4 col-md-6 col-dm-12'>
        <div class='da-card box-shadow'>
        <div class='da-card-photo'>
            <img src='". $fetch['video_name'] ."' alt=''>
            <div class='da-overlay'>
                <div class='da-social'> 
                    <h4 class='mb-10 color-white pd-20'></h4>
                    <ul class='clearfix'>
                        <li>
                            <a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
                        </li>
                        <li>
                            <a href='#'><i class='fa fa-link'></i></a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
        </li>
    "; ?>
    <?php
    }
?>
JDawwgy
  • 888
  • 2
  • 18
-3

You can't use echo inside an echo or should I say string statement.

What you have to do is variable substitution like:

echo 'This is my variable '.$x.'.';

Or...

echo "This is my variable $x.";

In your code:

<?php
                            
    require 'connect.php';
    $query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());
    while($fetch = mysqli_fetch_array($query)){
           $id= $fetch['id'];
           $video_name = $fetch['video_name'];
?>
    <?php echo "
        <li class='col-lg-4 col-md-6 col-dm-12'>
        <div class='da-card box-shadow'>
        <div class='da-card-photo'>
            <img src='$video_name' alt=''>
            <div class='da-overlay'>
                <div class='da-social'> 
                    <h4 class='mb-10 color-white pd-20'></h4>
                    <ul class='clearfix'>
                        <li>
                            <a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
                        </li>
                        <li>
                            <a href='#'><i class='fa fa-link'></i></a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
        </li>
    "; ?>
    <?php
    }
?>

Also you can ditch the <?php ?>:

<?php
require 'connect.php';

$query = mysqli_query($conn, "SELECT * FROM `videos`") or die(mysqli_error());

while($fetch = mysqli_fetch_array($query)) {
    $id = $fetch['id'];
    $video_name = $fetch['video_name'];

    echo "
        <li class='col-lg-4 col-md-6 col-dm-12'>
            <div class='da-card box-shadow'>
                <div class='da-card-photo'>
                    <img src='$video_name' alt=''>
                    <div class='da-overlay'>
                        <div class='da-social'>
                            <h4 class='mb-10 color-white pd-20'></h4>
                            <ul class='clearfix'>
                                <li>
                                    <a href='' data-fancybox='images'><i class='fa fa-picture-o'></i></a>
                                </li>
                                <li>
                                    <a href='#'><i class='fa fa-link'></i></a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </li>
    ";
}
Marco
  • 7,007
  • 2
  • 19
  • 49