PHP: mysql_insert_id()

July 16, 2009 by  
Filed under Tips and Tutorials

tn-phpSometimes it is necessary to get the ID value of the last record you inserted into a MySQL database.

For example, let’s say you have a shopping cart function that added records into the database. But you need to print out a receipt that includes the item tracking number. And this tracking number would be the record’s ID value, which is also the PRIMARY KEY of the table.

An easy way to do this in PHP is by using the function mysql_insert_id().
NOTE: This will only work for AUTO_INCREMENT fields.

Pretend STEP 1 is a form with a list of items. If you change the QUANTITY of the item, and submit, you will be at STEP 2, which is the sample code below.
Read more

Related Products:

The Code Book: The Science of Secrecy from Ancient Egypt to Quantum CryptographyThe Code Book: The Science of Secrecy from Ancient Egypt to Quantum CryptographyIn his first book since the bestselling Fermat's Enigma, Simon Singh offers the first sweeping history of encryption, tracing its evolution and reveal... Read More >
The Joy of PHP: A Beginner's Guide to Programming Interactive Web Applications with PHP and mySQLThe Joy of PHP: A Beginner's Guide to Programming Interactive Web Applications with PHP and mySQLThird Edition now with bonus chapters. Have you ever wanted to design your own website or browser application but thought it would be too difficult o... Read More >
Code Thief: Code ThiefCode Thief: Code Thief(Revised Edition Jan '17: Edited by, New York Times Best Selling Editor: Robin Smith) THE CODE THIEF is a fast paced, cyber, espionage thriller. A yo... Read More >

if $IE

July 11, 2009 by  
Filed under Geek Fun & Humor, PHP

I was working on a web project and I usually start with Firefox to test things. Then Safari, Chrome, and then IE. By the time I got to IE, I noticed that my hard work was just not viewable. Argh. So I tweeted out:

tweet-browsercheck

function browserCh() 
{ 
     $a = $_SERVER['HTTP_USER_AGENT']; 
     $IE = ((eregi("MSIE",$a) || eregi("Microsoft",$a)))==1; 

     if($IE) 
        return 'Bad Browser!';
}

I got a laugh at some of the responses.. :)

@honging : daynah, i prefer: <style type="text/css">* html body { display: none; }</style> ;)

@j_holtslander: @daynah that was kinda hot. LOL.

@robflynn43: @daynah Haha, browserCh() made me happy. You win a high five, redeemable any time you see me. =)

@ialexs@daynah :D (re: bad browser)

Related Products:

Let's Pretend This Never Happened: A Mostly True MemoirLet's Pretend This Never Happened: A Mostly True MemoirFrom the New York Times bestselling author of Furiously Happy...

When Jenny Lawson was little, all she ever wanted was to fit in. That dream wa... Read More >
The Joy of PHP: A Beginner's Guide to Programming Interactive Web Applications with PHP and mySQLThe Joy of PHP: A Beginner's Guide to Programming Interactive Web Applications with PHP and mySQLThird Edition now with bonus chapters. Have you ever wanted to design your own website or browser application but thought it would be too difficult o... Read More >
Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)The fully revised, updated and extended 4th edition of the hugely popular web development book - includes CSS, HTML5, jQuery and the mysqli extension.... Read More >

iPhone App Review: PHP Ref

October 16, 2008 by  
Filed under Apple, Reviews

PHP RefName: PHP Ref
Written By: Joseph Frizalone
Category: Utilities
Price: $.99
Size: 1.3 MB
Requirements: iPhone / iPod Touch

This is a great app for any PHP developer. It’s a quick reference to all 5149 standard PHP functions. You can search for a function or scroll through a list of them. Tap on the function and a description of its purpose and parameters will be displayed. The only improvement I would like is an example of how the function can be used. Other than that, it’s pretty nice.

PHP Ref PHP Ref PHP Ref

Related Products:

Modern PHP: New Features and Good PracticesModern PHP: New Features and Good Practices

PHP is experiencing a renaissance, though it may be difficult to tell with all of the outdated PHP tutorials online. With this practical guide, you... Read More >

Apple iPhone 6 16GB Factory Unlocked GSM 4G LTE Smartphone, Space Grey (Certified Refurbished)Apple iPhone 6 16GB Factory Unlocked GSM 4G LTE Smartphone, Space Grey (Certified Refurbished)What's in the box: Unlocked iPhone , USB Cable/Adapter. Comes in a Generic Box with a 90 days Limited Warranty. *IMPORTANT* - "GSM unlocked.

