php help for a beginner please?

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!

Moderator: General Moderators

Post Reply
skanxalot
Forum Newbie
Posts: 1
Joined: Thu Mar 27, 2003 9:53 pm

php help for a beginner please?

Post 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
Malder
Forum Newbie
Posts: 13
Joined: Wed Mar 19, 2003 11:09 pm

Post 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");
mattd
Forum Newbie
Posts: 6
Joined: Wed Mar 26, 2003 10:10 pm

query?

Post by mattd »

what is your query :?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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 ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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:
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Just take away the "'s... so this $name = $row["Name"] for example, becomes this $name = $row[Name]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply