0

So I created this script to test out if I could output from my database. And it works fine inside the php, but when I trying extracting that data inside a div tag in HTML, it does not work. Any suggestions, thanks.

//Connection to database, working fine, top of page
<?php
require "include/mysql.php";
if (!$con) {
die('Could not connect: ' . mysql_error());
}
?>

//This is also working as intended
<?php 
$sql = "SELECT * FROM `LISTINGS` ORDER BY `LISTINGS`.`YA` DESC";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){

    echo $record['TITLE'];

}
?>

//Here is were the problem, in the H2 tag, i'm trying to output a title but it is not working. 
<div class="cards-row">

    <div class="card-row">
        <div class="card-row-inner">
            <div class="card-row-image" data-background-image="assets/img/tmp/product-1.jpg">


            </div><!-- /.card-row-image -->

            <div class="card-row-body">
                <h2 class="card-row-title"><?php echo $record['TITLE'];?>title</h2>
                <div class="card-row-content">Description</div><!-- /.card-row-content -->
            </div><!-- /.card-row-body -->

            <div class="card-row-properties">
                <dl>

                        <dd>Test</dd><dt>Visit Website</dt>
                        <dd>Test</dd><dt>More Info</dt>                  
                        <dd>Added</dd><dt>AddedDate</dt>
                        <dd>Visited</dd><dt>Visited</dt>

                </dl>
            </div><!-- /.card-row-properties -->
        </div><!-- /.card-row-inner -->
    </div><!-- /.card-row -->


</div><!-- /.cards-row -->

I know there is a lot of unfinished code but im just testing a few things out at the moment, sorry for bad grammar.

1
  • so if one the answer is acceptable for you, mark it please! Commented Mar 25, 2016 at 22:07

2 Answers 2

3

You cannot access $record outside of the while loop. If there is only one record you want to access just get rid of the loop and write $record = mysql_fetch_array($myData); on its own.

Sign up to request clarification or add additional context in comments.

6 Comments

Literally, i figured that our 10 secs before you commented, i'm dum. Thanks anyways
Or, alternatively, put the whole <div thingy> clause within the while loop.
@FelipeLopez No problem. It's an easy mistake to make.
One more question, as @Gralgrathor said, when i out the entire div tag inside the while loop there is many errors, any ideas that could be causing it
Could you provide the specific error? @PeppiAlexandrova has provided some code that does that so maybe compare your code with theirs.
|
2

And if there are many rows move the html inside the while loop:

<?php 
$sql = "SELECT * FROM `LISTINGS` ORDER BY `LISTINGS`.`YA` DESC";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
?>



<div class="cards-row">

<div class="card-row">
    <div class="card-row-inner">
        <div class="card-row-image" data-background-image="assets/img/tmp/product-1.jpg">


        </div><!-- /.card-row-image -->

        <div class="card-row-body">
            <h2 class="card-row-title"><?php echo $record['TITLE'];?>title</h2>
            <div class="card-row-content">Description</div><!-- /.card-row-content -->
        </div><!-- /.card-row-body -->

        <div class="card-row-properties">
            <dl>

                    <dd>Test</dd><dt>Visit Website</dt>
                    <dd>Test</dd><dt>More Info</dt>                  
                    <dd>Added</dd><dt>AddedDate</dt>
                    <dd>Visited</dd><dt>Visited</dt>

            </dl>
        </div><!-- /.card-row-properties -->
    </div><!-- /.card-row-inner -->
</div><!-- /.card-row -->
<?php 

}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.