gpt4 book ai didi

php - 显示多行 MySQL

转载 作者:行者123 更新时间:2023-12-01 00:11:06 24 4
gpt4 key购买 nike

我的搜索结果编码 ID。

我正在显示搜索结果,目前它只会显示第一个结果,我怎样才能让它显示所有与搜索匹配的结果?

这是我的代码

      <?php
$name=$_GET["q"];
if ($name<=""){echo( "");
}
else
{
$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con)
{
die('Could not connect: line 513 ' . mysql_error());
}
mysql_select_db("cl49-xxx", $con)or die( "Unable to select database");
$result=mysql_query("SELECT * FROM products WHERE `prodname` LIKE '$name%' ")or die('Error: Line 519 ('.mysql_error().')' );

$row = mysql_fetch_array($result);
$prodID=$row['prodID'];
$prodname=$row['prodname'];
$catagory=$row['catagory'];
}
echo"
<div class='row-fluid portfolio-block'>
<div class='span5 portfolio-text'>
<img src='userpics/$prodID.jpg' height='80' width='80' alt='' />
<div class='portfolio-text-info'>
<h4>$prodname</h4>
<p></p>
</div>
</div>
<div class='span5'>
<div class='portfolio-info'>
Product ID
<span>$prodID</span>
</div>
<div class='portfolio-info'>
catagory
<span>$catagory</span>
</div>
</div>
<div class='span2 portfolio-btn'>
<a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View</span></a>
</div>
</div> ";

?>

任何帮助都会很棒!

最佳答案

像这样使用 while 循环:while ($row = mysql_fetch_array($result)) { }

<?php
$name = $_GET["q"];
if ($name <= "") {
echo ("");
}
else {
$con = mysql_connect("localhost", "cl49-XXX", "XXX");
if (!$con) {
die('Could not connect: line 513 ' . mysql_error());
}
mysql_select_db("cl49-xxx", $con) or die("Unable to select database");
$result = mysql_query("SELECT * FROM products WHERE `prodname` LIKE '$name%' ") or die('Error: Line 519 (' . mysql_error() . ')');
while ($row = mysql_fetch_array($result)) {
$prodID = $row['prodID'];
$prodname = $row['prodname'];
$catagory = $row['catagory'];
echo "
<div class='row-fluid portfolio-block'>
<div class='span5 portfolio-text'>
<img src='userpics/$prodID.jpg' height='80' width='80' alt='' />
<div class='portfolio-text-info'>
<h4>$prodname</h4>
<p></p>
</div>
</div>
<div class='span5'>
<div class='portfolio-info'>
Product ID
<span>$prodID</span>
</div>
<div class='portfolio-info'>
catagory
<span>$catagory</span>
</div>
</div>
<div class='span2 portfolio-btn'>
<a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View</span></a>
</div>
</div> ";
}
}
?>

关于php - 显示多行 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18236794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com