Advertisement
akhfa

TA-php test script

Sep 8th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2. // Let's pass in a $_GET variable to our example, in this case
  3. // it's aid for actor_id in our Sakila database. Let's make it
  4. // default to 1, and cast it to an integer as to avoid SQL injection
  5. // and/or related security problems. Handling all of this goes beyond
  6. // the scope of this simple example. Example:
  7. //   http://example.org/script.php?aid=42
  8. if (isset($_GET['aid'])) {
  9.     $aid = $_GET['aid'];
  10. } else {
  11.     $aid = 1;
  12. }
  13.  
  14. // Connecting to and selecting a MySQL database named sakila
  15. // Hostname: 127.0.0.1, username: your_user, password: your_pass, db: sakila
  16. $mysqli = new mysqli('127.0.0.1', '<username>', '<password>', '<database>');
  17.  
  18. // Oh no! A connect_errno exists so the connection attempt failed!
  19. if ($mysqli->connect_errno) {
  20.     // The connection failed. What do you want to do?
  21.     // You could contact yourself (email?), log the error, show a nice page, etc.
  22.     // You do not want to reveal sensitive information
  23.  
  24.     // Let's try this:
  25.     echo "Sorry, this website is experiencing problems.";
  26.  
  27.     // Something you should not do on a public site, but this example will show you
  28.     // anyways, is print out MySQL error related information -- you might log this
  29.     echo "Error: Failed to make a MySQL connection, here is why: \n";
  30.     echo "Errno: " . $mysqli->connect_errno . "\n";
  31.     echo "Error: " . $mysqli->connect_error . "\n";
  32.  
  33.     // You might want to show them something nice, but we will simply exit
  34.     exit;
  35. }
  36.  
  37. // Perform an SQL query
  38. $sql = "SELECT actor_id, first_name, last_name FROM actor WHERE actor_id = '$aid'";
  39. if (!$result = $mysqli->query($sql)) {
  40.     // Oh no! The query failed.
  41.     echo "Sorry, the website is experiencing problems.";
  42.  
  43.     // Again, do not do this on a public site, but we'll show you how
  44.     // to get the error information
  45.     echo "Error: Our query failed to execute and here is why: \n";
  46.     echo "Query: " . $sql . "\n";
  47.     echo "Errno: " . $mysqli->errno . "\n";
  48.     echo "Error: " . $mysqli->error . "\n";
  49.     exit;
  50. }
  51.  
  52. // Phew, we made it. We know our MySQL connection and query
  53. // succeeded, but do we have a result?
  54. if ($result->num_rows === 0) {
  55.     // Oh, no rows! Sometimes that's expected and okay, sometimes
  56.     // it is not. You decide. In this case, maybe actor_id was too
  57.     // large?
  58.     echo "We could not find a match for ID $aid, sorry about that. Please try again.";
  59.     exit;
  60. }
  61.  
  62. // Now, we know only one result will exist in this example so let's
  63. // fetch it into an associated array where the array's keys are the
  64. // table's column names
  65. $actor = $result->fetch_assoc();
  66. echo "Sometimes I see " . $actor['first_name'] . " " . $actor['last_name'] . " on TV.";
  67.  
  68. // Now, let's fetch five random actors and output their names to a list.
  69. // We'll add less error handling here as you can do that on your own now
  70. $sql = "SELECT actor_id, first_name, last_name FROM actor ORDER BY rand() LIMIT 5";
  71. if (!$result = $mysqli->query($sql)) {
  72.     echo "Sorry, the website is experiencing problems.";
  73.     exit;
  74. }
  75.  
  76. // Print our 5 random actors in a list, and link to each actor
  77. echo "<ul>\n";
  78. while ($actor = $result->fetch_assoc()) {
  79.     echo "<li><a href='" . basename($_SERVER['SCRIPT_FILENAME']) . "?aid=" . $actor['actor_id'] . "'>\n";
  80.     echo $actor['first_name'] . ' ' . $actor['last_name'];
  81.     echo "</a></li>\n";
  82. }
  83. echo "</ul>\n";
  84.  
  85. // The script will automatically free the result and close the MySQL
  86. // connection when it exits, but let's just do it anyways
  87. $result->free();
  88. $mysqli->close();
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement