Page 1 of 1
Poetry script help
Posted: Tue Aug 23, 2005 9:50 pm
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
Posted: Wed Aug 24, 2005 2:58 am
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>';
}
}
?>
Posted: Wed Aug 24, 2005 3:47 am
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.
Posted: Wed Aug 24, 2005 3:48 am
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>';
}
}
?>
Posted: Wed Aug 24, 2005 4:24 am
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 !

Posted: Wed Aug 24, 2005 5:04 am
by Grim...
Will it?
Okay, ignore me!
Posted: Wed Aug 24, 2005 5:23 am
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

Posted: Wed Aug 24, 2005 10:42 pm
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)'

Posted: Wed Aug 24, 2005 11:01 pm
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']."'";
Posted: Thu Aug 25, 2005 2:58 am
by JayBird
yup, i missed the last closing quote....whoopsie
Want me to make your dinner for ya aswel
