How To Get The Last MySQL Auto-ID in PHP

Posted on March 6, 2008 by admin.
Categories: PHP Programming.

There will be times when you will need to retrieve the last auto id on a table where you just did an insert. They way to do this is as follows:

<?php

$link = mysql_connect(’host’, ‘user’, ‘pass’);

if(!$link)
{
die(’could not connect: ‘. mysql_error() ) ;
}

mysql_query(”INSERT INTO table (field) values (’val1′)” );

echo “Last ID is “.mysql_insert_id());

Be sure you do this directly after the insert, if you need to use it for something, because it changes anytime another insert is done on a table with an auto id.

I have also noticed weird results on the first entry of a blank table, so watch out for that.

del.icio.us Slashdot Digg Facebook Technorati StumbleUpon Yahoo Ask

1 comment.

Easy PHP and Html Bar Graph

Posted on February 25, 2008 by admin.
Categories: PHP Programming.

I want to show you a quick and simple way to create a bar graph using HTML.

You need a graphic file that’s just 1 pixel wide and 5 px tall. Make it whatever color you want your bars to be. I named mine CountBar.gif.

In my example, I have an array of data for each month, containing sales. The key part to the whole concept can be seen here: (the table was created prior to this code block, and is ended after, I am just showing you the relevant section.

Basically, we loop through each month, $arrMonths contains the string values of each month like “January”, ect. $arrResults[$i]; contains the sales values for each corresponding month. (don’t get confused by my arrays, just focus on the concept).

The Key part is here: We set the width of the graphic equal to the amount of sales for the month divided by a static variable (we use this to keep the widths in a relative range, in this case, i know my sales data ranges from 0-50,000 so I want the width of my graph to go from 0-500 pixels)

<img src=”countBar.gif” width=”<?php echo intval($arrResults[$i] / 100);
$total += $arrResults[$i]; ?>” height=”5″ class=”chart”>

<?php

$total = 0;

for ($i = 0; $i < 12; $i++) {
?>
<tr>
<td width=”15%” height=”12″ align=”left” valign=”middle”><?php echo $arrMonths[$i + 1].” “;?></td><td width=”15%” height=”12″ align=”left” valign=”middle”>$<?php echo ” “.$arrResults[$i]; ?>: </td>
<td width=”70%” height=”12″><img src=”countBar.gif” width=”<?php echo intval($arrResults[$i] / 100);
$total += $arrResults[$i]; ?>” height=”5″ class=”chart”></td>
</tr>
<?php
}
?>

And you can get something like this: ( i have marked out theĀ  actual values on purpose)

del.icio.us Slashdot Digg Facebook Technorati StumbleUpon Yahoo Ask

no comments yet.

PHP Quick Reference Study Guide

Posted on February 17, 2008 by admin.
Categories: PHP Programming.

Comments In PHP

#this is a comment, to EOL

//this is also a comment, to EOL

/* This is also a comment

For multi lines

*/

Variables

 

Must start with (A-Za-z) or _(underscore)

like:

$blah

$_blah

$H4×0rs

Illegial Variables

$7337

$*UCK

Variables in PHP are loosely typed, and do not need to be declared prior to use.

So the following are all legal statements

$shane = “Shane Is Leet”;

$shane = 10 * 4 + 3;

Variable types in php are interchangeable, so I used the variable $shane as a string in one line, and then as an integer in another, and php dosen’t mind at all.

PHP does support indirect variable references, but I personally don’t use them. Here is a brief example:

$var1 = “Shane”;

$$var1 = “Who Is The 7337″;
print $Shane; # this will output “Who Is The 7337″

You can use as many levels of indirection as you can stomach.

Variable Management

  • isset()
  • unset()
  • empty()

isset() example:

if(isset($shane) {

print ‘$shane is set’;

}

unset() example:

unset($shane);

if(!isset($shane)) {

echo ‘$shane is unset’;

}

empty() is typically used to check form values, the variables value is converted to a bool, then checked for true/false, example:

if (empty($shane)) {

print ‘Error: $shane is empty’;

}
This prints the error message if $shane dosen’t contain a value that evaluates to true.

Data Types

PHP Suports, Integer, Hex (like 0xABCD) - Values (-100)

Floating Point like: 3.14, +0.1e-4, -1600.3, 22.6E42

Strings:

PHP supports strings, beginning and ending with single or double quotes, and allows embedding of variables like in the previous examples.

When using single quotes, you lose some of the escape characters support when using double quotes

Double Quote Escape Characters:

  • \n #newline
  • \t #Tab
  • \” #double quote
  • \\ #backslash
  • \0 #ASCII 0 (null)
  • \r #linefeed
  • \$ #the character $
  • \(Octal # ) #ex \70 would be the letter 8
  • \x(hex #) # like \0×32 is the letter 2

Single Quote Escape Characters:

  • \’ #single uote
  • \\ #backslash

Null

Null is a data type with only one value: the NULL value. Use it thusly:

$shane = NULL;

Arrays

Straitforward, array(1, 2, 3) or array(0 => 1, 1 => 2, 2 =>3)

$var1 = array(”name” => “Shane”, “age” => “28″);

echo $var1[”name”]; #prints “Shane”

There is quite a lot to arrays in php, so for more info check Arrays In PHP

To Iterate across the array, I typically use foreach()

$players = array(”Shane”, “Craig”, “Sandra”, “Meme”);

foreach($players as $key => $value) {

print “#$key = $value\n”;
}

Output is:

#0 = Shane

#1 = Craig

#2 = Sandra

#3 = Meme

You can also traverse the array using list() and each(), which I will not go over here.

More to come later.

del.icio.us Slashdot Digg Facebook Technorati StumbleUpon Yahoo Ask

3 comments.