Solved

error check - mysql - php

Posted on 2007-11-16
4
162 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
  • 2
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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 142

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Button Click 11 25
php documentation 4 19
Apostophes in PHP generated form 6 15
Duplicating MySQL Table columns 5 10
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now