Have created a function that will query a database and return the number of rows as a result of the query.
I use this function as part of log in access of myweb site. The first section of code is on the log in page.
$username = $_POST['usernamel'];
$password = $_POST['password'];
include "functionfile.php";
$rowqry = "SELECT * FROM tablename WHERE username = '$username' AND password = '$password' ";
if(numofrows($rowqry) == 1) // calling the fuction and checking that the results equal 1
{
// if result is correct add the info that you want to display here
}
else
{
echo "Your password or username may be incorrect, please try again";
}
This is the function that is called, I have a single fill (functionfile.php) with all my functions that I include in the code.
function numofrows($rowqry)
{
$host = "localhost"; // database host
$un = "username"; // database username
$pw = "password"; // database password
$dbname = "database"; // database name
$dbase = mysql_connect($host, $un, $pw);
if (!$dbase)
die ( "No Connection"); // connecting to database
mysql_select_db($dbname, $dbase)
or die ("Could not open $dbname: ".mysql_error()); // Opening database
$result = mysql_query($rowqry, $dbase); // applying query to database
$rows = mysql_num_rows($result); // counting rows in the applied query
return $rows; // returns result to fuction
}