Solved

PHP Array Looping using data from MYSQL

Posted on 2007-04-02
6
222 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 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