spearchilduser Posted March 26, 2012 Share Posted March 26, 2012 $sql = "SELECT * FROM studproj WHERE Enrolement_Number = students.'$username'"; $results = mysql_query($sql); //echo $results; //echo $sql; if(mysql_num_rows($result) == 0) { echo "You already have a project chosen you cannot choose another but you can see your selection on the main page!"; exit; } else { echo " Please continue and choose your Project"; } ?> this is the code however i dont know what to put where ==0 is so that it says if there is a row that matches then choose an error message any help ? Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/ Share on other sites More sharing options...
spearchilduser Posted March 26, 2012 Author Share Posted March 26, 2012 the result btw Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331373 Share on other sites More sharing options...
samshel Posted March 26, 2012 Share Posted March 26, 2012 not sure if i understand...but try this. if(mysql_num_rows($result) > 0) Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331379 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 in the mysql_num_rows call, the variable should be $results, not $result. what exactly are you asking? Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331380 Share on other sites More sharing options...
spearchilduser Posted March 26, 2012 Author Share Posted March 26, 2012 ive changed it a little bit $sql = "SELECT * FROM studproj WHERE Enrolement_Number ='$username'"; $result = mysql_query($sql); //echo $result; //echo $sql; if(!$result) { // problem with query echo "query is wrong"; } elseif (mysql_num_rows($result)) { echo " Please continue and choose your Project"; } else { echo "You already have a project chosen you cannot choose another but you can see your selection on the main page!"; } ?> but it always still just returns as if the query is true Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331381 Share on other sites More sharing options...
kicken Posted March 26, 2012 Share Posted March 26, 2012 So instead of checking for zero rows, you want to check for one or more rows? How about using >= 1 then. if(mysql_num_rows($results) >= 1) Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331382 Share on other sites More sharing options...
spearchilduser Posted March 26, 2012 Author Share Posted March 26, 2012 ive done these however it always shows the result as if the user has a entry in the databse when ive emptied the database so it should be telling them to choose a project ?php $username = $_SESSION['valid_user']; ECHO $username; $sql = "SELECT * FROM studproj WHERE Enrolement_Number ='$username'"; $result = mysql_query($sql); //echo $result; //echo $sql; if(!$result) { // problem with query echo "query is wrong"; } elseif (mysql_num_rows($result)>= 1) { echo " Please continue and choose your Project"; } else { echo "You already have a project chosen you cannot choose another but you can see your selection on the main page!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331383 Share on other sites More sharing options...
samshel Posted March 26, 2012 Share Posted March 26, 2012 check if your query is failing: $result = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331385 Share on other sites More sharing options...
scootstah Posted March 26, 2012 Share Posted March 26, 2012 What does echo mysql_num_rows($result); give you? Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331386 Share on other sites More sharing options...
spearchilduser Posted March 26, 2012 Author Share Posted March 26, 2012 THE ECHO dispalys the username and 0 Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331387 Share on other sites More sharing options...
kicken Posted March 26, 2012 Share Posted March 26, 2012 When you change your condition, you also changed your echo's so they no longer match up with what you checking. You need to make sure what you put inside your if statement's code body matches what you want to do under that condition. if (mysql_num_rows($results) >= 1){ echo 'You already have a project'; } else { echo 'You must choose a project.'; } Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331388 Share on other sites More sharing options...
spearchilduser Posted March 26, 2012 Author Share Posted March 26, 2012 even with the change to make sure the changes have been made it still always seems to choose the else {} option Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331390 Share on other sites More sharing options...
AyKay47 Posted March 27, 2012 Share Posted March 27, 2012 then there aren't any rows being grabbed from your query. Have you debugged your query? Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331392 Share on other sites More sharing options...
spearchilduser Posted March 27, 2012 Author Share Posted March 27, 2012 yes ive debugged it: echo mysql_num_rows($result); = dan0 echo $sql = shows the statment and dan where the $username is echo $result = resource4# Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331393 Share on other sites More sharing options...
AyKay47 Posted March 27, 2012 Share Posted March 27, 2012 echo mysql_num_rows($result); = 0 this means that there are 0 results being returned. Something is not right in the SQL if you are expecting results. Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331394 Share on other sites More sharing options...
spearchilduser Posted March 27, 2012 Author Share Posted March 27, 2012 Aww ive found the problem im asking it to match the enrolement number with the users username when it needs to look into the s6tudents table and match the username with the enrolement number that has that username would that be a join statement ? ive never had to do this before so im really not sure Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331397 Share on other sites More sharing options...
AyKay47 Posted March 27, 2012 Share Posted March 27, 2012 would that be a join statement ? yes, refer to the join documentation for correct syntax Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331398 Share on other sites More sharing options...
spearchilduser Posted March 27, 2012 Author Share Posted March 27, 2012 im getting rrally confused by the joining document woudl i be able to take a shortcut and do something like : $sql1 = "Select Enrolement_Number FROM students WHERE First_Name ='$username'"; $result1 = mysql_query($sql1) or die(mysql_error()); echo $result1; $sql = ("SELECT * FROM studproj WHERE Enrolement_Number = '$result1'") or die (mysql_error()); $result = mysql_query($sql) or die(mysql_error()); echo mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331401 Share on other sites More sharing options...
spearchilduser Posted March 27, 2012 Author Share Posted March 27, 2012 ive had one try of it so what im tryign to say is select all from the studprojc table if the enrolement number matches a field with the username init from the students table $sql1 = ("Select * FROM studproj INNER JOIN students ON students.First_Name = Students.'$username'"); Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331405 Share on other sites More sharing options...
spearchilduser Posted March 27, 2012 Author Share Posted March 27, 2012 i finally got the join statment to work wether its the best way to do it im not sure but it works not im havign difficulties taking the filed names and storing them into attributes to disply information from them: $sql = ("Select project.Module_Code,project.Title,project.First_Supervisor,project.Level,project.Description,studproj.Module_Code FROM studproj INNER JOIN project ON studproj.Module_Code=project.Module_Code "); $result = mysql_query($sql) or die(mysql_error()); f (mysql_num_rows($result1)==1) { echo 'You already have a project'; while ($row = mysql_fetch_row($result)) { $enrolementnumber = $row['Enrolement_Number']; $Modulecode = $row['Module_Code']; $Title = $row['Title']; $First_Supervisor = $row['First_Supervisor']; $Level = $row['Level']; $Description = $row['Description']; echo '<table border = "2"> <tr> <td>Module Code </td><td><input readonly type= "text" name="Module_Code1"></td> <td>Title </td><td><input readonly type= "text" name="Title1" ></td> <td>First Supervisor </td><td><input readonly type= "text" name="First_Supervisor1" ></td> <td>Level </td><td><input readonly type= "text" name="Level1" ></td></tr> </table> <table> <tr> <td>Description </td><td><input readonly type= "text" size = 200 name="Description1"></td> </tr> </table>'; } } else { echo 'You must choose a project.'; } ?> it wont display the table and i cannot see why it is late so that may be the reason but anyhelp is appricated Quote Link to comment https://forums.phpfreaks.com/topic/259770-difficluty-with-a-if-statement/#findComment-1331419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.