Solved

PHP database help

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

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