How to Connect to a MySQL Database using PHP

March 29, 2005 by  
Filed under Code Snippets, PHP, Scripts and Coding

I was helping my coworker yesterday to create her first PHP-Database program. She already knew how phpMyAdmin worked, so I created a sample page for her to show how to connect to the database and then retrieve data from a query. Maybe this sample page could help you too. I commented as much of the code as I could. Let me know if you have any questions.

Read more

Related Products:

PHP: Learn PHP in 24 Hours or Less - A Beginner's Guide To Learning PHP Programming Now (PHP, PHP Programming, PHP Course)PHP: Learn PHP in 24 Hours or Less - A Beginner's Guide To Learning PHP Programming Now (PHP, PHP Programming, PHP Course)Learn PHP In 24 Hours Or Less!
In this book you will find detailed instructions on how to learn the basics of the PHP language.

This eBook w... Read More >
Seven Databases in Seven Weeks: A Guide to Modern Databases and the NoSQL MovementSeven Databases in Seven Weeks: A Guide to Modern Databases and the NoSQL Movement

Data is getting bigger and more complex by the day, and so are the choices in handling that data. As a modern application developer you need to un... Read More >

Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)The fully revised, updated and extended 4th edition of the hugely popular web development book - includes CSS, HTML5, jQuery and the mysqli extension.... Read More >

Random Splash Pages

April 26, 2002 by  
Filed under Code Snippets, PHP

I was looking through my email and found this email from Grace:

Hi Daynah! It’s me, Grace. I would like to ask a question if you don’t mind -_-; Sorry if I am bothering you. I would like to ask if how can I make random splash pages with php? Or do you know any scripts I could download for
that? Thank you so much. And hope you are having a great week =)

Here is a piece of code that Albert (my boyfriend) wrote. It creates an array of random images to use. It’s pretty simple to modify if you want to create random splash pages. If you have comments/questions, please leave them below.

<?php

// Function name: randomimage($showimage)
// Purpose: Create an array with random data for an image
// Use: Please link http://php-princess.net/
function randomimage($showimage)
{

    // ARRAY OF SPLASH IMAGES
    $bgimages = array('images/splash1.jpg','images/splash2.jpg',
                      'images/splash3.jpg','images/splash4.jpg',
                      'images/splash1.jpg','images/splash2.jpg',
                      'images/splash3.jpg','images/splash4.jpg');

    $bgtext = array ('Picture of Splash 1','Picture of Splash 2',
                     'Picture of Splash 3','Picture of Splash 4',
                     'Picture of Splash 1', 'Picture of Splash 2', 
                     'Picture of Splash 3', 'Picture of Splash 4');

    $bgmax = count($bgimages)-1;

    // Check if viewing additional image option is 
    // set / look for non-integer value passing
    if ((!isset($showimage)) || (!ereg ("^[0-9]{1,2}$", $showimage)))
    {
        srand((double)microtime()*1000000);    
        print_r($names);  

        srand ((double) microtime() * 10000000);

        $rand_keys = array_rand ($bgimages, 2);

        // obtain random image from array
        $bgimage = $bgimages[$rand_keys[0]];
        $bgalt   = $bgtext[$rand_keys[0]];
        $bgkey   = $rand_keys[0];
    }    

    return array($bgimage,$bgalt,$bgkey);
}

// Creates a list of the variables with random image 
list($bgimage,$bgalt,$bgkey) = randomimage($showimage);

// Put printout into variable $mainbody
$mainbody = ''.$bgalt.'';

// Print out $mainbody
print "$mainbody";

?>

Related Products:

Clean Code: A Handbook of Agile Software CraftsmanshipClean Code: A Handbook of Agile Software CraftsmanshipEven bad code can function. But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significa... Read More >
Break the Code: Cryptography for Beginners (Dover Children's Activity Books)Break the Code: Cryptography for Beginners (Dover Children's Activity Books)No, it's not a message from outer space! Just read it backwards, starting from the bottom line, to discover what this specially coded message says. (T... Read More >
Code: The Hidden Language of Computer Hardware and SoftwareCode: The Hidden Language of Computer Hardware and Software

What do flashlights, the British invasion, black cats, and seesaws have to do with computers? In CODE, they show us the ingenious ways we manipulat... Read More >