• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PHP Function/Query code

I'm not much of a php coder, but I'm trying to learn by building a basic webapp.

In the code below, I'm getting an error of

Call to a member function query() on a non-object in /getApplications.php on line 24

line 24 // if ($conn->query($sql) === TRUE) {

Code:
  function createApplication($firstName, $lastName, $address, $city)
  {
    $conn = mysql_connect('localhost', 'user', 'passme123!', 'jobApplications');
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    } 
    else {
      $sql = "INSERT INTO tbl_jobApplications (firstName, lastName, address, city)
          VALUES ('$firstName', '$lastName', '$address', '$city')";

     if ($conn->query($sql) === TRUE) {  
        echo "New record created successfully";
      } 
      else {
       echo "Error: " . $sql . "<br>" . $conn->error;
      }

    }
    
    mysql_close($conn);
    
  }

I'm trying to create a function called createApplication which takes a set of arguments and inserts it into a database table. I think my overall function syntax is correct but how I'm calling the query is wrong. Also, I removed all but 4 of the arguments for sake of making code more legible.

Thoughts?
 
Last edited:
Nevermind.

I just spent some time learning about mysql_ vs mysqli_ in php

Using mysqli_ made everything work and it looks like mysqli_ is recommended for any modern development.
 
Back
Top