skip to Main Content

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:xampphtdocsdeskgallery.php on line 572
Why this error?
………………………………………………………..

2

Answers


  1. 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
        }
    ?>
    Login or Signup to reply.
  2. 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>
        ";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search