Solved

PHP Array Looping using data from MYSQL

Posted on 2007-04-02
6
224 Views
Last Modified: 2008-02-01
how can i handle data from a table in mysql with php

i have a connection to a database

and i want to have a query that returns  set number of records with 5 fields.

ID, Title, Article, Link, Picture

i then want to use these records on my page.

i.e.

Picture - Title
Article
Link

 i have wrote the following but don't seem to be getting very far on writing a loop to print the data on the page

require("Conn.php");

$sql = 'SELECT ID, Title,Article,Picture,Link FROM tblArticles LIMIT 10';
$result = mysql_db_query($db1, $sql) or die ("Could not execute query");

$row = mysql_fetch_array($result);


thanks
0
Comment
Question by:Marcusw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18837410
require("Conn.php");

$sql = 'SELECT ID, Title,Article,Picture,Link FROM tblArticles LIMIT 10';
$result = mysql_db_query($db1, $sql) or die ("Could not execute query");

$row = mysql_fetch_array($result);

echo $row("Title")
echo $row("Article")
echo $row("Picture")
0
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18837417
actually .. you need to loop through

require("Conn.php");

$sql = 'SELECT ID, Title,Article,Picture,Link FROM tblArticles LIMIT 10';
$result = mysql_db_query($db1, $sql) or die ("Could not execute query");

while($row = mysql_fetch_array($result);)
{
echo $row("ID")
echo $row("Title")
echo $row("Article")
echo $row("Picture")
echo $row("Link")
}

}
0
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18837425
one to many closing brackets .. so remove one }
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 4

Accepted Solution

by:
secondv earned 500 total points
ID: 18837427
Something like...

<?php

require_once('Conn.php');

$sql = "
      SELECT ID, Title, Article, Picture, Link
      FROM tblArticles
      LIMIT 10
";
$result = mysql_db_query($db1, $sql) or die('Could not execute query');

while ($row = mysql_fetch_array($result))
{
      echo "
<tr>
      <td>$row[Picture] - $row[Title]</td>
</tr>
<tr>
      <td valign=\"top\">$row[Article]</td>
</tr>
<tr>
      <td>$row[Link]</td>
</tr>
";
}

?>
0
 
LVL 4

Expert Comment

by:secondv
ID: 18837435
ZSoderquist beat me to it =)
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 18840072
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("DBNAME");
$sql = "SELECT ID, Title,Article,Picture,Link FROM tblArticles LIMIT 10";
$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
$numofrows=mysql_num_rows($result);
if($numofrows>0)
{

      while($row=mysql_fetch_array($result))
      {
            $ID=$row['ID'];
            $Title=$row['Title'];
            $Article=$row['Article'];
            $Link=$row['Link'];
            $Picture=$row['Picture'];

            echo $Picture."-".$ID."-".$Title."<br>".$Article."<br>".$Link."<br><br>";
      }
}

Regards,
M.Raja
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question