Solved

error check - mysql - php

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a loading gif while php runs server side 15 67
PHP Curl Problem 10 42
MySQL stored procedure returning null values 4 27
How do I speed up this PDO query 4 15
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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