Jump to content

[SOLVED] Display information from the MySQL Database


topflight

Recommended Posts

Hello all,

  I have created this code to display information from the MySQL database and when I go to the page all I get is a white page. May somebody please help me or give me advise how to do this in the future. This is my code.

 

<?

 

$query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'";

 

$mquery = mysql_query($query);

 

$num = mysql_num_rows($mquery) or die(mysql_error());

 

 

 

 

?>

<table width="10" border="1" cellspacing="0" cellpadding="0">

  <tr>

    <th scope="row">Author Name</th>

  </tr>

  <tr>

    <th scope="row">Date</th>

  </tr>

  <tr>

    <th scope="row">Topic</th>

  </tr>

  <tr>

    <th scope="row">Articale</th>

  </tr>

</table>

 

 

 

<tr bgcolor=#EEEEEE>

<td><?= "$num[authorname]"; ?></td>

<td><?= "$num[date]"; ?></td>

<td><?= "$num[newstitle]"; ?></td>

<td><?= "$num[news];" ?></td>

</tr>

 

 

</table>

Link to comment
Share on other sites

I have modified the code to this:

 

<?

ini_set('display_errors', 1);
error_reporting(E_ALL);


$query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'";

$mquery = mysql_query($query);

$num = mysql_num_rows($mquery) or die(mysql_error());




?>
<table width="10" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="row">Author Name</th>
  </tr>
  <tr>
    <th scope="row">Date</th>
  </tr>
  <tr>
    <th scope="row">Topic</th>
  </tr>
  <tr>
    <th scope="row">Articale</th>
  </tr>
</table>



<tr bgcolor=#EEEEEE>
<td><?= "$num[authorname]"; ?></td>
<td><?= "$num[date]"; ?></td>
<td><?= "$num[newstitle]"; ?></td>
<td><?= "$num[news];" ?></td>
</tr>


</table>

 

and now I am receiving the following message

 

Notice: Undefined variable: newstitle in C:\xampp\htdocs\viewnews.php on line 29

 

And what are short tags?

 

 

Link to comment
Share on other sites

I will use <?php instead thanks, now about the variable situation newtilte is a field name in the table in the database. I want to try have like a news system where I can input news in and it will show I have the database set up correctly? So I how can I pull the information from the database to show like a regular news system I am still trying to learn all this php lol. Also when I post code on here how can I have in the PHP colors.

Link to comment
Share on other sites

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'";

$mquery = mysql_query($query) or die(mysql_error());  //added this to see if problem with query

$num = mysql_num_rows($mquery) or die(mysql_error());  //this tells you how many rows from your database were returned, not the actual data
?>
<table width="10" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="row">Author Name</th>
  </tr>
  <tr>
    <th scope="row">Date</th>
  </tr>
  <tr>
    <th scope="row">Topic</th>
  </tr>
  <tr>
    <th scope="row">Articale</th>
  </tr>
<?php
foreach($mquery as $data){  //this will loop through your row data returned from the db
    echo "<tr bgcolor=#EEEEEE>\n";
    echo "<td>{$data["authorname"]}</td>\n";
    echo "<td>{$data["date"]}</td>\n";
    echo "<td>{$data["newstitle"]}</td>\n";
    echo "<td>{$data["news"]}</td>\n";
    echo "</tr>\n";
}
?>
</table>

Link to comment
Share on other sites

Well so far I created 3 pages page I is the actual form where u submit the news basic html. Page II check page I to make sure that the user put everything in and then insert it into the database. Page III which is this one displays the article like a regular CMS system. What I am trying to do is basically pull the information out of the database and display it like on a homepage something like that. If I am not making sence forgive me I am still trying to learn php. I have two books and I still don't know as much as I should.

Link to comment
Share on other sites

topflight waht cronix means by "not defined" is that $newstitle does not have a value. Therefore you are asking the db to return ONE news result  labeled with with an empty title.

 

Also the wrong array is used in your initial code ... you should be using the following...

<tr bgcolor=#EEEEEE>
<td><?= "$mquery[authorname]"; ?></td>
<td><?= "$mquery[date]"; ?></td>
<td><?= "$mquery[newstitle]"; ?></td>
<td><?= "$mquery[news];" ?></td>
</tr>

Link to comment
Share on other sites

ok now I am receiving this message.

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\viewnews.php on line 48

 

also may you guys please tell me where did u learn so I can help people out I tried W3SCHOOLS,php.net,youtube(lol) Php and mysql for dummies sam teach your self php and mysql ALL in one. And I still fill dumb lol.

Link to comment
Share on other sites

We need to see your code.

 

also may you guys please tell me where did u learn so I can help people out I tried W3SCHOOLS,php.net,youtube(lol) Php and mysql for dummies sam teach your self php and mysql ALL in one. And I still fill dumb lol.

 

Theres a good (free) book in my sig. Hudzilla. Don't try and skim though, If you take your time and read it all (cover to cover so to speak) you will have a good foundation.

Link to comment
Share on other sites

Thanks

 

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$query ="SELECT * FROM `news` ORDER BY `date` DESC LIMIT 5";

$mquery = mysql_query($query) or die(mysql_error());  //added this to see if problem with query

$num = mysql_num_rows($mquery) or die(mysql_error());  //this tells you how many rows from your database were returned, not the actual data
?>
<table width="10" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="row">Author Name</th>
  </tr>
  <tr>
    <th scope="row">Date</th>
  </tr>
  <tr>
    <th scope="row">Topic</th>
  </tr>
  <tr>
    <th scope="row">Articale</th>
  </tr>
</table>
<?php
foreach($mquery as $data){  //this will loop through your row data returned from the db
    echo "<tr bgcolor=#EEEEEE>\n";
    echo "<td>{$data[authorname]}</td>\n";
    echo "<td>{$data[date]}</td>\n";
    echo "<td>{$data[newstitle]}</td>\n";
    echo "<td>{$data[news]}" ?>}</td>\n";
    <?
echo "</tr>\n";

}

?>
</table>

 

 

Link to comment
Share on other sites

My bad, I made an error...

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$query ="SELECT * FROM `news` ORDER BY `date` DESC LIMIT 5";

$mquery = mysql_query($query) or die(mysql_error());  //added this to see if problem with query

$num = mysql_num_rows($mquery) or die(mysql_error());  //this tells you how many rows from your database were returned, not the actual data
?>
<table width="10" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="row">Author Name</th>
  </tr>
  <tr>
    <th scope="row">Date</th>
  </tr>
  <tr>
    <th scope="row">Topic</th>
  </tr>
  <tr>
    <th scope="row">Articale</th>
  </tr>
<?php
if($num>0){
    while($data = mysql_fetch_assoc($mquery){  //this was changed
        echo "<tr bgcolor=#EEEEEE>\n";
        echo "<td>{$data["authorname"]}</td>\n";
        echo "<td>{$data["date"]}</td>\n";
        echo "<td>{$data["newstitle"]}</td>\n";
        echo "<td>{$data["news"]}</td>\n";
        echo "</tr>\n";
    }
}
?>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.