PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Just a simple guest book here, I just started this yesterday. Question is, I'm trying to pull the data from the table and display it. Its going through the while loop fine, there's just nothing in the variables. What's wrong?
<html>
<body>
<?php
$myHost = ''; // edit
$myUser = ''; // edit
$myPass = ''; // edit
$myDB = ''; // edit
// connection to mysql-server
$myConn = mysql_connect($myHost, $myUser, $myPass) or die(mysql_error());
// choosing database where sequencing query should take place
mysql_select_db($myDB, $myConn) or die(mysql_error());
$query = 'SELECT TimeStamp,Name,Last,email,comment FROM tablename'; // edit: tablename
// query records. The resultset is identified by $result
$result = mysql_query($query, $myConn) or die($query. ': '. mysql_error());
?>
<!-- you may leave and re-enter php-blocks freely -->
<!-- any black-colored-text here will not be processed by php, because: -->
<!-- anything outside a php-block is sent "as is" to the client -->
<table>
<tr>
<th colspan="4">
there are <?php echo mysql_num_rows($result); ?> records
</th>
</tr>
<?php
// re-entering php-block to perform a while-loop on all rows
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["TimeStamp"]; ?></td>
<td><?php echo $row["Name"]; ?></td>
<td><?php echo $row["Last"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<tr>
<td colspan="4" style="background-color: #FFFFA;">
<?php echo $row["comment"]; ?>
</td>
</tr>
<?php
// entering php-Block, just to end the while-loop
}
?>
</table>
</body>
</html>
http://www.php.net/manual/en/function.mysql-fetch-array.php wrote:mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
define "extreme"
but serious: it's not that bad. Since php uses references for every- and anything (including strings) using fetch_array instead of fetch_assoc should produce negligible overhead in almost any case.
But now I have to define "negligible" and "almost any case"
//TRY TO CONNECT TO YOUR DATABASE WITH THIS---->//
$dbh=mysql_connect ("localhost", "youruser", "yourpass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("your database");
//THAN USE THIS CODE TO RETRIEVE AND POST YOUR DATA//
$result = mysql_query("SELECT * FROM yourtable",$dbh);
$count=0;
nincha - if you are using mysql_fetch_array() there is no need to use mysql_result(), check out Volka's post with all the code - you shouldn't be mixing those two functions like that.