Selecting and Echoing 5 Latest Entries

CrazyImpling

New Member
Reaction score
3
I'm working on creating a CMS just because I'm bored of using premade things and wanted to make my own.

So I have the base code for my PHP statement:
PHP:
 <?php
                
                	$con = mysql_connect(localhost);
                	if (!$con)
                	{
	                	die('Could not connect: ' . mysql_error());
                	}
                	
                	mysql_select_db("ms_site", $con);
                	
                	$query = "SELECT * FROM ms_news";
                	$result = mysql_query($query);
                	
                	echo $result;
                	
                ?>

And it gives me an output of:
Code:
Resource id #3

Any help?
 

mase

____ ___ ____ __
Reaction score
154
PHP:
<?php
                
                    $con = mysql_connect(localhost);
                    if (!$con)
                    {
                        die('Could not connect: ' . mysql_error());
                    }
                    
                    mysql_select_db("ms_site", $con);
                    
                    $query = "SELECT * FROM ms_news";
                    $result = mysql_query($query);
                    while($row=mysql_fetch_array($result)){
                    $echo1[] = $row['NAMEOFROW1'];
                    $echo2[] = $row['NAMEOFROW2'];
                    $echoN[] = $row['NAMEOFROWN'];
                    }
                    $repeat = mysql_num_rows($result);
                    for($i=0;$i<$repeat;$i++){
                    echo $echo1[$i];
                    echo $echo2[$i];
                    echo $echoN[$i];
                    }
 ?>
 

SFilip

Gone but not forgotten
Reaction score
634
Just the query is not enough for any output...
PHP:
<?php
                
                    $con = mysql_connect(localhost);
                    if (!$con)
                    {
                        die('Could not connect: ' . mysql_error());
                    }
                    
                    mysql_select_db("ms_site", $con);
                    
                    $query = "SELECT * FROM ms_news";
                    $result = mysql_query($query);
                    $row = mysql_fetch_assoc($result)
                    
                    echo $row['put a column name here'];
                    
                ?>

Edit: It seems mase was faster...
 

CrazyImpling

New Member
Reaction score
3
Haha, I knew that.. I had the $result2 = mysql_fetch_array part in there, but I didn't know you needed to name the column, so I was just getting "Array". Anyways, one last question, could I display two columns, such as a title and the message?
 

mase

____ ___ ____ __
Reaction score
154
Haha, I knew that.. I had the $result2 = mysql_fetch_array part in there, but I didn't know you needed to name the column, so I was just getting "Array". Anyways, one last question, could I display two columns, such as a title and the message?

PHP:
<?php
                
                    $con = mysql_connect(localhost);
                    if (!$con)
                    {
                        die('Could not connect: ' . mysql_error());
                    }
                    
                    mysql_select_db("ms_site", $con);
                    
                    $query = "SELECT * FROM ms_news";
                    $result = mysql_query($query);
                    while($row=mysql_fetch_array($result)){
                    $echo1[] = $row['title'];
                    $echo2[] = $row['message'];
                    }
                    $repeat = mysql_num_rows($result);
                    for($i=0;$i<$repeat;$i++){
                    echo $echo1[$i];
                    echo $echo2[$i];
                    }
 ?>
 

DDRtists

ɹoʇɐɹǝpoɯ ɹǝdns
Reaction score
415
Why do it the hard way? :p

PHP:
<?php              
$con = mysql_connect(localhost);
if (!$con) die('Could not connect: ' . mysql_error());
                    
mysql_select_db("ms_site", $con);
                    
$query = "SELECT * FROM ms_news LIMIT 5";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)) $data[] = $row;

foreach( $data as $news )
{
	echo 'News: '.$news['content'];
}

 ?>
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
Don't put such things into your pages:
if (!$con) die('Could not connect: ' . mysql_error());

Not only does it look cheap if an error occurs, but it reveals sensitive information in case someone wants to exploit your "application". Catch your error and display a friendly error message, like apologizing for not being able to show the requested content, rather than stopping where you were and printing a bunch of MySQL errors.


While you're at it, consider switching to the MySQLi, or better PDO (with the MySQL driver), as the first one is a relict of software history with a limited functionality, be it security or feature wise. PDO is available since PHP5: keep in mind that PHP4 is considered end of life and won't be supported anymore after December 31th 2007.

$query = "SELECT * FROM ms_news LIMIT 5";
For performance reasons, don't select * but only query the fields you're going to need. There's no point in retrieving a few 5K text blocks, if all you're showing is a title, a link and a date.

Shouldn't you also "order by publish_date desc"?
 

DDRtists

ɹoʇɐɹǝpoɯ ɹǝdns
Reaction score
415
@enouwee: I Put that in there because he had it. If you need every row, you should select every row. ;)
I would recommend not using it as well.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top