Jump to content


few PHP/Mysql questions


5 replies to this topic

#1 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 29 September 2006 - 01:44 PM

Hello guys and gals, its time for me to ask stupid questions yet again.

Ok well I want to display how many times an admin has posted news, here is my code.
$select2 = mysql_query("SELECT * FROM news WHERE `author` = '$row1'") or die(mysql_error());
$row2 = mysql_num_rows($select2);
I just want that to count how many entrys of news that admin has posted.

Another one, I want to display if they are an admin or not. Here is my code.
$admin = "SELECT admin FROM users WHERE username = '$_SESSION[username]'";
$admin = mysql_query($admin);
$admin = mysql_fetch_array($admin);
It works fine in other cases, like when I want to hide a certain menu from regular users. However where im trying to use it now, it wont work. To display if they are an admin or not I use this.
<?php if($admin['admin'] == 1) {
  echo 'Yes';
}else{
  echo 'No';
} ?>

I am also having trouble with my "Update Profile" section. I posted this one a while back, but I recoded it a different way to update and now this one isnt working either. Here is the mysql_query parts, apparently my screwup is in here because I get a mysql error. Which is posted below it.
SOLVED!!!

If you can take a look at all of these, I would greatly appreciate it!

Edited by Korndawg, 29 September 2006 - 06:49 PM.


#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 29 September 2006 - 03:28 PM

Well, with just a quick run through your code, you have some wrong quotes on your first mysql_query().

mysql_query("INSERT INTO 'users' SET `pass` = '$_POST[passwd]' WHERE `username`='$_SESSION[username]'") or die(mysql_error());
Should be:
mysql_query("INSERT INTO `users` SET `pass` = '$_POST[passwd]' WHERE `username`='$_SESSION[username]'") or die(mysql_error());

The exact same thing is true for ALL of the queries in the block of code you supplied. the ` marks indicate an actual table/column, whereas the ' quotes indicate a value. :)

Just a personal preferance to save space, but I would use ternary operators to tack together commands into one $sql variable, then execute it at the end as one, instead of making multiple queries when not necessary. ;)

#3 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 29 September 2006 - 05:32 PM

Thanks for clearing that up Demonslay. I did what you said, however when I tried to change the password it still gave me a mysql error.

Quote

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 'WHERE `username`='admin'' at line 1
That is the error it gave me.

As for making it only have one query to update everything, the reason I made it so it would do each individual was because I didn't want it to insert a blank password if I was updating other things.

Anyone else have any ideas on this issue?

#4 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 29 September 2006 - 05:38 PM

umm...well im now going to bed but spot the crap line:

INSERT INTO `users` SET `pass` = '$_POST[passwd]' WHERE `username`='$_SESSION[username]'

You should be using UPDATE not INSERT.

#5 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 29 September 2006 - 05:47 PM

Ya I normally used UPDATE, however I was just testing that... Guess I forgot to change it back, thanks matt!

#6 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 29 September 2006 - 07:43 PM

Ok I solved all of the problems... The other problems I was having was because I was half assing everything and didn't realize it. Thanks Matt and Demonslay for your help. :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users