?
Solved

PHP database help

Posted on 2003-10-28
4
Medium Priority
?
181 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
[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
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 500 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

Industry Leaders: 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!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

777 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