Solved

PHP database help

Posted on 2003-10-28
4
174 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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…

947 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