DevTek.Org Code Snippets
Getting the ID of a record just inserted into a MySQL DB
To get the value of an auto-incrementing field in a MySQL DB after
you insert a record, use the following right after you do the
mysql_query():
<?php
$idNum = mysql_insert_id();
?>
Of course the $idNum can be any variable name you'd like.
Some examples of use would be where you are building a trouble ticket system and want to show the customer their call ticket number. Another example would be if you were updating two or more MySQL servers where the master server auto-increments the ID field and you use that ID to insert a record on the other servers which do not auto-increment their ID field.
Update records on multiple MySQL Servers
This snippet uses a function to add a record to three different MySQL servers. There are a few things that must be in place to use this:
- All the servers must have the same database name.
- All the servers must have the same table name.
- All the servers must have the same field names.
- All the servers must have the same user with rights to connect from the server this function is running from. This user must have rights on the table.
- This function is written to just add/insert records in the DBs. It can be written to do edits and deletes too.
<?php
//Define variables for the database and connect to the database
$dbserver1="www.server1.com"; # Primary DB Server
$dbserver2="www.server2.com"; # Secondary DB Server
$dbserver3="www.server3.com"; # Tertiary DB Server
$dbuser="DB_USER";
$dbpassword="DB_USER_PASSWORD";
$dbname="DB_NAME";
$tblname="TABLE_NAME";
function addLink($serverName, $dbname, $tblname, $dbuser, $dbpassword, $id="")
{
GLOBAL $idNum;
//Setting up the DB connection to the server
if ($conn=mysql_connect($serverName,$dbuser,$dbpassword))
{
@$select=mysql_select_db($dbname, $conn);
//if it's not the master server we want to insert the ID held in $idNum
if ($serverName != "www.server1.com")
@$sqlupdate="INSERT INTO " . $tblname . "(id, field2, field3) VALUES ('" . $id . "','" . $_POST['field2'] . "','" . $_POST['field3'] . "')";
else
@$sqlupdate="INSERT INTO " . $tblname . "(field2, field3) VALUES ('" . $_POST['field2'] . "','" . $_POST['field3'] . "')";
$result=mysql_query($sqlupdate) or die(mysql_error());
//if the current server is the master, get the just inserted ID.
if ($serverName == "www.server1.com")
$idNum = mysql_insert_id();
$whatHappened = '<div align="center"><font color="green"><b>Successful add on ' . $serverName . '</b></font></div><br>' . "\r\n";
}
else
$whatHappened = '<div align="center"><font color="red"><b>Failed add on ' . $serverName . '</b></font></div><br>' . "\r\n";
@mysql_close($conn);
RETURN $whatHappened;
}
if ($_POST['add'] == "add")
{
$whatHappened = addLink($dbserver1, $dbname, $tblname, $dbuser, $dbpassword);
$whatHappened .= addLink($dbserver2, $dbname, $tblname, $dbuser, $dbpassword, $idNum);
$whatHappened .= addLink($dbserver3, $dbname, $tblname, $dbuser, $dbpassword, $idNum);
}
?>
Timing a Chunk of Code
<?php
//Set up beginning of timer
$stime = microtime(true);
/*
Code to be timed here.
*/
//Set up end of timer
$etime = microtime(true);
//Display results
echo "Your code completed in " . floor($etime - $stime) . " seconds!";
?>
Find a challenge/response e-mail list subscriber
I own and operate numerous technical help discussion lists. We had a problem with a person who had subscribed an address using a challenge / response system, the one where if they haven't previously authorized your e-mail address, you receive a message from the challenge response system stating you must reply to the e-mail to verify you're a human.
While in theory this might seem like a good system to have, it wreaks havoc on a discussion list because when someone posts a message to the list, they get that challenge e-mail. The problem with most of these systems is that it doesn't tell you which e-mail address generated the challenge, so for list owners it makes it nearly impossible to track that address down to remove it.
I wrote the below script to send an individual probe e-mail to each subscriber address and placed the ID from the database in the subject so that when I received the challenge back, I could match the ID with the address in the database.
This script assumes you have a database with two fields. One called "id" and one called "email".
<html>
<head>
<title>List Message Probe</title>
</head>
<body bgcolor="silver">
<P align=center><FONT size=10>List Message Probe</FONT></p>
<P align=center><FONT size=5>
The mail probe is being sent.<br>
Do <b>not</b> close this window until you see the "Probe complete" message.
</FONT></P>
<?php
$dbserver="SERVER";
$dbuser="USER";
$dbpassword="PASSWORD";
$dbname="DATABASE";
$table="USER_TABLE"
$conn=mysql_connect($dbserver,$dbuser,$dbpassword) or die(mysql_error());
$select=mysql_select_db($dbname, $conn) or die(mysql_error());
$sqlview="SELECT * FROM " . $table;
$result=mysql_query($sqlview);
while ($r=mysql_fetch_assoc($result))
{
$email_to = $r["email"];
$subject = $r["ID"] . " - Please do not respond to this probe";
$message = "Please ignore this message... We're tracking down a problem.
Do *not* reply to this message. It'll only confuse the issue.
Thank you.
List Moderator
";
mail($email_to, $subject, $message, "From: moderators@example.com");
}
echo "Probe Complete.";
?>
</body>
</html>
Lazy Man's Virtual Host
Let's say you have multiple domain names registered and they're all pointing to the same IP on your web hosting services server, but you want each domain to more or less live in its own world. The below snippet placed at the top of your main page in the root folder (i.e. index.php, default.php or whatever it is) will do it for you.
<?php
// Get the requested host from the host header sent by the browser and convert it
// to uppercase.
$hostRequested = strtoupper($_SERVER['HTTP_HOST']);
// Testing if "DOMAIN1" is part of the requested host. Must be in CAPS.
// If so, redirect the browser into SOMEFOLDER1 and load index.php from there.
if (strstr($hostRequested, "DOMAIN1")) {
header('Location: SOMEFOLDER1/index.php');
exit;
}
// Testing if "DOMAIN2" is part of the requested host. Must be in CAPS.
// If so, redirect the browser into SOMEFOLDER2 and load index.php from there.
elseif (strstr($hostRequested, "DOMAIN2")) {
header('Location: SOMEFOLDER2/index.php');
exit;
}
?>
You need to replace DOMAINx and SOMEFOLDERx with the domain and subfolders that match your needs. Remember, DOMAINx needs to be in caps but SOMEFOLDERx does not.
This was a quick hack for a friend of mine who ran into this
problem. If you really want to do this right, try out one of the
free DNS management services. I recommend
MyDomain as I've been using
them for many years and have never had any problems with their
service. These services will allow you to do what's called "stealth"
or "masked" redirects to do what the above script does.
PHP Easter Egg Put the following in the URL after the .php page ?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000 i.e. http://www.example.com/index.php?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000 Note: You can prevent this by setting "expose_php = Off" in
your php.ini





