在PHP / MySQL中显示链接? [英] Displaying links in PHP/MySQL?

查看:183
本文介绍了在PHP / MySQL中显示链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您在在MySQL JOIN查询中忽略null结果回答我的问题

我在localhost创建了自己的广播电台网站,位于 http://myradiostation.localhost

I've created my own radio station website in localhost, at http://myradiostation.localhost

这是代码:

<?php
    $connection = mysql_connect("localhost", "root", "PASSWORD") or die("Error connecting to database");
    mysql_select_db("radio1", $connection);
    $result = mysql_query("SELECT * FROM presenters;", $connection) or die("error querying database");
    $i = 0;
    while($result_ar = mysql_fetch_assoc($result)){
    ?>

这是HTML代码:

            <div class="divider"></div>
            <div class="main" style="width:552px;">
                <img src="<?php echo $result_ar['image']; ?>" width=115 height=60>
                <div class="time"><?php echo $result_ar['airtime']; ?></div>
                <div class="show"><h3><b><a href="<?php echo $result_ar['link']; ?>"><?php echo $result_ar['presenter']; ?></a></b></h3>
                    <p><?php echo $result_ar['showinfo']; ?></p></div>
                <div class="footer"></div>
            </div>
    <?php
    $i+=1;
    }
    ?>

确实工作,除了一件事 - 没有链接的内容仍然链接回页面本身,即使数据库列有一些空白内容。

It does work, except for one thing - the content without links still links back to the page itself, even though the database columns have some blank content.

这是SQL代码 - 在PHPmyadmin中创建一个名为radio1的数据库,代码如下: p>

Here's the SQL code - create a database called radio1 in PHPmyadmin and this code:

CREATE TABLE IF NOT EXISTS `presenters` (
  `presenter` varchar(255) NOT NULL,
  `link` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL,
  `airtime` time NOT NULL,
  `showinfo` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `presenters`
--

INSERT INTO `presenters` (`presenter`, `link`, `image`, `airtime`, `showinfo`) VALUES
('Non-Stop Nightshift', '', '', '01:00:00', 'More Music Variety through your weekend'),
('Greatest Hits', '', '', '06:00:00', 'Hit Music now!'),
('John Doe', 'http://www.myradiostation.localhost/johndoe', '', '08:00:00', 'Join John at the weekend');

除链接之外,它没有任何重大问题。

It works with no major issues, except the linking one.

它显示正确,如下所示:

It displays properly, like this:

http://i.stack.imgur.com/b91Dj.jpg

我该如何解决这个问题? (如果没有图像,我想我可以在字段中设置默认值),你会推荐什么?

How can I fix this? (if there's no images I suppose I could set a default value in the field though), and what would you recommend?

简而言之,我应该如何存储HTML链接一个PHPMyadmin数据库?

In short, how should I store HTML links in a PHPMyadmin database?

谢谢

推荐答案

你需要使用用于检测没有链接的行的条件语句。这样的事情:

You will need to use a conditional statement to detect rows without links. Something like this:

<div class="show"><h3><b>
<?php
if ( empty( $result_ar['link'] ) ) {
    echo $result_ar['presenter'];
}
else {
    echo '<a href="' . $result_ar['link'] . '">' . $result_ar['presenter'] . '</a>';
}
?>
</b></h3>

这篇关于在PHP / MySQL中显示链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