doupian9490 2014-03-17 17:57
浏览 7

PHP输出作为数据库的链接

I have the following code that works by outputting as a link ( the link comes from a field in my database) I wish to do the same for the code below, however i cannot get it work, here is the example of what I have that works, and the code that i wish to make output as a link:

Working Code what I want it to look like

if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM adrenaline WHERE title LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){  
     echo '<br> <a href="../' . $row['description'] . '"> '. $row['title'] .'</a>';  
 }

}  

?>

And the code that i have at the moment, it works by be manually typing in the hyper link, however I wish to make it take the link from the database like the example above

//query the database
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");

//ferch the results / convert results into an array

    WHILE($rows = mysql_fetch_array($query)):

        $title = $rows['title'];

    echo "<a href='shard.php'>$title</a>";


endwhile;
?>

Many thanks!

  • 写回答

1条回答 默认 最新

  • douqin231881 2014-03-17 18:06
    关注

    I am not 100% certain if this is what you meant to ask... let me know in comments:

       <?PHP
        $query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
        if(mysql_num_rows($query)   >=  1) {
            while($rows =   mysql_fetch_array($query)) {
                echo sprintf("<a href=\"%s\">%s</a>", $rows["description"], $rows["title"]);
            }
        } else { echo "No hobbies found."; }
        ?>
    

    I believe you might have faced some syntax issues while dealing with quotes parsing a variable in <a html tag. Consider using sprintf something like in my example.


    I have also added a mysql_num_rows() just in case and you can see its a good fail-safe method incase there are no rews found on any select query.


    IMPORTANT: STOP using mysql_ functions because its deprecated from new PHP versions. Use PDO or mysqli instead.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法