Jump to content

how to write a php class & function that returns a MySQL SELECT Query result


Recommended Posts

Hey all, and happy 2011!

 

I'm currently working on a website using PHP5 and MySQL Database, and I really need help with how to use a PHP function (performing a SQL SELECT search) that has been written in a different PHP file.

 

In other words, I need help with how to write a php function that performs a MySQL query? And, how to call/use that function in another php file?

 

Here is the situation, and I have made up a similar and extremely simplified example to explain my problem:

This example includes 3 files:

[*]index.php

[*]Functions.php

[*]users.sql

 

 

In index.php, a user enters his/her username in form textfield called username. After the form has been submitted, the username from the form, is stored in a php variable called $a_username. Then, the  $a_username calls the search function from the Functions.php (The file is included at the beginning of index.php file), the functions processes the input data and returns the result in another php variable called $a_birthdate

 



<?php
// allows page to use the search() function in the Functions class
include(Functions.php);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
    <div id="input">
    <form action="#" method="post">
          <p><label>Your username:<input type="text" name="username" id="username" /> </label></p>
          <p> <input type="submit" name="find" id="find" value="Find your birthdate" /></p>
  </form>
    </div>
    <div id="output">
    <?php
// retrives the form value and processes the "username" textfield after the form has been posted
if(isset($_POST['username'])) {
$a_username = $_POST['username'];
$a_birthdate = Functions::search($a_username);

$error = "person/birthdate not found";
if($a_birthdate == $error) { ?>
<p><?php echo $error . ". Please try again."?></p>
<?php }
else ?>
<p><?php echo "Your birthdate is: " . $a_birthdate; ?></p>
<?php }
?>
    </div>
</body>
</html>

 

Functions.php contains a class called Functions and a static function called search(). The functions takes a username, and does a MySQL SELECT in the user.sql database to find the birthdate for the user. Then, it returns the birthdate. If no match is found, then it returns an error message.

 



<?php
// connects to user.sql, contains MySQL database connection info
require_once('connections.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// the class starts here
class Functions {

// searches the birthdates table and return the birthday os the supplised input username. If not found, an error message is returned
static function search($a_username) {
mysql_select_db($database_users, $users);
    $query_birthday = "SELECT * FROM birthdates WHERE username = '$a_username';
    $birthday = mysql_query($database_users, $users) or die(mysql_error());
    $row_birthday = mysql_fetch_assoc($birthday);
    $totalRows_birthday = mysql_num_rows($birthday);

    if(totalRows_birthday < 1) {
        return "person/birthdate not found";
    }
    else
        return $row_birthday
}
}

?>

<?php
mysql_free_result($birthday);
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.