Solved

PHP database help

Posted on 2003-10-28
4
173 Views
Last Modified: 2006-11-17
Hey,
Im learning databases, i got it to open the database and print the first results:
this is my code that prints the first thing in the database

<?php


$_host = "localhost";
$_user = "ultimate";
$_pwd = "password";
$_db = "ultimatewarez";
$handle = mysql_connect($_host,$_user,$_pwd);

//if ($handle) { $errorCode="";
//} else {
//$errorCode="Can't Open <B style="color:black;background-color:#ffff66">Database</B> ".$_db." ]";
//}

mysql_select_db($_db);

$strQuery = "SELECT * FROM movies";
$intQID = mysql_db_query($_db, $strQuery);
$QueryResult = mysql_fetch_assoc($intQID);
   $id = $QueryResult["ID"];
   $movie = $QueryResult["title"];
   $date = $QueryResult["date"];
   $imbd = $QueryResult["IMDB"];
   $discuss = $QueryResult["discuss"];
mysql_close($handle);




?>


<table width='650' bgcolor='black' align='center' class='normal2' cellpadding='1' cellspacing='1'>
  <tr>
    <td width='50'  bgcolor='#999999' align='center'><b>Discuss:</b></td>
    <td width='50' bgcolor='#777777' align='center'><b>IMBD:</b></td>
    <td width='180' bgcolor='#999999' align='center'><b>Filename:</b></td>
    <td width='70'  bgcolor='#999999' align='center'><b>Date:</b></td>
    <td width='40'  bgcolor='#777777' align='center'><b>Other:</b></td>
  </tr>
  <tr>
    <td width='50'  bgcolor='#999999' align='center'><a href=''>Discuss</a></td>
    <td width='50' bgcolor='#777777' align='center'><a href=''><? echo " $imbd "; ?></a></td>
    <td width='180' bgcolor='#999999' align='center'><a href=''><? echo " $movie "; ?></a></td>
    <td width='70'  bgcolor='#999999' align='center'><a href=''><? echo " $date "; ?></a></td>
    <td width='40'  bgcolor='#777777' align='center'><a ahref=''>Get it! </a></td>
  </tr>

  </table>


/////////////////////////////////////////////////////////////
 
Now what im needing help with is printing everything from the database in a for loop or something, and printing it in the tables.
If anyone can help me with a loop that prints everything in the database and prints it in a table, i would greatly appreciate it
Thanksz
0
Comment
Question by:markyello
  • 3
4 Comments
 

Expert Comment

by:mrjoe
ID: 9634061
Try this:

<?php


$_host = "localhost";
$_user = "ultimate";
$_pwd = "password";
$_db = "ultimatewarez";
$handle = mysql_connect($_host,$_user,$_pwd);

//if ($handle) { $errorCode="";
//} else {
//$errorCode="Can't Open <B style="color:black;background-color:#ffff66">Database</B> ".$_db." ]";
//}

mysql_select_db($_db);

$strQuery = "SELECT * FROM movies";
$result= mysql_db_query($_db, $strQuery);
$num=mysql_numrows($result);
mysql_close();
?>
<table width='650' bgcolor='black' align='center' class='normal2' cellpadding='1' cellspacing='1'>
  <tr>
    <td width='50'  bgcolor='#999999' align='center'><b>Discuss:</b></td>
    <td width='50' bgcolor='#777777' align='center'><b>IMBD:</b></td>
    <td width='180' bgcolor='#999999' align='center'><b>Filename:</b></td>
    <td width='70'  bgcolor='#999999' align='center'><b>Date:</b></td>
    <td width='40'  bgcolor='#777777' align='center'><b>Other:</b></td>
  </tr>
<?php
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"ID");
$movie=mysql_result($result,$i,"title");
$date=mysql_result($result,$i,"date");
$imdb=mysql_result($result,$i,"IMDB");
$discuss=mysql_result($result,$i,"discuss");
?>
  <tr>
    <td width='50'  bgcolor='#999999' align='center'><a href=''>Discuss</a></td>
    <td width='50' bgcolor='#777777' align='center'><a href=''><? echo " $imbd "; ?></a></td>
    <td width='180' bgcolor='#999999' align='center'><a href=''><? echo " $movie "; ?></a></td>
    <td width='70'  bgcolor='#999999' align='center'><a href=''><? echo " $date "; ?></a></td>
    <td width='40'  bgcolor='#777777' align='center'><a ahref=''>Get it! </a></td>
  </tr>
<?php
++$i;
}

?>
  </table>
0
 

Accepted Solution

by:
mrjoe earned 125 total points
ID: 9634137
Sorry, the loop section (starting with <?php $i=0; while ($i ..... ) Can actually be done simpler this way:

<?php
  while ( $row = mysql_fetch_array($result) ) {
?>
  <tr>
    <td width='50'  bgcolor='#999999' align='center'><a href=''>Discuss</a></td>
    <td width='50' bgcolor='#777777' align='center'><a href=''><? echo $row["IMDB"]; ?></a></td>
    <td width='180' bgcolor='#999999' align='center'><a href=''><? echo $row["title"]; ?></a></td>
    <td width='70'  bgcolor='#999999' align='center'><a href=''><? echo $row["date"]; ?></a></td>
    <td width='40'  bgcolor='#777777' align='center'><a ahref=''>Get it! </a></td>
  </tr>
<?php
}
?>

You don't need the $num=mysql_numrows($result); line either then.

Give it a try!
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9646335
A comment for mrjoe - depending on your PHP version and so forth you may need to add 'php' after every <? or it will not be recognized.

so it would be '<?php echo $row["IMDB"];' and so forth

Happy coding
Red010knight
0
 

Expert Comment

by:mrjoe
ID: 9646709
Completely right, good remark.

greetz
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

746 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

11 Experts available now in Live!

Get 1:1 Help Now