Page 1 of 1

php help for a beginner please?

Posted: Thu Mar 27, 2003 9:53 pm
by skanxalot
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?

Code: Select all

while ($row = mysql_fetch_array($result))
{
   $ts = $rowї"TimeStamp"];
   $name = $rowї"Name"];
   $last = $rowї"Last"];
   $email = $rowї"email"];
   $comment = $rowї"comment"];

   echo "<tr>
   <td>$ts</td>
   <td>$name</td>
   <td>$last</td>
					      
  <td>$email</td></tr>
  <tr> <td colspan=4 bgcolor="#FFFFA0">$comment</td>
  </tr>";
					 
&#125; //end while loop

Posted: Thu Mar 27, 2003 11:08 pm
by Malder
I hope you didn't forget to connect to database:

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("could not connect");
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

query?

Posted: Thu Mar 27, 2003 11:14 pm
by mattd
what is your query :?:

Posted: Fri Mar 28, 2003 1:23 am
by volka
just another style and a bit more error reporting.
If you feel confused, ignore this post ;)

Code: Select all

<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/ref.mysql.php

Posted: Fri Mar 28, 2003 9:44 am
by protokol
You guys all missed a key element. He is using mysql_fetch_array(). When he accesses the rows:

$row = mysql_fetch_array($blah);

he tries using $row['key_value']... What he WANTS to use is:

mysql_fetch_assoc()

I think this might help his problem a bit ;)

Posted: Fri Mar 28, 2003 9:48 am
by volka
nope ;)
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.

Posted: Mon Mar 31, 2003 7:54 am
by protokol
Damn, oh well. I can't be right 100% of the time :-)

But just the same, if mysql_fetch_array() does all of that it seems like extreme overkill.

Posted: Mon Mar 31, 2003 9:37 am
by volka
define "extreme" :D
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" :roll:

Posted: Mon Mar 31, 2003 12:26 pm
by nincha
//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;

while ($row = mysql_fetch_array($result))
{

$ts = mysql_result($result,$count,"TimeStamp");
$name = mysql_result($result,$count,"Name");
$last = mysql_result($result,$count,"Last");
$email = mysql_result($result,$count,"email");
$comment = mysql_result($result,$count,"comment");

echo "<tr>
<td>$ts</td>
<td>$name</td>
<td>$last</td>

<td>$email</td></tr>
<tr> <td colspan=4 bgcolor=\"#FFFFA0\">$comment</td>
</tr>";

$count++;
}

Good Luck.

Posted: Mon Mar 31, 2003 1:43 pm
by twigletmac
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.

Mac

Posted: Mon Mar 31, 2003 3:17 pm
by Gen-ik
Just take away the "'s... so this $name = $row["Name"] for example, becomes this $name = $row[Name]

Posted: Mon Mar 31, 2003 4:19 pm
by volka
uh? please read http://www.php.net/manual/en/language.t ... rray.donts and keep the " (maybe change them to ' but keep them) ;)

Code: Select all

$key = 'a';
$v = $arr[$key];  // not $arr["$key"], no error, no warning, just useless
// but
$v = $arr['a']; // not $arr[a], which produces a warning