0

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?

2
  • If your printf version works, you can use sprintf. Commented Jul 22, 2017 at 3:52
  • Alternatively, you could use: $string = $row["Title"] . ', ' . $row["Date"] . PHP_EOL; Commented Jul 22, 2017 at 3:55

4 Answers 4

1

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"]; 
Sign up to request clarification or add additional context in comments.

Comments

1

you may concat any no of variables like below.

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

 }

Comments

1

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"]);
    ....

1 Comment

Oh, I did forget to mention that I was considering this as well. It didn't answer my original question, but it 100% works and clearly both $string = $row["Title"].",". $row["Date"]; and $string = sprintf ("%s, %s\n", $row["Title"], $row["Date"]); works.
1

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;
?>

3 Comments

you cannot concatenate strings with + in PHP.
in php concatenate is ., so $string = $row["Title"].', '.$row["Date"];
oh my bad haha I haven't programmed in PHP in a while

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.