我目前还在学习PHP,所以有些事情我仍然在努力。我一直在慢慢地学习,并且阅读了一些帮助我的教程,但是我弄不清楚这个问题。
我有一个数据库表(在mysql中),比如100个urls。有一列名为“url”,第二列“文本”。我已经有分页代码的工作,所以也将使用。
我想要做的是回显URL(它们都在我的站点根目录中的名为blog的文件夹中),但是使用文本作为链接。
因此,例如,我表中的前三行可能是:
url
001.php
002.php
003.php
文本
随机文本
一些随机文本
更多文本
当回显时,链接显示列文本中的文本,如下所示:
随机文本
一些随机文本
更多文本
并在单击时打开相关的url。
我猜它将需要某种循环来收集所有的URL,并保存我手动添加链接文本,然后我的分页代码将它们分开。
这是我第一次在这里问问题,所以如果不够清楚或者你需要更多的信息,请告诉我。
我在网上做过多次搜索,但似乎找不到教程。
发布于 2012-02-27 22:58:45
假设您连接到具有用户名"root“和密码"root”的本地mysql服务器,并将您的url存储在名为url_table的数据库url_database中的表中,您可以这样做:
$connection = mysql_connect("127.0.0.1","root","root"); // Connect to the mysql server
mysql_select_db("url_database"); // Open the desired database
$query = "SELECT url,text FROM url_table"; // Query to select the fields in each row
$result = mysql_query($query); // Run the query and store the result in $result
while($row = mysql_fetch_assoc($result)) // While there are still rows, create an array of each
{
echo "<a href='".$row['url']."'>".$row['text']."</a>"; // Write an anchor with the url as href, and text as value/content
}
mysql_close($connection); // close the previously opened connection to the database发布于 2012-02-27 22:58:08
你需要的是:
(请参阅数组()文档中关于这一点的更多信息)
While( $row = mysql_fetch_array($result) ){
echo '<a href='.$row['url'].'>'.$row['text'].'</a>';
}发布于 2012-02-27 22:59:47
下面是一个示例,您可以从以下示例开始:
$con = mysql_connect("host","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT the_url, the_text FROM my_table");
while($row = mysql_fetch_array($result))
{
echo '<a href="' . $row['the_url'] . '">"' . $row['the_text'] . '</a> <br />';
}
mysql_close($con);https://stackoverflow.com/questions/9473594
复制相似问题