Poetry script help

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
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Poetry script help

Post by Smackie »

i been working on this script and for some reason i cant get it where if the user doesnt have any poems posted it would say ''Sorry but user hasnt posted a poem yet" but i hope someone can help me..
right now i set it to this
if ($row['poemname'] == '') {
it shows the users poem names and it also shows the "Sorry but user hasnt posted a poem yet" at the same time on every user.

if i change it to
if ($row['poemname'] == '1') {
it will only show the users poem names like it should but the users that dont have a poem posted it shows blank..

Code: Select all

<?php

$poemname = mysql_query("SELECT * FROM poems WHERE user_name like '".$_GET['user_name']."'");
	while ($row = mysql_fetch_array($poemname)) {

echo '<font class="txt"><a href="index.php?pages=poem&poemname='.$row['poemname'].'"><font class=\"txt"\ color=\"red"\>'.$row['poemname'].'</font></a><br></font>';

}
     if ($row['poemname'] == '') {

   echo "<font class=\"txt\">Sorry but this user hasn't submitted a Poem yet.</font>"; 

}
?>

Thank you
Smackie
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

<?php 

$query = "SELECT * FROM poems WHERE user_name like '".$_GET['user_name']; 

$result = mysql_query($query) or die(mysql_error());


if(mysql_num_rows($result) == 0) {
     echo "<font class=\"txt\">Sorry but this user hasn't submitted a Poem yet.</font>"; 
} else {

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

          echo '<font class="txt"><a href="index.php?pages=poem&poemname='.$row['poemname'].'"><font class=\"txt"\ color=\"red"\>'.$row['poemname'].'</font></a><br></font>'; 

      }
}
?>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Hmm - I don't think that will work. Remove the 'or die' from the end of the first result, or, if there are no poems, the script will 'die' at that point.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

A bit clearer:

Code: Select all

<?php

$query = "SELECT * FROM poems WHERE user_name like '".$_GET['user_name'];

$result = mysql_query($query); //removed from here


if(mysql_num_rows($result) == 0) {
     echo "<font class=\"txt\">Sorry but this user hasn't submitted a Poem yet.</font>";
} else {

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

          echo '<font class="txt"><a href="index.php?pages=poem&poemname='.$row['poemname'].'"><font class=\"txt"\ color=\"red"\>'.$row['poemname'].'</font></a><br></font>';

      }
}
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Grim... wrote:Hmm - I don't think that will work. Remove the 'or die' from the end of the first result, or, if there are no poems, the script will 'die' at that point.
Nope, the script will only "die" if there is an error with the query, NOT if it just doesn't return any results.

And it is best practice to always use the "or die" function to allow for easier debugging.

Trust me ! :wink:
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Will it?

Okay, ignore me!
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Pimptastic was right.
a die will occur only if the query is not formed properly or a table name or a field mentioned in the query is not actually found or mapped in the database :)
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

Pimptastic the script tha you gave me for some reason i get this error and im not for sure how to fix it :?
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''Smackie' at line 1
:? only thing is if i go to a different user it would change and be like use near "(the Users Name)' :?
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

There should be a close quote in the query. :)

The line:

Code: Select all

$query = "SELECT * FROM poems WHERE user_name like '".$_GET['user_name'];
Should be:

Code: Select all

$query = "SELECT * FROM poems WHERE user_name LIKE '".$_GET['user_name']."'";
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yup, i missed the last closing quote....whoopsie 8)

Want me to make your dinner for ya aswel :lol:
Post Reply