0

This code was working perfectlty as most of you said. I had a little issue with the path pointing to this script. The problem now is that I am having issues with the hyperlink with the href code line. I have a field in my database that is labled fulltext . I am trying to create a script that allows me display the content of fulltext (echo "{$row['fulltext']}.";) when I click on the Read More button. The hyperlink should be populated with the echo "{$row['title']}."; What mistake am I making by inserting a href="fulltext.php?=$row['fulltext']

<table>
<?php
    $dbhost = 'localhost';
    $dbuser = 'myusernm';
    $dbpass = 'mypwd';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass);

    if(! $conn )
    {
        die('Could not connect: ' . mysql_error());
    }

    $sql = 'SELECT title, introtext, created, created_by, catid FROM      mydb_items';
    mysql_select_db('muslimtimes360');
    $retval = mysql_query( $sql, $conn );

    if(! $retval )
    {
        die('Could not get data: ' . mysql_error());
    }

    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        echo '<tr>'; echo '<td>'; echo '<span class="post-date">'; echo   "{$row['created']}."; echo '</span>'; echo '</td>'; echo '</tr>';
        echo '<tr>';
        echo '<td>'; echo '<h2 class="blog-post-title">'; echo "{$row['title']}."; echo '</h2>'; echo '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<td>'; echo '<p>'; echo "{$row['introtext']}."; echo '</p>'; echo '</td>'; echo '</tr>';

        echo '<p>'; echo '<tr>';
        echo '<td>'; echo '<a href="#">'; echo '<input type="button" value="Read More" />'; echo '</a>'; echo '</td>';
        echo '</tr>';
        echo '</div>';
        echo '<div class="blog-meta">';
        echo '<img src="img/avatar.png" alt="Avatar" />';
        echo '<tr>'; echo '<td>'; echo '<h4 class="blog-meta-author">'; echo "{$row['created_by']}."; '</h4>';
        echo '<span>'; echo 'Category:'; echo "{$row['catid']}."; echo '</span>'; echo '</td>'; echo '</tr>';
        echo '</table>';
    }

    echo "";

    mysql_close($conn);
?>
6
  • 2
    Format your code properly please, it's a mess. Commented Sep 16, 2015 at 10:14
  • have you checked for any syntax error?? if not then please go through this first, after that check whether the content really don't appears. I mean, sometimes we set the text color same as that of background and we not able to see content, if possible share your css. Commented Sep 16, 2015 at 10:17
  • You should really be using MySQLi or PDO, MySQL is deprecated as of PHP 5.5.0. Take a look at stackoverflow.com/questions/8891443/… and stackoverflow.com/questions/13569/…. Commented Sep 16, 2015 at 10:25
  • If you are not getting any results then it is likely that while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) does not have any data to look through, which would mean your query is not returning any results. Commented Sep 16, 2015 at 10:28
  • i run this code in my local machine and its working file. it's may be your mysql version problem so can you please check first run this query (SELECT title, introtext, created, created_by, catid FROM mydb_items) on phpmyadmin ? Commented Sep 16, 2015 at 10:37

4 Answers 4

1

Please note mysql_fetch_array only need data (array), no need to give MYSQL_ASSOC, because you're conflating MySQL and MySQLi

and

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

Change this

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))

to this:

while($row = mysql_fetch_array($retval))

and inside while just use:

echo  $row['created'];

No need of use like this echo "{$row['created']}".

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

Comments

0

You are Using Old Mysql Syntax Which is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. If you are using Latest version of PHP then your code will not Work.Change the appropriate Syntax and Try again.

Comments

0

Try remove the first line table and put inside

example

while($row = mysql_fetch_array($retval))
    {
     echo '<table>';

Comments

0

I have tried to run your code with only adjusted connection credentials and table/column names and it works fine (using php 5.5 and php 5.3).

Isn't possible there is no record in the database? Another possibility could be some typo.

Edit: Also you have only one table opening tag while as many closing tags as there is records present. Try to inspect source code of the 'blank page'.

Edit2: Try running this snippet instead of your former code and let us know what happens:

<?php
    // connection string constants
    define('DSN', 'mysql:dbname=muslimtimes360;host=localhost');
    define('USER', 'myusernm');
    define('PASSWORD', 'mypwd');

    // pdo instance creation
    $pdo = new PDO(DSN, USER, PASSWORD);

    // query preparation
    $stmt = $pdo->query("
        SELECT title, introtext, created, created_by, catid
        FROM mydb_items
    ");

    // fetching results
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // if this returns 0 then it means no records are present
    echo count($result) . "\n";

    // the loop should print valid table
    foreach ($result as $index => $row) {
        if ($index == 0) echo '<table>';
        echo <<<HTM
<tr>
    <td>
        <span class="post-date">{$row['created']}</span>
    </td>
</tr>
<tr>
    <td>
        <h2 class="blog-post-title">{$row['title']}</h2>
    </td>
</tr>
<tr>
    <td>
        <p>
            {$row['introtext']}
        </p>
    </td>
</tr>

<tr>
    <td>
        <p>
            <a href="#"><input type="button" value="Read More" /></a>
        </p>
    </td>
</tr>
<tr>
    <td>
        <div class="blog-meta">
            <img src="img/avatar.png" alt="Avatar" />
            <h4 class="blog-meta-author">{$row['created_by']}</h4>
            <span>Category: {$row['catid']}</span>
        </div>
    </td>
</tr>
HTM;
        if ($index == (count($result)-1)) echo '</table>';
    }

4 Comments

I am using php 5.4. could that be the problem? I have also tried a couple of the suggestions here but none seems to work.
@nick PHP version shouldn't matter in this case. Another thing you can try is to set error_reporting(-1); on top of the script. Maybe some useful notices will appear.
I realized that my problem was the php include in my html was not pointing to the PHP script to retrieve the mysql data. Now everything works perfectly.
@nick: I am glad to hear that:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.