Search code examples
phpmysqlwampserver

I am getting some syntax error in my PHP code, which i am unable to solve


I am getting some syntax error in my PHP code, which I am unable to solve. Please have a look at this and help me to solve this. Here Is The Screenshot.

I have attached the screenshot of the error which I am getting on line 14.

<?php
    $link = mysqli_connect('localhost','root','','satyam');
    if(isset($_POST['name']))
    {
        $name = $_POST['name'];
        $sql = "SELECT * FROM students WHERE name='".$name."'";
        $sql_run = mysqli_query($link,$sql);
        if($row=mysqli_fetch_array($sql_run))
        {
            $name = $row["name"];
            $roll = $row["roll"];
            $branch = $row["branch"];
            $address = $row["address"];
            echo 'Name: '.$name.' Roll: '.$roll.' Branch: '.$branch.' Address: '.$address';
        } else {
            echo "No Record Found";
        }
    }
?>

Solution

  • You added an extra unnecessary single quote (') end of the following line

    echo 'Name: ' . $name . ' Roll: ' . $roll . ' Branch: ' . $branch . ' Address: ' . $address;
    

    This code works fine.

    $link = mysqli_connect('localhost','root','','satyam');
    if(isset($_POST['name'])) {
        $name    = $_POST['name'];
        $sql     = "SELECT * FROM students WHERE name='" . $name."'";
        $sql_run = mysqli_query($link,$sql);
        if($row = mysqli_fetch_array($sql_run)) {
            $name    = $row["name"];
            $roll    = $row["roll"];
            $branch  = $row["branch"];
            $address = $row["address"];
            echo 'Name: ' . $name . ' Roll: ' . $roll . ' Branch: ' . $branch . ' Address: ' . $address;
        } else {
            echo "No Record Found";
        }
    }