You are here: Re: [PHP] IBM's Learning PHP Part 1 tutorial. « PHP « IT news, forums, messages
Re: [PHP] IBM's Learning PHP Part 1 tutorial.

Posted by Andrι Medeiros on 07/06/05 17:25

On Wed, 2005-07-06 at 14:12 +0100, Richard Davey wrote:
> Hello Bill,
>
> Wednesday, July 6, 2005, 3:36:09 AM, you wrote:
>
> BM> I'm working my way through IBM's PHP tutorial. Generally good ...
> BM> but I'm stuck at an error point and have no idea what's going
> BM> wrong. Before adding a new row to the mysql database (already
> BM> opened) we do a query to see if a particular record already
> BM> exists. (see $resultT).
>
> BM> We then test, using if, to see if $resultT is true or false. If
> BM> it's false we are then supposed to enter a new record. Problem:
> BM> it's never false. It always evaluates true. What am I doing wrong?
>
> BM> /* build query to see if the record is entered already */
> BM> $sqlT = "select * from users where
> BM> username='".$_POST["name"]."'";
> BM> $resultT = mysql_query($sqlT);
>
> BM> /* Now test -- did we find anything ... if not add this user */
> BM> if (! $resultT) {
> BM> /* here we add the new record if it doesn't already exit /*
>
> To be honest that is quite shocking code, especially from a "teaching
> beginners" perspective - and even more so coming from the likes of
> IBM. But, SQL injection issues aside, the problem is most likely that
> there is nothing wrong with your SQL query. mysql_query will return a
> false (for a SELECT query) only if there is an error, not if "no
> records exist" - that isn't an error.
>
> It would make more sense to actually do a: "SELECT COUNT(*) AS hits FROM
> users WHERE username = 'x'" and then check the value of the returned
> "hits" (which will always return something, even if zero).
> Alternatively instead of doing if (!$result) you could do: if
> (mysql_num_rows($result) > 0) ... that way you know that the user
> already exists.
>
> Best regards,
>
> Richard Davey
> --
> http://www.launchcode.co.uk - PHP Development Services
> "I do not fear computers. I fear the lack of them." - Isaac Asimov
>

Depending on the sittuation, IMHO, COUNT(*) wouldn't be the way to go.
If you need the user's id or somesuch, you have to run an additional
query to get the info.

mysql_num_rows($result) will do just fine.

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация