Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

error check - mysql - php

Posted on 2007-11-16
4
Medium Priority
?
173 Views
Last Modified: 2013-12-13
What's the best way to do an error check to see if the user ID exists?  if it doesn't exist, come back with "this user ID doesn't exist, try again".

ideas?  suggestions?

//GET ID # FROM USERS TABLE
$getIDsql = "select id from users where user_login = '$username'";
$getIDquery = mysql_query($getIDsql) or die ("Error in query: $getIDsql. ".mysql_error());
$getIDresult =  mysql_result($getIDquery, 0, 'id');
0
Comment
Question by:bschwarting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20298635
use the mysql_num_rows() function:
//GET ID # FROM USERS TABLE
$getIDsql = "select id from users where user_login = '$username'";
$getIDquery = mysql_query($getIDsql) or die ("Error in query: $getIDsql. ".mysql_error());
if (mysql_num_rows($getIDquery) == 0)
{
  echo "this user ID doesn't exist, try again.";
}
else
{
   $getIDresult =  mysql_result($getIDquery, 0, 'id');
    ...
 }

Open in new window

0
 
LVL 1

Author Comment

by:bschwarting
ID: 20298782
I also need to set my variable $getIDresult to 1 if there isn't a result, how can i add this?

if (mysql_num_rows($getIDquery) == 0)
{
  echo "this user ID doesn't exist, try again.";
  $getIDresult = '1';
}

This doesn't seem to work.
0
 
LVL 1

Author Comment

by:bschwarting
ID: 20298935
oops, had the wrong variable. and another piece of bad code.  that fixed it!
if (mysql_num_rows($getIDquerycompare) == 0)
{
  echo "this user ID doesn't exist, try again.";
  $getIDresultcompare = '1';
}

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20298938
>This doesn't seem to work.
in how far? the code looks fine...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question