MYSQL QUestion

Hello and thank you for this great site!!!

I have a list of entries attached to an id ($sid) number so the id number is sometimes used more than once. As long as the id number appears only once, it returns the result. When the id number is repeated in the database, it gives the error message “Sorry, the ID you submitted is not present in our database. Please hit your browser’s back button and reenter your student’s ID.” So it will only return results with one line. Anything more returns the error statement.

I’m thinking it’s in the last “if” statement somewhere but I’m too new to PHP to know why.

sql=" SELECT * FROM `volunteer` WHERE sid='$sid'"
	or die ("Sorry $id, I Stopped at the select query");
 
//$results variable = the results of the query
$results = mysql_query($sql)
	or die ("Stopped at the mysql_query");

$num=mysql_numrows($results);

$sql1=mysql_query("SELECT sid FROM volunteer WHERE sid='$sid'");

if (mysql_num_rows($sql1)==0 || mysql_num_rows($sql1)>1)
{ die ("Sorry, the ID you submitted is not present in our database.  Please hit your browser's back button and reenter your student's ID.");
}

else;
(more code)

What am I doing wroing?

Thank you very much for your help

Ok, I see a few things in your code that doesn’t look right to me… I’ve added comments…

    sql=" SELECT * FROM `volunteer` WHERE sid='$sid'" //PROBLEM #1
       or die ("Sorry $id, I Stopped at the select query");

    //$results variable = the results of the query
    $results = mysql_query($sql)
       or die ("Stopped at the mysql_query");

    $num=mysql_numrows($results); //PROBLEM #2

    $sql1=mysql_query("SELECT sid FROM volunteer WHERE sid='$sid'"); //PROBLEM #3

    if (mysql_num_rows($sql1)==0 || mysql_num_rows($sql1)>1) //PROBLEM #4
    { die ("Sorry, the ID you submitted is not present in our database.  Please hit your browser's back button and reenter your student's ID.");
    }

    else;
    (more code)

Problem #1 = there should be a $ at the very beginning of the line - I’m assuming this got lost in the cut/paste
Problem #2 = mysql_numrows should be mysql_num_rows
Problem #3 = What is the purpose of this second query? You already have this information from the first query.
Problem #4 = This is the main issue you are talking about here. You have “if the number of rows is equal to 0 OR if the number of rows is greater than 1” So the only time that is true is if there is EXACTLY one result returned from your query.

Basically you want this…

$query = "SELECT * FROM `volunteer` WHERE sid='$sid'";
$result = mysql_query($query) or die("Sorry $id, I Stopped at the select query");

$num_of_rows = mysql_num_rows($result);

if ($num == 0) { //sid was not in DB
  die("Sorry, the ID you submitted is not present in our database.  Please hit your browser's back button and reenter your student's ID.");
} else if ($num == 1) { //sid was only in DB once
  //INSERT code here 
} else {  //sid was in DB multiple times
  //Code Continues
}

Hopefully that helps. You may be trying to do more than necessary, but you may be trying to do something I’m not sure of…

Thank you very much for your help. It works great now. Much appreciated!

No problem! My pleasure!

Sponsor our Newsletter | Privacy Policy | Terms of Service