简体   繁体   English

从mysql数据库中提取数据时,如何在php字符串中使用多个属性?

[英]How do I use multiple attributes in a php string when pulling data from a mysql database?

I have a website and it uses a lytebox to show some images and other websites on top of a current website. 我有一个网站,它使用lytebox在当前网站的顶部显示一些图像和其他网站。 This all works fine, but I have transferred the data into a MYSQL database and now the class of the line does not work. 一切正常,但是我已将数据传输到MYSQL数据库中,现在该行的类不起作用。

This is what I had previously and this works: 这是我以前的工作,并且有效:

<div id="photo01">
<a href="images/strayKatts/photo01.JPG" title="" class="thickbox">
   <img src="images/strayKatts/photo01.JPG" alt="Stray Katts Creations" 
    width="150px" height="113px" border="0"/>
</a>
</div>

but because I am getting more and more data in the directory I have transferred my data into a mysql database to maintain a lot easier. 但是由于目录中越来越多的数据,我已将数据传输到mysql数据库中,以便于维护。

I can pull the data and everything, it is just not being displayed probably and I currently have this: 我可以提取数据和所有数据,可能只是不显示它,而我目前有:

<div id="photo01">
    <?php
        $category_set = mysql_query("SELECT * FROM currentmarkets 
                          WHERE id = '3' AND visible = '1';", $dbconnect);
            if (!$category_set) {
            die("Database query failed: " . mysql_error());
            }
            while ($titleItem = mysql_fetch_array($category_set)) {
            echo "<a href='{$titleItem["imageLink01"]}' title='' class='thickbox'><img src='{$titleItem["imageSrc01"]}' alt='{$titleItem["imageAlt01"]}' width='150px' height='113px' border='0'/>";}
    ?></div>

I am sure it has something to do with the way I am using my '' and my "" not sitting right. 我确信这与我使用“”和“”的方式不正确有关。

Any more advice / help? 还有其他建议/帮助吗?


EDIT!! 编辑!!

Sorry maybe I am not doing it right, but I have tried all the answer and none of them change anything even though they seem right - this is confusing:-) 对不起,也许我做的不对,但是我尝试了所有答案,即使它们看起来正确,也没有任何改变-这很令人困惑:-)

oh and sorry I forgot that - I'll never do that again:-) 哦,很抱歉,我忘了-我再也不会这样做了:-)

Try like this, using concatenation. 像这样使用串联。 Also try to just print the array fetched to see if it's a problem of your embedded html in php or to check if the query is not ok. 也可以尝试仅打印获取的数组,以查看这是否是您在php中嵌入的html的问题,或者检查查询是否正常。

<div id="photo01">
    <?php
        $category_set = mysql_query("SELECT * FROM currentmarkets WHERE id = '3' AND visible = '1';", $dbconnect);
            if (!$category_set) {
            die("Database query failed: " . mysql_error());
            }
            while ($titleItem = mysql_fetch_array($category_set)) {
            echo "<a href='".$titleItem["imageLink01"]."' title='' class='thickbox'><img src='".$titleItem["imageSrc01"]."' alt='".$titleItem["imageAlt01"]."' width='150px' height='113px' border='0'/>";}
    ?></div>

change: 更改:

echo "<a href='{$titleItem["imageLink01"]}' title='' class='thickbox'><img src='{$titleItem["imageSrc01"]}' alt='{$titleItem["imageAlt01"]}' width='150px' height='113px' border='0'/>";}

to: 至:

echo "<a href='" . $titleItem["imageLink01"] . "' title='' class='thickbox'><img src='" . $titleItem["imageSrc01"] . "' alt='" . $titleItem["imageAlt01"] . "' width='150px' height='113px' border='0'/></a>";}

In your while loop: 在您的while循环中:

while ($titleItem = mysql_fetch_array($category_set)) {
    echo "<a href='".$titleItem["imageLink01"]."' title='' class='thickbox'>";
    echo "<img src='".$titleItem["imageSrc01"]."' alt='".$titleItem["imageAlt01"]."'";
    echo " width='150px' height='113px' border='0'/></a>";
}

You forgot the </a> closing tag, and also need to just use concatenation to echo your vars from the query results. 您忘记了</a>结束标记,还需要仅使用串联从查询结果中echo您的var。

这可能应该可以工作,我看不到您的代码有任何其他错误:

echo "<a href='" . $titleItem["imageLink01"] . "' title='' class='thickbox'><img src='" . $titleItem["imageSrc01"] . "' alt='" . $titleItem["imageAlt01"]. "' width='150px' height='113px' border='0'/></a>";

Uses a heredoc when you've got a long mix of HTML and PHP variables: 当您将HTML和PHP变量混合使用时,请使用Heredoc

while ($titleItem = mysql_fetch_array($category_set)) {
    echo <<<EOL
<a href="{$titleItem['imageLink01']}" title="" class="thickbox">
    <img src="{$titleItem['imageSrc01']}" alt="{$titleItem['imageAlt01']}" width="150px" height="113px" border="0" />
</a>
EOL;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP和MySQL-在显示给用户之前,从数据库中提取数据时是否应验证/清除数据? - PHP and MySQL - Should I validate/sanitize my data when pulling it from my database before displaying to user? 如何将 MySQL 数据库中的字段作为字符串存储在 PHP 中? - How do i store a field from a MySQL database as a string in PHP? 如何使用PHP将HTML文档中的文本区域数据发送到MySQL数据库? - How do I use PHP to send text area data from an HTML document to MySQL database? 如何从php中的mysql数据库正确输出数据 - How do I output data from a mysql database in php correctly 我如何制作一个 PHP 表单,从 mySQL 数据库中提取数据并在按下按钮时将其显示在网页上? - how do i make a PHP form that pulls data from mySQL database and display it on webpage when the button is pressed? 如何从多个实体中选择属性? 使用MySQL和PHP - How do I select attributes from multiple entities? using mysql and php 如何使用SESSION数据来选择1个数据库条目,然后使用PHP和MySQL的表单中的数据插入到另一个表中? - How do I use SESSION data to SELECT 1 database entry and then INSERT into another table with data from a form as well with PHP and MySQL? PHP-将数据从MYSQL提取到JSON时,如何从UTC更改日期时间? - PHP - How to change datetime from UTC when pulling data from MYSQL into JSON? PHP / MYSQL:从数据库中提取信息 - PHP/MYSQL: Pulling information from database 我如何从html输入中添加变量并在从mysql数据中提取的url中显示 - How do I add variable from html input and display in a url pulling from mysql data as well
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM