简体   繁体   中英

PHP function within MySQL result

I am very new to PHP. I have been programming in PLSQL and .NET for a while. I have a tricky assignment since I lack PHP experience. n1, n2, n3 are all numeric values identifiers for employees. I want to call a function that looks up the employee by the number and return the employee name on the html page. In PLSQL I would create a function and call it but I am not sure how to do it here.

<?php
  $servername = "localhost";
  $username = "username ";
  $password = "password";
  $dbname = "dbname ";


  $conn = new mysqli($servername, $username, $password, $dbname);

   if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  function GetEmployee($nEmployee){
    $result = mysql_query("SELECT szLastName FROM employee WHERE nEmployee = 
    $nEmployee ") or trigger_error(mysql_error()); 
    return $result;
  }

  $sql = "SELECT idPosition, n1, n2, n3 FROM offense ORDER BY idPosition";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {
     echo "<tr><td>".$row["idPosition"]."</td><td>".$row[$GetEmployee("n1")]."
    </td>
    <td>".$row[$GetEmployee("n2")]."</td><td>".$row[$GetEmployee("n3")]."</td>
    </tr>";
  }
  } else {
  echo "<tr><td>"%nbsp;"</td><td>"%nbsp;"</td><td>"%nbsp;"</td><td>"%nbsp;"
  </td>
  </tr>";
  }
  $conn->close();
?>  

There are quite a few bugs in there.

  1. You are using both mysql and mysqli ( mysql is deprecated and you should rather use PDO than mysqli, but if you wanna keep it, keep it with mysqli everywhere in your code ).
  2. Instead of $row[$GetEmployee("n1")] you should use $row[GetEmployee("n1")]
  3. Once you change your functions from mysql to mysqli you'll have to pass the connection variable and to access it inside your getemployee function you have to either make it global by declaring global $conn; inside your getemployee function or to pass it as parameter.
  4. After setting those right, you'll probably stil get "Undefined offset: 0" error because you are trying to access an element of the array at a key that doesn't exist in your case.

Here it is fixed, but I might've missunderstood your table structure or expected output since you haven't provided any examples, but I'm confident you'll find what you need in it as it takes the n1, n2, n3 from offense and then pull szLastName for each one of them from employee:

<?php
  $servername = "localhost";
  $username = "username";
  $password = "password";
  $dbname = "database";

  $conn = new mysqli($servername, $username, $password, $dbname);

   if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

    function getEmployee($nEmployee){
        //Declare your global conn to be able to use it inside
        //the function, HOWEVER, this is not recommendet.
        global $conn;
        $resultEmployee=mysqli_query($conn, "Select szLastName from employee WHERE nEmployee = $nEmployee");
        while($row=mysqli_fetch_assoc($resultEmployee)){
            return $row['szLastName'];
        }
    }

    $resultNumbers = mysqli_query($conn, "SELECT idPosition, n1, n2, n3 FROM offense ORDER BY idPosition");
    if($resultNumbers ->num_rows > 0){
        while($row = mysqli_fetch_assoc($resultNumbers)){
            echo "<tr><td>";
            echo $row['idPosition'];
            echo "</td>";

            echo "<td>";
            echo getEmployee($row['n1']);
            echo "</td>";

            echo "<br/>";

            echo "<td>";
            echo getEmployee($row['n2']);
            echo "</td>";

            echo "<br/>";

            echo "<td>";
            echo getEmployee($row['n3']);
            echo "</td></tr>";

        }
    }

  $conn->close();
?> 

If you are new to PHP and want to continue, you should write your PHP to MySQL connections using PDO. (A very good source is: https://phpdelusions.net/pdo )

Also this is my first answer on StackOverflow, I hope you find it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM