Jump to content

Recommended Posts

this is mysql part of my php code

<?
$query=("select * from username WHERE username='$username'");
if(@mysql_query($query) > 0) 
{ 
     die("Username already in use."); 
}
else {
//Add data
//NOW()
$sql=( "INSERT INTO u_data (id,username,status) VALUES ('NULL','$username','$as')");
if (@mysql_query($sql)) {
    echo('<p>Your information has been Succesfuly added</p>');
  } else {
    echo('<p>There was error adding information</p>');
}
mysql_close();
}
?>

What happens when you run it? My guess is nothing because you have errors turned off

 

In your first if statement, your checking if a resource_ID is greater than 0 and you need to be checking the num_rows returned from the query.

replace this:

if(@mysql_query($query) > 0)
{ 

 

With this:

$res=@mysql_query($query);
if (mysql_num_rows($res) > 0 ) 
{

 

I think that's what you're looking for, if I read your post right.

if username exist continue else exist die

So what happens?  Do you see any errors?  Provide more information.

 

- You shouldn't be using the mysql_query return value (resource or FALSE) to check how many rows are returned.  Use mysql_num_rows.

- You shouldn't be using the '@' suppressor symbol.  Fix your errors & warnings.

- If the username is already in use, don't kill the script by using die.  Handle it properly, maybe give them another chance to pick a name.

What happens when you run it? My guess is nothing because you have errors turned off

 

In your first if statement, your checking if a resource_ID is greater than 0 and you need to be checking the num_rows returned from the query.

replace this:

if(@mysql_query($query) > 0)
{ 

 

With this:

$res=@mysql_query($query);
if (mysql_num_rows($res) > 0 ) 
{

 

I think that's what you're looking for, if I read your post right.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a9953296/public_html/add_user.php on line 38

 

 

no errors, it add's data (in database user exists) so it adds new id agen but it should show error and kill the code

hmm ok thanks i got my error mistake

$query=("select * from u_data WHERE username='$username'");
//$res=mysql_query($query) or die(mysql_error());
if(mysql_query($query) > 0) 
{ 
     die("Username already in use."); 
}
else {

but now it shows only "Username already in use." hmm my brain is totaly crached  :-\

hmm ok thanks i got my error mistake

$query=("select * from u_data WHERE username='$username'");
//$res=mysql_query($query) or die(mysql_error());
if(mysql_query($query) > 0) 
{ 
     die("Username already in use."); 
}
else {

but now it shows only "Username already in use." hmm my brain is totaly crached  :-\

 

I really try to refrain from disrespecting people (well, usually) but you really need to pull your head out. That is the same invalid code you posted originally. You have been told several times in this post what the problem is and how to fix it, but you go back to what you originally had.

 

//Create a query string
$query = "SELECT * FROM u_data WHERE username='$username'";
//Run the query and put RESULT REFERENCE ID into variable
$result = mysql_query($query) or die(mysql_error());
//Check if there are any rows in the RESULT REFERENCE ID
if(mysql_num_rows($result)>0)
{
    //User exists
    echo "Username already in use.";
   //Add proper error handling
}
else
{
    //User name does not exist - continue
}

Comments in the code:

$query="select * from u_data WHERE username='$username'";
echo "QUERY-> " . $query;  //Echo out the string query value
$res=mysql_query($query) or die(mysql_error()); //die and show error, if query is invalid
if(mysql_num_rows($res) > 0) //retrieve number of results returned from query
{ 
     die("Username already in use.");  //you need to handle this properly & not use die()
}

 

EDIT: mjd beat me to it.

thanks for all who was helping me im realy sorry for any problems i have made

 

//Create a query string
$query = "SELECT * FROM u_data WHERE username='$username'";
//Run the query and put RESULT REFERENCE ID into variable
$result = mysql_query($query) or die(mysql_error());
//Check if there are any rows in the RESULT REFERENCE ID
if(mysql_num_rows($result)>0)
{
    //User exists
    echo "Username already in use.";
   //Add proper error handling
}
else{
//Add data

it's working now

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.