Jump to content

mattchamp97

Members
  • Posts

    15
  • Joined

  • Last visited

Everything posted by mattchamp97

  1. I'm not sure how to error check like this as my PHP scripts are sending responses to my app via JSON objects. I didn't get an error code in my error checking, but by process of elimination I was able to identify that the line of code identified earlier is what is not working for me. if(strtoupper($row1['accountType']) == 'STUDENT') I tried this line for my if statement but unfortunately it does not work.
  2. I want to compare the result (Which will be either Student OR Lecturer) to the String "Student". Through error checking I have discovered that the line that will not run is the following. if($row1['accountType'] == 'Student') { Where am I going wrong here? I have also tried the following with no luck. $sql1 = "select accountType from user_info where idNum = '$user'"; $result1 = mysqli_query($con,$sql1); $row1 = mysqli_fetch_row($result1); if($row1[0] == 'Student') {
  3. $user = $_GET["userID"]; $sql1 = "select accountType from user_info where idNum = '$user'"; $result1 = mysqli_query($con,$sql1); $row1 = mysqli_fetch_assoc($result1); #$usertype = $row1['accountType']; if($row1['accountType'] == 'Student') { #run code here The line that is failing me is my if statement. I don't understand how to store the result of my sql query and compare it to a string. And yes I do understand what you meant regarding good practice with my coding, I'll be implementing the join as suggested inside of my loop once I figure out how to identify the account type.
  4. I understand what people mean but I just don't understand PHP very well and can't progress with my work without this bit of code. My problem is that I need to fetch the account type, which can only be Student or Lecturer. I then want to run different code to return modules relevant to the account type based on this. I can't find anywhere online how to compare the result of a SELECT to a string, this is ultimately where I'm getting stuck.
  5. Hi guys, My knowledge of PHP is very basic. The only piece of functionality I need is to see if the result of following SELECT equals a string "Student", and to then run the code inside my loop. $user = $_GET["userID"]; $sql1 = "select accountType from user_info where idNum = '$user'"; $result1 = mysqli_query($con,$sql1); $row1 = mysqli_fetch_assoc($result1); #$usertype = $row1['accountType']; if($row1['accountType'] == 'Student') { #run code here I don't understand joins to a huge degree I just want to simply compare the string to the result of my SQL statement and run the code in my if.
  6. Hi Phill, idNum is a string as in my case, ID numbers may contain characters as well as numbers. This is also not production code, just a personal project I'm working on.
  7. What I'm trying to achieve is find the account type of the user who's ID is passed to the PHP script and stored as $user. $user is a string variable as in my instance it can contain letters as well as numbers. So I'm looking to find the record where the idNum equals that passed to the script, and then see what it has stored under accountType, which will either be "Student" or "Lecturer".
  8. Hi All, I'm working on PHP scripts to interact with a web hosted MySQL DB for an Android Application. Simply what I am trying to do is in the PHP script is run a SELECT statement which will return the value of a column, UserType, and compare the result of this to a string, which will then execute code depending on it's value. This user type can only be either 'student' or 'lecturer'. Any help with this would be much appreciated. <?php require "init.php"; $user = $_GET["userID"]; #Selects column account type where the idNum equals $user which is passed from my app. $sql1 = "select accountType from user_info where idNum = '$user'"; $result1 = mysqli_query($con,$sql1); $row1 = mysqli_fetch_assoc($result1); #This is where I am stuck. Simply, I am trying to run the code in the loop where the result of $sql1 equals 'Student'. The else will run if it is not #student and therefore is 'Lecturer'. I'm also not sure if my code inside the IF is fully correct either as it's not running that far. if($row1['accountType'] == 'Student') { $sql2 = "select courseCode from user_info where idNum = '$user'"; $result2 = mysqli_query($con,$sql2); $row2 = mysqli_fetch_assoc($result2); $sql3 = "select * from module_details where classListCourseCode = '".$row2['courseCode']."'"; $result3 = mysqli_query($con,$sql3); $response = array(); while($row = mysqli_fetch_array($result3)) { array_push($response,array("moduleID"=>$row[0],"lecturerID"=>$row[1],"moduleName"=>$row[2],"classListCourseCode"=>$row[3])); } echo json_encode(array("server_response"=>$response)); } Thanks in advance.
  9. I forgot to reply to you before but you got me up and running in the end, thanks very much 👍
  10. Hi, thanks very much for your help. I've just tried to run the following code. if($insertresult) { $response = array(); $code = "module_created"; $message = "Your new module was created successfully."; $q = "select moduleID from module_details WHERE moduleName = '$moduleName' AND lecturerID = '$lecturerID'"; if (!$sqlmodID = mysqli_query($con, $q)) { echo "Module Id could not be researched"; exit(); } $row = mysqli_fetch_assoc($sqlmodID); $modid = $row['moduleID']; $sqlstudent = "select idNum from user_info WHERE courseCode = '$courseCode'"; $result = mysqli_query($con, $sqlstudent); while($row = mysqli_fetch_assoc($result)) { mysqli_query($con,"insert into student_classlist VALUES ('$modid', '" . $row['idNum'] . "'"); } //Push variables $code and $message into an array array_push($response,array("code"=>$code,"message"=>$message)); //Put array into a JSON object to push to the app echo json_encode(array("server_response"=>$response)); } As you questioned in your previous post, I am seeking all student IDs, not just one. I tried to implement a loop as you suggested with this while loop where I am processing one row at a time but it does not seem to be giving me my desired results. Am I processing the rows incorrectly in my while loop or where have I gone wrong?
  11. <?php require "init.php"; $lecturerID = $_POST["lecturerID"]; $moduleName = $_POST["moduleName"]; $courseCode = $_POST["courseCode"]; $moduleID = NULL; $sql = "select * from user_info where idNum = '$lecturerID' AND accountType = 'Lecturer'"; $sqlresp = mysqli_query($con,$sql); $rowcount = mysqli_num_rows($sqlresp); if($rowcount<1) { $response = array(); $code = "module_creation_error"; $message = "Please enter a valid Lecturer ID."; //Push variables $code and $message into an array array_push($response,array("code"=>$code,"message"=>$message)); //Put array into a JSON object to push to the app echo json_encode(array("server_response"=>$response)); } else { //Inserting new module details into module table $sqlinsert = "insert into module_details(lecturerID,moduleName,classListCourseCode) VALUES ('$lecturerID', '$moduleName', '$courseCode')"; $insertresult = mysqli_query($con,$sqlinsert); if($insertresult) { $response = array(); $code = "module_created"; $message = "Your new module was created successfully."; $sqlmodID = mysqli_query($con,"select moduleID from module_details WHERE moduleName = '$moduleName' AND lecturerID = '$lecturerID'"); $sqlstudent = "select idNum from user_info WHERE courseCode = '$courseCode'"; $result = mysqli_query($con, $sqlstudent); $row = mysqli_fetch_array($result, MYSQLI_NUM); $studentArray = array_map('strval', $row); for($i=0; $i < count($studentArray); $i++) { mysqli_query($con,"insert into student_classlist VALUES ('$sqlmodID', '$studentArray[$i]')"); } //Push variables $code and $message into an array array_push($response,array("code"=>$code,"message"=>$message)); //Put array into a JSON object to push to the app echo json_encode(array("server_response"=>$response)); } else { $response = array(); $code = "module_insert_error"; $message = "Server Error. Try again"; //Push variables $code and $message into an array array_push($response,array("code"=>$code,"message"=>$message)); //Put array into a JSON object to push to the app echo json_encode(array("server_response"=>$response)); } } mysqli_close($con); ?> That's my whole file there. Basically once a module is created, I want to populate a different table, student_classlist based on the values passed to this script. I've read through the manual but came here as I found some parts of it confusing. I have used mysqli_fetch_array on the resource returned by the query to get my array if this is what you mean. Or are you saying I need to do a fetch on $result and $sqlmodID ?
  12. Which query do you mean? The one to set the value of $sqlModID? This is only storing a single integer value. If you meant the result of mysqli_query($con, $sqlstudent); , I'm using the line after this to store it in an array named row, which I then converted the contents of to string values. I don't understand how to use fetch on this as I'm only new to PHP, would you be able to show me some sort of example? Thanks very much for your help with this.
  13. This is the line throwing the error mysqli_query($con,"insert into student_classlist VALUES ('$sqlmodID', '$studentArray')");
  14. Hi, Yes there's a line number identifying that the error I'm getting is with the condition inside the loop. The whole error message is as follows: "Recoverable fatal error: Object of class mysqli_result could not be converted to string in /storage/ssd5/641/12544641/public_html/newmodule.php on line 54"
  15. The following is an extract of PHP code I have interacting with a MySQL database and I keep getting the "object of class mysqli_result could not be converted to string" error on the line of code inside my loop. This is my code. #This is just storing a single integer, a moduleID $sqlmodID = mysqli_query($con,"select moduleID from module_details WHERE moduleName = '$moduleName' AND lecturerID = '$lecturerID'"); #This is a query to get relevant idNumbers $sqlstudent = "select idNum from user_info WHERE courseCode = '$courseCode'"; #Running the previous statement $result = mysqli_query($con, $sqlstudent); #Storing the statements results in an array $row = mysqli_fetch_array($result); #I'm not sure if this line is necessary but it converts the array values into a string (I'm storing the ID's as strings as they contain numbers and letters) $studentArray = array_map('strval', $row); #The error flashes on my condition of this loop. I'm trying to take each element of my array, and enter it to my table, assigning each variable the same ModuleID. for($i=0; $i < count($studentArray); $i++){ mysqli_query($con,"insert into student_classlist VALUES ('$sqlmodID', '$studentArray')"); } Any help would be much appreciated. I hope I've explained my code sufficiently.
×
×
  • 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.