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);
?>
nick
  • 333
  • 1
  • 4
  • 15
  • 2
    Format your code properly please, it's a mess. – Epodax Sep 16 '15 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. – Rohit Kumar Sep 16 '15 at 10:17
  • You should really be using MySQLi or PDO, MySQL is deprecated as of PHP 5.5.0. Take a look at http://stackoverflow.com/questions/8891443/when-should-i-use-mysqli-instead-of-mysql and http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons. – JoeP Sep 16 '15 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. – JoeP Sep 16 '15 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 ? –  Sep 16 '15 at 10:37
  • use error_reporting to see what is problem http://php.net/manual/tr/function.error-reporting.php also you can check error log files to get better information. – tanaydin Sep 16 '15 at 10:50

4 Answers4

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']}".

halfer
  • 19,824
  • 17
  • 99
  • 186
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
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.

0

Try remove the first line table and put inside

example

while($row = mysql_fetch_array($retval))
    {
     echo '<table>';
Fiido93
  • 1,918
  • 1
  • 15
  • 22
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>';
    }
helvete
  • 2,455
  • 13
  • 33
  • 37
  • 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 Sep 16 '15 at 11:02
  • @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. – helvete Sep 16 '15 at 12:24
  • 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 Sep 16 '15 at 17:21
  • @nick: I am glad to hear that:) – helvete Sep 16 '15 at 20:36