help :s

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
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

help :s

Post by ericburnard »

this is driving me crazy.

Code: Select all

<?
include('http://eric.madashatters.com/header.inc');
?>

<?php
// Connecting, selecting database
mysql_connect('localhost', '*******', '**********')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('madashat_mail') or die('Could not select database');


echo "<BR>";

$result = mysql_query("select * from pm where user = ".$_GET['user']);


echo "<table>
<tr style='border: dotted 1px'>
<td>New</td>
<td>Subject</td>
<td>Sender</td>
<td>Date</td>
<td>Delete</td>";

$subject = mysql_result($result,0,"subject");
$new = mysql_result($result,0,"new");
$message = mysql_result($result,0,"message");
$date = mysql_result($result,0,"date");
$sender = mysql_result($result,0,"sender");
$id = mysql_result($result,0,"id");

echo "<tr style='border: dotted 1px'>";
echo "<td><div class='white'>$new</td>";
echo "<td><div class='white'>$subject</td>"; 
echo "<td><div class='white'>$sender</td>";
echo "<td><div class='white'>$date</td>";
echo "<td></td>"; 
echo "</tr>";

?>
</table>
<?
include('http://eric.madashatters.com/footer.inc');
?>
this is the code i have made and it is ment to display all of the messages that have got the username that is in the url (eg. http://eric.madashatters.com/mail.php?u ... sername_is[/b]) in the user colum in my table pm. i have tried it with the id number in the pm table and it works fine but not with any of the other colums. Help why is it not working :s

oh and if anybody knows how i can make the massages with the code below repeat so it shows them all that would be great (is it something to do with $i ??)

Code: Select all

$subject = mysql_result($result,0,"subject");
$new = mysql_result($result,0,"new");
$message = mysql_result($result,0,"message");
$date = mysql_result($result,0,"date");
$sender = mysql_result($result,0,"sender");
$id = mysql_result($result,0,"id");

echo "<tr style='border: dotted 1px'>";
echo "<td><div class='white'>$new</td>";
echo "<td><div class='white'>$subject</td>"; 
echo "<td><div class='white'>$sender</td>";
echo "<td><div class='white'>$date</td>";
echo "<td></td>"; 
echo "</tr>";
thanks again
eric
Last edited by ericburnard on Mon Aug 01, 2005 2:42 pm, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Please remove your username / password!
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

hawleyjr wrote:Please remove your username / password!
ok done anyone got any help :s pleeeeeeese

thanks
eric
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Put quotes around the username in your query.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: help :s

Post by timvw »

ericburnard wrote: $result = mysql_query("select * from pm where user = ".$_GET['user']);
Strings (i suppose username is a string, not a number) should be between ''.
Currently your code gives great results when i request: ?user=1 OR user LIKE '%'


So your code becomes:

Code: Select all

$username = mysql_real_escape_string($_GET['user']);
$query = "SELECT * FROM pm WHERE user='$username'";
$result = mysql_query($query) or die(mysql_error());
ericburnard wrote: $subject = mysql_result($result,0,"subject");
$new = mysql_result($result,0,"new");
$message = mysql_result($result,0,"message");
$date = mysql_result($result,0,"date");
$sender = mysql_result($result,0,"sender");
$id = mysql_result($result,0,"id");
An alternative is:

Code: Select all

$row = mysql_fetch_assoc($result);
extract($row);
You can use a loop that adds 1 to i every time, and then retrieve mysql_result($result, $i, "fieldname");

But usuallly you will see code like:

Code: Select all

while ($row = mysql_fetch_assoc($result))
{
  echo "<tr style='border: dotted 1px'>";
  echo "<td><div class='white'>{$row['new']}</td>";
  echo "<td><div class='white'>{$row['subject']}</td>"; 
  echo "<td><div class='white'>{$row['sender']}</td>";
  echo "<td><div class='white'>{$row['date']}</td>";
  echo "<td></td>"; 
  echo "</tr>";
}
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

yay thanks timvw thats great i have got it working now, thanks for the showing me the other ways of doing it.

for looping the results so that it displays all of the corret entrys would this code be correct???

Code: Select all

for($i=0;$i<10;$i++)

$username = mysql_real_escape_string($_GET['user']); 
$query = "SELECT * FROM pm WHERE user='$username'"; 
$result = mysql_query($query) or die(mysql_error()); 

echo "<table> 
<tr style='border: dotted 1px'> 
<td>New</td> 
<td>Subject</td> 
<td>Sender</td> 
<td>Date</td> 
<td>Delete</td>"; 

$subject = mysql_result($result,$i,"subject"); 
$new = mysql_result($result,$i,"new"); 
$message = mysql_result($result,$i,"message"); 
$date = mysql_result($result,$i,"date"); 
$sender = mysql_result($result,$i,"sender"); 
$id = mysql_result($result,$i,"id");
thanks again
Eric :d
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You should think for yourself... You're repeating the loop 10 times...
Well you are intending anyway.. If you have multiple commands/lines that need to be executed in a loop.. You should place braces around them..

for (...)
{
...
...
}


What happens if there are more or less results?
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

sorry it is met to say that

for($i=0;$i;$i++) i think :S:S:S

it would put errors if there were more or less results?

Eric

[edit] i dont understan the
for (...)
{
...
...
}
bit i tried it and i got no results shown at all
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i dont understan the for
It's all explained here: http://www.php.net/for

for($i=0;$i;$i++) i think :S:S:S
it would put errors if there were more or less results?
Questions to answer:
Would it raise errors if there are less rows available?
Would it raise errors if there are more rows available?
How can you determine the number of rows, and avoid the error?
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

okeys i have read though it and thing i understand it. i have managed to make it loop the results and the answers to the questions u gave me are that if there are less rows (which there are) then it will produce errors. but i guess if there are more rows then they will not be displayed unless you i have stated in the for() function that there should be more loops eg for ($i = 1; $i <= 50; $i++) or for ($i = 1; $i <= 100; $i++)

am i right :D

thanks
eric
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You're getting it ;)


Here is the last bit you need to know :)
How can you determine the number of rows, and avoid the error?
http://www.php.net/mysql_num_rows
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

ok now this bit is confusing me i dnt get it at all. dosnt that just tell you how many rows there are and not get rid of the errors?

Eric
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

But if you know how many rows there are... You know how many times your loop can run without you get an error because the row doesn't exist..

Code: Select all

$number_of_rows = mysql_num_rows($result);
for ($i = 0; $i < $number_of_rows; ++$i)
{
  // do my stuff here
}
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

timvw wrote:But if you know how many rows there are... You know how many times your loop can run without you get an error because the row doesn't exist..

Code: Select all

$number_of_rows = mysql_num_rows($result);
for ($i = 0; $i < $number_of_rows; ++$i)
{
  // do my stuff here
}
Is it that simple lol :S i think i must have been getting tired lol. Thank you everso much timvw. You have been an amazing help.

I think i might post the finished thing on here when its done (membership system, with blog, photo album and internal email :D) if you all dont mind. Im sure somebody might find it usefull.

Thanks again
Eric :D

[EDIT] Ohhh we have some kind of colour with the codes :D
Post Reply