Solved

PHP Array Looping using data from MYSQL

Posted on 2007-04-02
6
220 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

828 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