[英]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. 因此,我正在创建一个系统,该系统搜索并显示MySQL表数据库中的结果,并且试图弄清楚如何将结果存储在php变量中,以便可以格式化和处理它,而不是使用printf
函数。 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. 上面的$ string变量无法识别我放置在其中的内容。 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. 当我用单引号引起来时,它将打印出实际字符$row["Title"]
, $row["Date"]
而不是变量实际存储的内容。
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? 除了回显存储printf
内容的单独变量($ string)之外,如何使php显示与原始printf函数相同的PHP,或者是否有更简单的方法来显示信息并仍然能够轻松地对其进行格式化?
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';
}
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"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.