Solved

PHP Array Looping using data from MYSQL

Posted on 2007-04-02
6
221 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

679 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