Solved

error check - mysql - php

Posted on 2007-11-16
4
167 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 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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

696 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