简体   繁体   中英

How do I insert a PHP Variable inside a PHP Variable and Display it?

So, I'm creating a system that searches and displays results from a MySQL table database, and I'm trying to figure out how to store the result inside a php variable so I can format and process it rather than using a printf function. However, it doesn't seem to understand what I am putting inside the variable:

<?php
if(isset($_POST['submit']))
{ 
    if(isset($_GET['go']))
    { 
        if(preg_match("/[A-Z  | a-z | 0-9]+/", $_POST['search-input']))
        { 
            $searchcondition=$_POST['search-input']; 
            //connect  to the database 
            $database = "mysql";
            $hostname = "localhost";
            $link = new mysqli($hostname, "root", "", $database);
            //$link = mysqli_connect($hostname, "root", "", $database);
            if (!$link) 
            {
                echo "Error: Unable to connect to MySQL." . PHP_EOL;
                echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
                echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
                exit;
            }
$query1 = "SELECT `Title`, `Date` FROM `search-database` WHERE `Title` LIKE '%" . $searchcondition . "%' ";
//$query1 = "SELECT `ID`,`FirstName`, `LastName`, `Email`, `PhoneNumber` FROM `test` WHERE `FirstName` LIKE '%" . $searchcondition . "%' AND `Email` LIKE '%" . $searchcondition . "%' ";
if ($result = $link->query($query1)) 
{
    /* fetch associative array */
    while ($row = $result->fetch_assoc()) 
    {
        $string = '$row["Title"], $row["Date"]'; 
        echo $string;


        /* The $string variable above is not recognizing what I just put above. When I do no single quotes, it gives an error. When I do put single quotes, it prints out the actual characters "$row["Title"], $row["Date"]" rather than what the variables actually store.

        here is the Original Code (which does work): 
        printf ("%s, %s, %s, %s, %s\n", $row["ID"], $row["FirstName"], $row["LastName"], $row["Email"], $row["PhoneNumber"]);
        */
                }


    /* free result set */
    $result->free();
}
mysqli_close($link);
    }
    else
    { 
        echo  "<p>Please enter a search query</p>"; 
    } 
} 
}
?> 

The $string variable above is not recognizing what I placed inside it. When I don't put in single quotes, it shows an error. When I do put single quotes, it prints out the actual characters $row["Title"] , $row["Date"] rather than what the variables actually store.

Here is the Original Code (which works):

printf ("%s, %s\n", $row["Title"], $row["Date"]);

How do I get the php to display the same as the original printf function except by echoing a separate variable ($string) storing the printf contents, or is there an easier way to display the information and still be able to format it easily?

I would just put a comment but I don't have enough points. Would this not sufficient ?

<?php
    $string = $row["Title"] . ", " . $row["Date"];
    echo $string;
?>

you may concat any no of variables like below.

while ($row = $result->fetch_assoc()) 
{
    echo $string = $row["Title"].','. $row["Date"].'\n'; 

 }

If you already have printf , consider using sprintf , which is similar to printf , but stores the result in a variable instead of printing it:

<?php
    ....
    $string = sprintf ("%s, %s\n", $row["Title"], $row["Date"]);
    ....

Single quote is not needed in this case. Since you need "," in between the Strings, you can write the code like this

$string = $row["Title"].",". $row["Date"]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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