mysql_num_rows == 0

mysql_num_rows == 0

am 30.05.2011 22:31:09 von Nazish Zafar

--20cf300515bc225e2a04a4842c50
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

I've run into a little barrier, and I'm wondering whether you have any
insights. I'm entering values into a MySQL database. Before running the
mysql_query, I'm checking if the value already exists (using mysql_num_rows
== 0). If the value already exists in the database, the page will echo
"Username already exists" and it won't insert the user's new value. It runs
that far, and echoes the message accordingly. However, if the username is
new, and does not exist in the database, the query dies (without leaving a
mysql error).

I tried changing the Unique IDs in the database, but it doesn't seem to be
the issue. The syntax seems fine, as I used it for another similar page. Any
idea why it's dying?


$check = mysql_query("SELECT * FROM user WHERE
user_name='$user_name'") or die ("Unable to query database:".mysql_error());

$numrows = mysql_num_rows($check) or die ("Unable to search
database:".mysql_error()); -----> DIES HERE when a new value is entered. no
mysql_error msg.

if ($numrows == 0)
{

}
$alert = "Username already exists!"; ----> ECHOES correctly if
username already exists in the database (when mysql_num_rows != 0).

--20cf300515bc225e2a04a4842c50--

Re: mysql_num_rows == 0

am 30.05.2011 22:43:34 von Peter Lind

On 30 May 2011 22:31, Nazish wrote:
> Hi all,
>
> I've run into a little barrier, and I'm wondering whether you have any
> insights. I'm entering values into a MySQL database. Before running the
> mysql_query, I'm checking if the value already exists (using mysql_num_ro=
ws
> == 0).  If the value already exists in the database, the page wi=
ll echo
> "Username already exists" and it won't insert the user's new value. It ru=
ns
> that far, and echoes the message accordingly. However, if the username is
> new, and does not exist in the database, the query dies (without leaving =
a
> mysql error).
>
> I tried changing the Unique IDs in the database, but it doesn't seem to b=
e
> the issue. The syntax seems fine, as I used it for another similar page. =
Any
> idea why it's dying?
>
>
>        $check =3D mysql_query("SELECT * FROM user WHE=
RE
> user_name=3D'$user_name'") or die ("Unable to query database:".mysql_erro=
r());
>
>        $numrows =3D mysql_num_rows($check) or die ("U=
nable to search
> database:".mysql_error());  -----> DIES HERE when a new value is ent=
ered. no
> mysql_error msg.

bla bla or die("more bla");

is a very bad way of handling error checks. In your particular case,
your foot has been shot off because mysql_num_rows will return 0 (no
rows match the query) and thus the die() is executed - but of course
there's no error in the query, it executed just fine, this we know
already.

Moral: don't use ' or die();' for anything else than hands on debugging pur=
poses

Regards
Peter

--=20

WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: mysql_num_rows == 0

am 31.05.2011 00:04:36 von Karl DeSaulniers

try this instead..

if(mysql_num_rows($check) > 0) {
//true
} else {
//false
}

and yes, Peter's right... please dont make everything die().

Karl

On May 30, 2011, at 3:43 PM, Peter Lind wrote:

> On 30 May 2011 22:31, Nazish wrote:
>> Hi all,
>>
>> I've run into a little barrier, and I'm wondering whether you have
>> any
>> insights. I'm entering values into a MySQL database. Before
>> running the
>> mysql_query, I'm checking if the value already exists (using
>> mysql_num_rows
>> == 0). If the value already exists in the database, the page will
>> echo
>> "Username already exists" and it won't insert the user's new
>> value. It runs
>> that far, and echoes the message accordingly. However, if the
>> username is
>> new, and does not exist in the database, the query dies (without
>> leaving a
>> mysql error).
>>
>> I tried changing the Unique IDs in the database, but it doesn't
>> seem to be
>> the issue. The syntax seems fine, as I used it for another similar
>> page. Any
>> idea why it's dying?
>>
>>
>> $check = mysql_query("SELECT * FROM user WHERE
>> user_name='$user_name'") or die ("Unable to query
>> database:".mysql_error());
>>
>> $numrows = mysql_num_rows($check) or die ("Unable to search
>> database:".mysql_error()); -----> DIES HERE when a new value is
>> entered. no
>> mysql_error msg.
>
> bla bla or die("more bla");
>
> is a very bad way of handling error checks. In your particular case,
> your foot has been shot off because mysql_num_rows will return 0 (no
> rows match the query) and thus the die() is executed - but of course
> there's no error in the query, it executed just fine, this we know
> already.
>
> Moral: don't use ' or die();' for anything else than hands on
> debugging purposes
>
> Regards
> Peter
>
> --
>
> WWW: plphp.dk / plind.dk
> LinkedIn: plind
> BeWelcome/Couchsurfing: Fake51
> Twitter: kafe15
>

>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Karl DeSaulniers
Design Drumm
http://designdrumm.com


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: mysql_num_rows == 0

am 31.05.2011 01:13:54 von Nazish Zafar

--00151774190820015d04a486723c
Content-Type: text/plain; charset=ISO-8859-1

That did the trick: I was over-enthusiastic in my usage of die(mysql_error).

I initially used mysql_error to troubleshoot another problem (which has now
re-emerged), but that's a different question which is puzzling me. The error
message ($alert = "Username already exists!";) displays on the page as soon
as the value has been inserted into mysql. It's strange, since the message
should be bypassed through the else statement, and the script only runs if
the user clicks 'submit'. Is this familiar?


> $check = mysql_query("SELECT * FROM user WHERE
> user_name='$user_name'") or die ("Unable to query
database:".mysql_error());
>
> $numrows = mysql_num_rows($check) or die ("Unable to search
> database:".mysql_error()); -----> DIES HERE when a new value is entered.
no
> mysql_error msg.

bla bla or die("more bla");

is a very bad way of handling error checks. In your particular case,
your foot has been shot off because mysql_num_rows will return 0 (no
rows match the query) and thus the die() is executed - but of course
there's no error in the query, it executed just fine, this we know
already.

Moral: don't use ' or die();' for anything else than hands on debugging
purposes

Regards
Peter

2011/5/30 Peter Lind

> On 30 May 2011 22:31, Nazish wrote:
> > Hi all,
> >
> > I've run into a little barrier, and I'm wondering whether you have any
> > insights. I'm entering values into a MySQL database. Before running the
> > mysql_query, I'm checking if the value already exists (using
> mysql_num_rows
> > == 0). If the value already exists in the database, the page will echo
> > "Username already exists" and it won't insert the user's new value. It
> runs
> > that far, and echoes the message accordingly. However, if the username is
> > new, and does not exist in the database, the query dies (without leaving
> a
> > mysql error).
> >
> > I tried changing the Unique IDs in the database, but it doesn't seem to
> be
> > the issue. The syntax seems fine, as I used it for another similar page.
> Any
> > idea why it's dying?
> >
> >
> > $check = mysql_query("SELECT * FROM user WHERE
> > user_name='$user_name'") or die ("Unable to query
> database:".mysql_error());
> >
> > $numrows = mysql_num_rows($check) or die ("Unable to search
> > database:".mysql_error()); -----> DIES HERE when a new value is entered.
> no
> > mysql_error msg.
>
> bla bla or die("more bla");
>
> is a very bad way of handling error checks. In your particular case,
> your foot has been shot off because mysql_num_rows will return 0 (no
> rows match the query) and thus the die() is executed - but of course
> there's no error in the query, it executed just fine, this we know
> already.
>
> Moral: don't use ' or die();' for anything else than hands on debugging
> purposes
>
> Regards
> Peter
>
> --
>
> WWW: plphp.dk / plind.dk
> LinkedIn: plind
> BeWelcome/Couchsurfing: Fake51
> Twitter: kafe15
>

>

--00151774190820015d04a486723c--

Re: mysql_num_rows == 0

am 31.05.2011 02:18:00 von Nazish Zafar

--20cf300515bc5de74e04a4875755
Content-Type: text/plain; charset=ISO-8859-1

Code runs smoothly now, thanks. It's always great to pick up diverse coding
tips through the conversations here.

2011/5/30 Nazish

> That did the trick: I was over-enthusiastic in my usage of
> die(mysql_error).
>
> I initially used mysql_error to troubleshoot another problem (which has now
> re-emerged), but that's a different question which is puzzling me. The error
> message ($alert = "Username already exists!";) displays on the page as
> soon as the value has been inserted into mysql. It's strange, since the
> message should be bypassed through the else statement, and the script only
> runs if the user clicks 'submit'. Is this familiar?
>
>
> > $check = mysql_query("SELECT * FROM user WHERE
> > user_name='$user_name'") or die ("Unable to query
> database:".mysql_error());
> >
> > $numrows = mysql_num_rows($check) or die ("Unable to search
> > database:".mysql_error()); -----> DIES HERE when a new value is entered.
> no
> > mysql_error msg.
>
> bla bla or die("more bla");
>
> is a very bad way of handling error checks. In your particular case,
> your foot has been shot off because mysql_num_rows will return 0 (no
> rows match the query) and thus the die() is executed - but of course
> there's no error in the query, it executed just fine, this we know
> already.
>
> Moral: don't use ' or die();' for anything else than hands on debugging
> purposes
>
> Regards
> Peter
>
> 2011/5/30 Peter Lind
>
>> On 30 May 2011 22:31, Nazish wrote:
>> > Hi all,
>> >
>> > I've run into a little barrier, and I'm wondering whether you have any
>> > insights. I'm entering values into a MySQL database. Before running the
>> > mysql_query, I'm checking if the value already exists (using
>> mysql_num_rows
>> > == 0). If the value already exists in the database, the page will echo
>> > "Username already exists" and it won't insert the user's new value. It
>> runs
>> > that far, and echoes the message accordingly. However, if the username
>> is
>> > new, and does not exist in the database, the query dies (without leaving
>> a
>> > mysql error).
>> >
>> > I tried changing the Unique IDs in the database, but it doesn't seem to
>> be
>> > the issue. The syntax seems fine, as I used it for another similar page.
>> Any
>> > idea why it's dying?
>> >
>> >
>> > $check = mysql_query("SELECT * FROM user WHERE
>> > user_name='$user_name'") or die ("Unable to query
>> database:".mysql_error());
>> >
>> > $numrows = mysql_num_rows($check) or die ("Unable to search
>> > database:".mysql_error()); -----> DIES HERE when a new value is
>> entered. no
>> > mysql_error msg.
>>
>> bla bla or die("more bla");
>>
>> is a very bad way of handling error checks. In your particular case,
>> your foot has been shot off because mysql_num_rows will return 0 (no
>> rows match the query) and thus the die() is executed - but of course
>> there's no error in the query, it executed just fine, this we know
>> already.
>>
>> Moral: don't use ' or die();' for anything else than hands on debugging
>> purposes
>>
>> Regards
>> Peter
>>
>> --
>>
>> WWW: plphp.dk / plind.dk
>> LinkedIn: plind
>> BeWelcome/Couchsurfing: Fake51
>> Twitter: kafe15
>>

>>
>
>

--20cf300515bc5de74e04a4875755--