我在mysql中有一个这样的表名为links_tbl,它有5行数据
ID, LINKurl LINKname
1 google.com GOOGLE
2
3
4
5
通过使用php我想将5行回显到html页面上,作为这样的链接:
echo "<li> <a href=$linkurl></a>$LINKname</li>"
我遇到的问题是我无法理解如何从表中获取所有LINKurl和LINKname行以在列表中回显它们。
任何人都可以帮我解决问题吗?
答案 0 :(得分:1)
您可以使用PDO进行查询:
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
$dbh = new PDO($dsn, $user, $password);
$sth = $dbh->prepare("SELECT LINKurl, LINKname FROM [YOUR TABLE]");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
foreach($sth->fetchAll()as $result)
{
echo "<li><a href=\"{$result[0]}\">{$result[1]}</a></li>"
}
获取的结果将包含字段
答案 1 :(得分:0)
$query = "select * from 'yourtbale'";
$result = mysql_query('yourdb',$query);
在您的视图文件中,循环结果
foreach($result as $value){
<a href='$value['linkurl']'>$value['linkname']</a>
}
答案 2 :(得分:0)
这应该有效:
//Database Settings
$host = "sql.yourserver.com";
$user = "username";
$pass = "password";
$dbnm = "database_name";
//Connect to Database
$conn = mysql_connect ($host, $user, $pass);
if ($conn) {
$db= mysql_select_db ($dbnm);
if (!$db) {
die ("Database Not Found");
}
} else {
notify("Fatal Error. Can not connect to Database", "");
}
//Form Query
$query = "SELECT * FROM `links_tbl`";
//Fetch Results
$data = mysql_query($query) or die(mysql_error());
//Start UL
echo "<ul>\n"
//Loop through results
while($info = mysql_fetch_array( $data ))
{
//echo the list item
echo "<li><a href=".$info['LINKurl'].">".$info['LINKname']."</a></li>\n";
}
//End UL
echo "</ul>\n"
请注意,您需要在输出名称后关闭<a>
标记。