morrisjohnny Posted August 14, 2006 Share Posted August 14, 2006 Hello, I'm totaly new to PHP and was wondering if i could get some help ( i have read the w3schools page)I was wanting to know:When a person who was logged in has 1 unread mailed how to display textI do use Mysql tables, and i believe it has somthing to do with this: <? $lijstGebruikers = "SELECT * FROM inbox WHERE reciever='$cookieusername' AND unread='0'"; $resultLijstGebruikers = mysql_query($lijstGebruikers); $messag = mysql_num_rows($resultLijstGebruikers); ?> <tr><td class="menuItem">- <a class="menuItem" href="inbox.php">Inbox<? if ($messag > 0) { echo " [$messag new]"; } ?></a></td></tr> Thanks in advance-Jny Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 You're probably on the right track. But, semantically, I think it would either be unread=1 or read=0. Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 Oka yi added that in but i still recive a warning saying.Warning: mysql_query(): Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/jnymris/public_html/Maffia (05-05-05)/Info.php on line 26Warning: mysql_query(): A link to the server could not be established in /home2/jnymris/public_html/Maffia (05-05-05)/Info.php on line 26Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/jnymris/public_html/Maffia (05-05-05)/Info.php on line 27 Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 OK, well that doesn't have anything to do with your code. You are not connected to the SQL server. Since it gives the user as nobody@localhost, and no password, I'm thinking you probably aren't even trying to connect at all. You need to use this function once on each page you want to access the database to connect:http://www.php.net/manual/en/function.mysql-connect.phpYou give that function the server name, SQL user name, and SQL password, which are all set up in the MySQL software. Once you have connected to the server, you use this function to select which database you want to work with:http://www.php.net/manual/en/function.mysql-select-db.phpAfter you are connected and have selected a database, then you can use mysql_query to start sending queries to the database server.Here's an example from php.net: <?php$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');if (!$link) { die('Not connected : ' . mysql_error());}// make foo the current db$db_selected = mysql_select_db('foo', $link);if (!$db_selected) { die ('Can\'t use foo : ' . mysql_error());}?> Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 Okay i hace "conneted to mysql" as the Text says so But i now recive this error Parse error: syntax error, unexpected T_STRING in /home2/jnymris/public_html/Maffia (05-05-05)/Info.php on line 13And this is my code for the whole page<html><head> <title>Mafia :: v0.10 Beta</title> <link rel="stylesheet" type="text/css" href="src/standard.css" /><?php$link = mysql_connect('localhost', '*******', '*******');if (!$link) { die('Could not connect: ' . mysql_error());} $lijstGebruikers = "SELECT * FROM inbox WHERE reciever='$cookieusername' AND unread='0'"; $resultLijstGebruikers = mysql_query($lijstGebruikers); $messag = mysql_num_rows($resultLijstGebruikers); "<a href ="inbox.php" target ="showframe">Inbox <? if ($messag > 0) { echo " [$messag new]"; } ?></a>"mysql_close($link);?> <script type="text/javascript">var d = new Date()document.write(d.getDate())document.write(".")document.write(d.getMonth() + 1)document.write(".")document.write(d.getFullYear())</script><script type="text/javascript">var d = new Date()document.write(d.getHours())document.write(".")document.write(d.getMinutes())document.write(".")document.write(d.getSeconds())</script></head></html> Link to comment Share on other sites More sharing options...
aspnetguy Posted August 14, 2006 Share Posted August 14, 2006 you have to put the <?php ?> before the <html> and make sure there is no extra lines before the php either. <?php has to be the first 5 characters of the file. Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 Okay i have Changed the file to start with <?phpI now recive the very simlier error Parse error: syntax error, unexpected T_STRING in /home2/jnymris/public_html/Maffia (05-05-05)/Info.php on line 9New Code<?php$link = mysql_connect('localhost', '***', '***');if (!$link) { die('Could not connect: ' . mysql_error());} $lijstGebruikers = "SELECT * FROM inbox WHERE reciever='$cookieusername' AND unread='0'"; $resultLijstGebruikers = mysql_query($lijstGebruikers); $messag = mysql_num_rows($resultLijstGebruikers); "<a href ="inbox.php" target ="showframe">Inbox <? if ($messag > 0) { echo [$"messag new"]; } ?></a>"mysql_close($link);?> <html><head> <title>Mafia :: v0.10 Beta</title> <link rel="stylesheet" type="text/css" href="src/standard.css" /><script type="text/javascript">var d = new Date()document.write(d.getDate())document.write(".")document.write(d.getMonth() + 1)document.write(".")document.write(d.getFullYear())</script><script type="text/javascript">var d = new Date()document.write(d.getHours())document.write(".")document.write(d.getMinutes())document.write(".")document.write(d.getSeconds())</script><br /></head></html> Link to comment Share on other sites More sharing options...
SFB Posted August 14, 2006 Share Posted August 14, 2006 you have to put the <?php ?> before the <html> and make sure there is no extra lines before the php either. <?php has to be the first 5 characters of the file.Humm i didnt know that. It has always worked fror me to do something like<html><head><?php $pgtitle = 'my test page';$content = 'this is me testing php'; ?><title><?php echo"$pgtitle";?></title></head><body><?phpecho"$content";?></body></html> Link to comment Share on other sites More sharing options...
reportingsjr Posted August 14, 2006 Share Posted August 14, 2006 I dont think you must put the mysql connection at the top of the page, at least doesnt matter for me.Anyways, your problem now is this line: "<a href ="inbox.php" target ="showframe">Inbox <? if ($messag > 0) { echo [$"messag new"]; } ?></a>" First, im guessing your wanting to print or echo this on the pageso you put this: echo "<a href ="inbox.php" target ="showframe">Inbox <? if ($messag > 0) { echo [$"messag new"]; } ?></a>" Second, im confused why you have this: echo [$"messag new"]; I see there you are trying to print the array out right there. but your doing it way wrong. Try this: echo $messag['new']; I am blanking out on mysql_num_rows right now though :)Try that and see what happens.The final code should be: echo "<a href ="inbox.php" target ="showframe">Inbox <? if ($messag > 0) echo $messag['new']; } ?></a>" Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 Replace line 9 with this: echo "<a href =\"inbox.php\" target =\"showframe\">Inbox";if ($messag > 0) { echo " [{$messag} new]"; }echo "</a>"; He's not trying to print an array, he's trying to print text like this:Inbox [3 new]$messag is a counter. Link to comment Share on other sites More sharing options...
reportingsjr Posted August 14, 2006 Share Posted August 14, 2006 oh, shoulda seen that . Sorry! Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 justsomeguy You nearly have my idea, I want it to say. "You Have New Mail" and make it able so u can click it and then it goes to inbox.php If you have 1 or more than 1 unread mail. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 if ($messag > 0) echo "<a href =\"inbox.php\" target =\"showframe\">";echo "Inbox";if ($messag > 0) { echo " [{$messag} new]"; echo "</a>";} The difference between that and what I posted above is here the inbox text will only be clickable if there are new messages. Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 if ($messag > 0) echo "<a href =\"inbox.php\" target =\"showframe\">";echo "Inbox";if ($messag > 0) { echo " [{$messag} new]"; echo "</a>";} The difference between that and what I posted above is here the inbox text will only be clickable if there are new messages. Thanks i think we are making progress.I will use this code untill i find out is it possible to only show this if new messages exsist Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 That's what it's doing. If you only want to show the inbox text when there are new messages (so if there are not new messages, they don't even see the inbox), then just wrap the whole thing in an if statement: if ($messag > 0) { echo "<a href =\"inbox.php\" target =\"showframe\">Inbox [{$messag} new]</a>";} Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 Okay thts how i wanted, Thank you for solving that problem, Now i seem to have a different one, Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/jnymris/public_html/Maffia (05-05-05)/Info.php on line 814.8.2006 22.26.55 <?php$link = mysql_connect('localhost', '***', '***');if (!$link) { die('Could not connect: ' . mysql_error());} $lijstGebruikers = "SELECT * FROM inbox WHERE reciever='$cookieusername' AND unread='0'"; $resultLijstGebruikers = mysql_query($lijstGebruikers); $messag = mysql_num_rows($resultLijstGebruikers);if ($messag > 0){ echo "<a href =\"inbox.php\" target =\"showframe\">Inbox [{$messag} new]</a>";}mysql_close($link);?> <html><head> <title>Johnnys Junk Clock Frame</title> <link rel="stylesheet" type="text/css" href="src/standard.css" /><script type="text/javascript">var d = new Date()document.write(d.getDate())document.write(".")document.write(d.getMonth() + 1)document.write(".")document.write(d.getFullYear())</script><script type="text/javascript">var d = new Date()document.write(d.getHours())document.write(".")document.write(d.getMinutes())document.write(".")document.write(d.getSeconds())</script><br /></head></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 The query has an error. Do this:$resultLijstGebruikers = mysql_query($lijstGebruikers) or echo mysql_error();I'm betting it's because you aren't selecting the database.BTW, what's with the foreign language in your code, you appear to be on top of some tweed somewhere in the UK. Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 MY database is called jnymris_mozza if that helps? i'm just about to try it now. I hope i'm not causing any trouble, like i say i'm totaly new ( started last night ) and i really do thank you very muchOn top of the tweed? i live up north in the uk yeah. I got the scripts from a mate, who i think he got from someone eles Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 You need to select the database after you connect with mysql_select_db.http://us2.php.net/manual/en/function.mysql-select-db.php On top of the tweed? i live up north in the uk yeah. I got the scripts from a mate, who i think he got from someone elesOh. Well it's nothing advanced, you might as well write it so it makes sense to you.$sql = "SELECT * FROM inbox WHERE reciever='$cookieusername' AND unread='0'";$result = mysql_query($sql);$num_messages = mysql_num_rows($result); Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 <?php$link = mysql_connect('localhost', 'jnymris_mozza', '***');if (!$link) { die('Could not connect: ' . mysql_error());}$sql = "SELECT * FROM inbox WHERE reciever='$cookieusername' AND unread='0'";$result = mysql_query($sql);$num_messages = mysql_num_rows($result);if ($messag > 0){ echo "<a href =\"inbox.php\" target =\"showframe\">Inbox [{$messag} new]</a>";}mysql_close($link);?> The mail is in a table called Inbox and my phpadmin is called : jnymris_mozza i know i'm totaly new but it looks fine to me and i can't find mysql_select_db did i miss it out? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 Dude, look at my last post. You need to select the database. I'm not trying to be insulting, but it doesn't matter if it looks fine to you if you don't know how to write it yet. The first thing you need to do is connect to the server, and the second (which you are not doing) is select the database. That's what I said earlier. $link = mysql_connect('localhost', 'jnymris_mozza', '***');if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db("whatever_your_database_is_called"); THEN you can query the database. Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 Thanks, I can't do it right now cause the server i am using has stopped working, sorry i cause so much trouble, I'm sure it will work now as if thts all wht i was missing. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2006 Share Posted August 14, 2006 It's no trouble, I just repeated myself a few times. Not a big deal, learning happens. Ask if there's anything else. Link to comment Share on other sites More sharing options...
morrisjohnny Posted August 14, 2006 Author Share Posted August 14, 2006 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/jnymris/public_html/Maffia (05-05-05)/Info.php on line 10I'm starting to get sick of php already, I hope this can be sorted soon, This is the last one for the night, I've had enough.<?php$link = mysql_connect('localhost', 'jnymris_mozza', '***');mysql_select_db(inbox);if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db("Inbox");$sql = "SELECT * FROM inbox WHERE reciever='$cookieusername' AND unread='0'";$result = mysql_query($sql);$num_messages = mysql_num_rows($result);if ($messag > 0){ echo "<a href =\"inbox.php\" target =\"showframe\">Inbox [{$messag} new]</a>";}mysql_close($link);?> Link to comment Share on other sites More sharing options...
reportingsjr Posted August 14, 2006 Share Posted August 14, 2006 Yes, mysql_num_rows has to be used certain ways.. just do this: $messag = "0";while($row = mysql_num_rows($result){$messag++;}Then the rest of the code will work! Replace that with $num_messages ) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now