Jump to content
MakeWebGames

Recommended Posts

Posted

Add numbers to MySQL database results

 

<?php

/*

This will add a number in front of each row

eg.

1 - Legend-Killer
2 - Mdshare
3 - Scarlet
4 - LostOne

*/

$sql = "SELECT name FROM people";

$result = mysql_query($sql);

$thenumber = 1;

while ($row = mysql_fetch_array ($result)) {

   echo $thenumber . ' - ' . $row['name'];

   $thenumber++;
} 

?>

 

Add data to a MySQL database

 

<?php

// Do your insert query...
mysql_query("INSERT etc...");

// This finds the id of the row once it has been added...
$id = mysql_insert_id();

// Display it...
echo $id;

?>

 

Create a random password

 

<?php

/**
* The letter l (lowercase L) and the number 1
* have been removed, as they can be mistaken
* for each other.
*/

function createRandomPassword() {

   $chars = "abcdefghijkmnopqrstuvwxyz023456789";
   srand((double)microtime()*1000000);
   $i = 0;
   $pass = '' ;

   while ($i <= 7) {
       $num = rand() % 33;
       $tmp = substr($chars, $num, 1);
       $pass = $pass . $tmp;
       $i++;
   }

   return $pass;

}

// Usage
$password = createRandomPassword();
echo "Your random password is: $password";

?> 

 

Add data to a MySQL database

 

<?php

/**
* Change the first line to whatever 
* you use to connect to the database.
*
* We're using two values, title and
* text. Replace these with whatever
* you want to add to the database.
*
* Finally, change tablename to the 
* name of your table.
*/

// Your database connection code
db_connect();

$query = "INSERT INTO tablename(title, text) VALUES('$title','$text')";

$result = mysql_query($query);

echo "The data has been added to the database.";

?>

 

Total Load Time

 

<?php

// Insert this block of code at the very top of your page:

$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$start = $time;

// Place this part at the very end of your page

$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$finish = $time;
$totaltime = ($finish - $start);
printf ("This page took %f seconds to load.", $totaltime);

?> 

 

Connect to a MySQL database

Many will be familiar, as you need to do this for the MC codes, if you dont have an installer :p

 

<?php

/**
* Edit the database settings to your own.
* To use, just include the function and 
* call it using <?php db_connect(); ?>
*
* You can then do your database queries
*/

// Edit your database settings
$db_host = "localhost";
$db_user = "username";
$db_pass = "password";
$db_name = "databasename";

function db_connect() {
   global $db_host;
   global $db_user;
   global $db_pass;
   global $db_name;
   $connection = mysql_connect($db_host,$db_user,$db_pass);
   if (!(mysql_select_db($db_name,$connection))) {
       echo "Could not connect to the database";
   }
   return $connection;
}

?>

 

Validate an email address using regular expressions

If you want a PHP script to verify an email address then use this quick and simple PHP regular expression for email validation. This is also case-insensitive, so it will treat all characters as lower case. It is a really easy way to check the syntax and format of an email address.

 

<?php

$email = "[email protected]";

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
 echo "Valid email address.";
}
else {
 echo "Invalid email address.";
}

?>

 

Count and display the number of rows in a mysql database table

This code will count the number of rows (entries / records) in a MySQL database table and then display it using echo on the screen.

 

<?php

/*

 Datebase connection code goes here.
 If you don't know how to do it, see above :)

 Change tablename to match the name of your own table

*/

// Connect to the database
db_connect();

// Query the database and get the count
$result = mysql_query("SELECT * FROM tablename");
$num_rows = mysql_num_rows($result);

// Display the results
echo $num_rows;

?>

 

Display date and time with PHP

Quick one-liner to output the current server date and time on a page.

 

<?php

/**
* Just add this in your page where you
* want the date/time to appear
*
* For more configuration options look
* in the PHP manual at [url]http://uk2.php.net/date[/url]
*/

// Displays in the format Saturday, November 22, 2003 11.38
echo date("l, F d, Y h:i" ,time());

?>

 

Hope you found this particular tutorial helpful

¬LK

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.   Paste as plain text instead

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