php $result = mysql_query()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15617824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:37:44  来源:igfitidea点击:

$result = mysql_query()

phpmysql

提问by keithiopian

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.

我是 php/mysql 的新手,所以请原谅我在这里的知识水平,如果我所做的已经过时,请随时指导我朝着更好的方向发展。

I am pulling in information from a database to fill in a landing page. The layout starts with an image on the left and a headline to the right. Here, I am using the query to retrieve a page headline text:

我正在从数据库中提取信息以填写登录页面。布局从左侧的图像和右侧的标题开始。在这里,我使用查询来检索页面标题文本:

<?php
$result = mysql_query("SELECT banner_headline FROM low_engagement WHERE thread_segment = 'a3'", $connection);
if(!$result) {
    die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
    echo $row["banner_headline"];
}
?>

This works great, but now I want to duplicate that headline text inside the img alttag. What is the best way to duplicate this queries information inside the alt tag? Is there any abbreviated code I can use for this, or is it better to just copy this code inside the alt tag and run it twice?

这很好用,但现在我想复制img alt标签内的标题文本。在 alt 标签内复制此查询信息的最佳方法是什么?是否有任何我可以使用的缩写代码,或者最好将此代码复制到 alt 标签中并运行两次?

Thanks for any insight!

感谢您的任何见解!

采纳答案by woz

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

?>

回答by Jonathan Jones

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_headlineresults? If you will only ever get one result then all you need to do is something like this:

如果不了解更多,很难提供帮助。您正在将结果泵入一个数组,您希望只返回一个结果还是多个banner_headline结果?如果你只会得到一个结果,那么你需要做的就是这样:

PHP:

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:

<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 PDOor mysqli

附带说明一下,您应该停止使用mysql-*函数,它们已被弃用。
你应该调查PDOmysqli