Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP database help

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

Independent Software Vendors: 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

610 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