$result = mysql_query()

前端 未结 2 1192
情歌与酒
情歌与酒 2020-12-18 10:40

I am brand new to php/mysql, so please excuse my level of knowledge here, and feel free to direct me in a better direction, if what I am doing is out of date.

I am p

相关标签:
2条回答
  • 2020-12-18 11:06

    It is hard to help without knowing more. You are pumping the results into an array, are you expecting to only return one result or many banner_headline results? If you will only ever get one result then all you need to do is something like this:

    PHP:

    $result = mysql_query("
        SELECT `banner_headline`
        FROM `low_engagement`
        WHERE `thread_segment` = 'a3'", $connection) or die(mysql_error());
    // This will get the zero index, meaning first result only
    $alt = mysql_result($result,0,"banner_headline");
    

    HTML:

    <html>
    <body>
    <!--- Rest of code -->
    <img src="" alt="<?php echo $alt ?>">
    

    On a side note, you should stop using mysql-* functions, they are deprecated.
    You should look into PDO or mysqli

    0 讨论(0)
  • 2020-12-18 11:08

    You are, as the comment says, using deprecated functions, but to answer your question, you should declare a variable to hold the value once your retrieve it from the database so that you can use it whenever your want.

    <?php
    $result = mysql_query("SELECT banner_headline FROM low_engagement WHERE thread_segment = 'a3'", $connection);
    if(!$result) {
        die("Database query failed: " . mysql_error());
    }
    
    $bannerHeadline = "";
    
    while ($row = mysql_fetch_array($result)) {
        $bannerHeadline = $row["banner_headline"];
    }
    
    echo $bannerHeadline; //use this wherever you want
    
    ?>
    
    0 讨论(0)
提交回复
热议问题