Solved

PHP Array Looping using data from MYSQL

Posted on 2007-04-02
6
215 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now