470,849 Members | 1,133 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Testing mysql query

Basically I am using PHP 5.1.2 with Apache 2.0.5 on a FreeBSD 5.4 box
with Mysql 4.1.1 running.

I am attempting to write information to a Mysql table called Jabber. I
can connect successfully, query data, and write data pretty much
without problem. My problem is this.

I am trying to do some duplicate checking on this table so I query the
dbase with the following:

/* Searching for username in Authreg database and assiging it to
variable */
$query1 = "SELECT * FROM `$table` WHERE username='$username'";
$query1_results = mysql_query($query1) or die("Query Failed");
$existing = mysql_result($query1_results, 0,0);

Problem is this.

When a user already exsits in database I have a conditional if
statement that looks like such:

/* Conditional statement that searches for exsiting username */
if ($username == $existing) {
die ("Please select a unique username");
} else {
....
}

And it errorr out telling me to, "Please select a unique username" as
stated above.

When I am adding a new user to the database above I get the following
error in my logs or in the browser depending on what --display-errors=
is set to in my php.ini.:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row
0 on MySQL result index 4 in /usr/local/www/sfg/add_jabber/add_test.php
on line 38

I know this error is happening as a result of the following line when
the user is not present in the database:

$existing = mysql_result($query1_results, 0,0);

So how would I write something that is conditional to test if the name
exsits in the database before allowing me to write to the various
tables. Any help appreciated.

Feb 10 '06 #1
2 1919
ch****@gmail.com wrote:
Basically I am using PHP 5.1.2 with Apache 2.0.5 on a FreeBSD 5.4 box
with Mysql 4.1.1 running.

I am attempting to write information to a Mysql table called Jabber. I
can connect successfully, query data, and write data pretty much
without problem. My problem is this.

I am trying to do some duplicate checking on this table so I query the
dbase with the following:

/* Searching for username in Authreg database and assiging it to
variable */
$query1 = "SELECT * FROM `$table` WHERE username='$username'";
$query1_results = mysql_query($query1) or die("Query Failed");
$existing = mysql_result($query1_results, 0,0);

Problem is this.

When a user already exsits in database I have a conditional if
statement that looks like such:

/* Conditional statement that searches for exsiting username */
if ($username == $existing) {
die ("Please select a unique username");
} else {
...
}

And it errorr out telling me to, "Please select a unique username" as
stated above.

When I am adding a new user to the database above I get the following
error in my logs or in the browser depending on what --display-errors=
is set to in my php.ini.:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row
0 on MySQL result index 4 in /usr/local/www/sfg/add_jabber/add_test.php
on line 38

I know this error is happening as a result of the following line when
the user is not present in the database:

$existing = mysql_result($query1_results, 0,0);

So how would I write something that is conditional to test if the name
exsits in the database before allowing me to write to the various
tables. Any help appreciated.


Check to see how many rows are returned. If the username doesn't exist
in the database, MySQL won't return anything.

Also - always check to see if result is not false!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 11 '06 #2
> So how would I write something that is conditional to test if the name
exsits in the database before allowing me to write to the various
tables. Any help appreciated.


$query1 = "SELECT * FROM `$table` WHERE username='$username'";
$query1_results = mysql_query($query1) or die("Query Failed");
if (mysql_num_rows($query1_results) > 0) {
echo "User already exists.";
}

Feb 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by JoeAley2003 | last post: by
reply views Thread by Michael Weiner | last post: by
reply views Thread by Mike Chirico | last post: by
1 post views Thread by Ike | last post: by
3 posts views Thread by Me Alone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.