Solved

error check - mysql - php

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I wonder how people fake their ip address? 3 38
Showing random records from database 10 37
send post values 7 26
Using coldfusion <cfexecute> to restore mysql database 7 30
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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