How to change the sorting type from our web using PHP?

Stiebel Eltron
Stiebel Eltron used Ask the Experts™
on
<?php
session_start();

$edition = $_GET['edition'];
$description = $_GET['description'];
$category_id = $_GET['category_id'];
$country_id = $_GET['country_id'];
$city_id = $_GET['city_id'];
$year = $_GET['year'];
$firstdate = $_GET['firstdate'];
$lastdate = $_GET['lastdate'];
$status_id = $_GET['status_id'];
$source = $_GET['source'];
$remark = $_GET['remark'];
$sort_field = $_GET['sort_field'];
$sort_type = $_GET['sort_type'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>:::My Sbux Collections:::</title>

<style TYPE="text/css">
<!--
	a
 	{
  		color:#000080;
		font-family:verdana;
		text-decoration:none;
		font-size:12px
	}
	
	
	a:active
 	{
  		color:red;

	}

	a:hover
 	{
  		color:#FF0000;
		text-decoration:none;
		cursor:crosshair;
	}
		
	table
	{
		font-family:verdana;
	}
	
	.td
	{
		font-family:verdana;
		font-size:10px;
		font-weight:lighter;
		text-align:center;
	}

	.td1
	{
		font-family:verdana;
		font-size:10px;
		font-weight:lighter;
		text-align:center;
		color:#FFF;
	}

	.title
	{
		font-family:verdana;
		font-size:12px;
		font-weight:bold;
		text-align:center;
	}
	
	.header
	{
		font-family:Tahoma;
		font-size:17px;
		font-weight:bold;
		text-align:left;
	}

	.b
	{
		font-color:#FF0000;
		font-family:verdana;
		font-size:12px;
	}
	
	body
	{
	background-color:#0c3f00;
	background-size:cover;
	margin-top: 0px;
	}
	
	.searchspan {
		font-family:Tahoma, Geneva, sans-serif;
		font-size:12px;
		text-align:right;
		float:right;
		margin-right:15px;
		margin-top:3px;
	}


-->
</STYLE>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'image/Sbux-header1.jpg'
theImages[1] = 'image/Sbux-header2.jpg'
theImages[2] = 'image/Sbux-header3.jpg'
theImages[3] = 'image/Sbux-header4.jpg'
theImages[4] = 'image/Sbux-header5.jpg'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//  End -->
</script>

</head>
<body>

<table width="900" border="1" cellspacing="0" cellpadding="5" style="border-collapse:collapse" bordercolor="#999999" align="center">
  <tr>
  	<td colspan="13" style="background-size:cover;" width="100%" height="100%" align="center" bgcolor="#FFFFFF">
    	<SCRIPT LANGUAGE="JavaScript">
    	<!-- Begin
		showImage();
		//  End -->
		</script>		
    </td>
  </tr>
  <tr>
  	<td colspan="13" bgcolor="#FFFFFF">
    	<?php
		include('menu.php');
		?>
    </td>
  </tr>
  <tr>
    <td colspan="13" bgcolor="#D6D6D6" class="header">My Sbux Collections<span class="searchspan"><a href="item_search.php?search_mode=2">Search</a></span></td>
  </tr>
  <tr>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=item.id&sort_type=<?php if ($sort_field == 'item.id' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Mug Details</a><?php if ($sort_field == 'item.id') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6">No.</td>
    <td class="title" bgcolor="#D6D6D6">Picture</td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=item.edition&sort_type=<?php if ($sort_field == 'item.edition' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Edition</a><?php if ($sort_field == 'item.edition') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=item.description&sort_type=<?php if ($sort_field == 'item.description' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Description</a><?php if ($sort_field == 'item.description') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=category.category_name&sort_type=<?php if ($sort_field == 'category.category_name' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Category</a><?php if ($sort_field == 'category.category_name') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=country.country_name&sort_type=<?php if ($sort_field == 'country.country_name' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Country</a><?php if ($sort_field == 'country.country_name') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=city.city_name&sort_type=<?php if ($sort_field == 'city.city_name' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">City</a><?php if ($sort_field == 'city.city_name') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=item.year&sort_type=<?php if ($sort_field == 'item.year' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Year</a><?php if ($sort_field == 'item.year') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=item.date_acq&sort_type=<?php if ($sort_field == 'item.date_acq' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Date Acquired</a><?php if ($sort_field == 'item.date_acq') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=status.status_name&sort_type=<?php if ($sort_field == 'status.status_name' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Status</a><?php if ($sort_field == 'status.status_name') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=item.sorce&sort_type=<?php if ($sort_field == 'item.sorce' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Source</a><?php if ($sort_field == 'item.sorce') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
    <td class="title" bgcolor="#D6D6D6"><a href="item_show.php?edition=<?php echo $edition;?>&description=<?php echo $description;?>&category_id=<?php echo $category_id;?>&country_id=<?php echo $country_id;?>&city_id=<?php echo $city_id;?>&year=<?php echo $year;?>&firstdate=<?php echo $firstdate;?>&lastdate=<?php echo $lastdate;?>&status_id=<?php echo $status_id;?>&source=<?php echo $source;?>&remark=<?php echo $remark;?>&sort_field=item.remark&sort_type=<?php if ($sort_field == 'item.remark' && $sort_type == 'ASC') { echo 'DESC'; } else { echo 'ASC'; }?>">Remark</a><?php if ($sort_field == 'item.remark') { ?> <img src="<?php if ($sort_type == 'DESC') { echo "image/up.png"; } else { echo "image/down.png"; } ?>" width="12" height="12" border="0" /><?php } ?></td>
  </tr>
  <?php
  include('connect.php');
  
  if ($sort_field == '') {
	  $sort_field = 'country.country_name';	  
  }
  
  if ($sort_type == '') {
	  $sort_type = 'ASC';
  }
  
  if ($sort_field == 'country.country_name') {
	  $sort_next = 'city.city_name ASC , item.date_acq ASC';
  } else if ($sort_field == 'city.city_name') {
	  $sort_next = 'item.date_acq';
  } else if ($sort_field == 'item.date_acq') {
	  $sort_next = 'country.country_name ASC, city.city_name ASC';
  } else {
	  $sort_next = 'country.country_name ASC, city.city_name ASC, item.date_acq';
  }
  
  if ($edition != '') {
	  $sql_edition = "AND item.edition LIKE '$edition'";
  }
  
  if ($description != '') {
	  $sql_description = "AND item.description LIKE '$description'";
  }
  
  if ($category_id != '') {
	  $sql_category_id = "AND category.category_id = '$category_id'";
  }
  
  if ($country_id != '') {
	  $sql_country_id = "AND country.country_id = '$country_id'";
  }
  
  if ($city_id != '') {
	  $sql_city_id = "AND city.city_id = '$city_id'";
  }
  
  if ($year != '') {
	  $sql_year = "AND item.year = '$year'";
  }
  
  if ($firstdate != '') {
	  if ($lastdate == '') {
		  $sql_date = "AND item.date_acq = '$firstdate'";
	  } else {
		  $sql_date = "AND item.date_acq >= '$firstdate' AND item.date_acq <= '$lastdate'";
	  }
  }
  
  if ($status_id != '') {
	  $sql_status_id = "AND status.status_id = '$status_id'";
  }
  
  if ($source != '') {
	  $sql_source = "AND item.sorce LIKE '$source'";
  }
  
  if ($remark != '') {
	  $sql_remark = "AND item.remark LIKE '$remark'";
  } 
  
  $query = mysql_query("SELECT item.*,category.*,country.*,city.*,status.* 
					   FROM (((item LEFT JOIN category ON item.category_id = category.category_id)
					   LEFT JOIN country ON item.country_id = country.country_id)
					   LEFT JOIN city ON item.city_id = city.city_id)
					   LEFT JOIN status ON item.status_id = status.status_id
					   WHERE status.status_id != '2'
					   $sql_edition
					   $sql_description
					   $sql_category_id
					   $sql_country_id
					   $sql_city_id
					   $sql_year
					   $sql_date
					   $sql_status_id
					   $sql_source
					   $sql_remark
					   ORDER BY $sort_field $sort_type,  
					   $sort_next") or die (" error 253 ");
  $no = 0;
  while ($result = mysql_fetch_array($query)) {
	  $no++;
	  if ($bgcolor == "#FFFFFF") {
		$bgcolor = "#CCCCCC";
	} else {
		$bgcolor = "#FFFFFF";
	}
  ?>
  <tr bgcolor="<?php echo $bgcolor;?>">
    <td class="td"><a href="item_show_detail.php?id=<?php echo $result['id'];?>">Details</a></td>
    <td class="td"><?=$no;?></td>
    <td class="td"><img src="picture/<?=$result["picture"];?>" width="125" height="100"></td>
    <td class="td"><?php echo $result['edition'];?></td>
    <td class="td"><?php echo $result['description'];?></td>
    <td class="td"><?php echo $result['category_name'];?></td>
    <td class="td"><?php echo $result['country_name'];?></td>
    <td class="td"><?php echo $result['city_name'];?></td>
    <td class="td"><?php echo $result['year'];?></td>
    <td class="td"><?php echo $result['date_acq'];?></td>
    <td class="td" <?php if ($result['status_id'] == '1') {  echo "bgcolor=\"#FFFFCC\"";  } else if ($result['status_id'] == '2') { echo "bgcolor=\"#FFCC99\""; } ?>><?php echo $result['status_name'];?></td>
    <td class="td"><?php echo $result['sorce'];?></td>
    <td class="td"><?php echo $result['remark'];?></td>
  </tr>
  <?php
  }
  ?>
  <tr>
  	<td colspan="13" bgcolor="#666666" class="td1">
		STARBUCKS MUGS COLLECTION &copy;2012
    </td>
  </tr>

</table>

</body>
</html>

Open in new window

Dear EE experts,

Kindly check the attached embed experts for the script of our webpage:
http://www.headhuntermanila.com/sbux/item_show.php

We would like to sort the items by CATEGORY then COUNTRY then by CITY.
At this moment, it is sorted by COUNTRY then by CITY only.

Thank you in advance and hope to hear soon...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
You would need to change the contents of the $sort_field and $sort_type variables, or change the ORDER BY clause on line 264.  Please post the CREATE TABLE statement for the category table and tell us which column you want to sort by.

Author

Commented:
Hi! Could u please explain further? "Please post the CREATE TABLE statement for the category table and tell us which column you want to sort by. "

We would like to sort the items by CATEGORY then COUNTRY then by CITY.
At this moment, it is sorted by COUNTRY then by CITY only.

Thank you & hope to hear soon...
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please run a query to SHOW CREATE TABLE and post the output here.  Then we can see the table definition.  Once we see that we can probably show you how to write the ORDER clause.

Author

Commented:
Hello to you again!

Would it be possible to show me how to run a query to show create table?

As I'm new with this database, specially with MySql db...

Actually, I'm just continuing the other changes/additionals that the designer of this website did. The person who creates this web left already so I need to handle it...

Thank you!
Most Valuable Expert 2011
Top Expert 2016
Commented:
I'm not at my computer now; I'm in the classroom and my teaching set up does not give me full access to my regular libraries.  Make a Google search for "mysql show create table" and see if you can find anything that looks promising.  You may be able to follow the general guidelines shown in these code snippets.  If you run into trouble, please post back and show us the code you're using and the output you're getting.
<?php // RAY_mysql_example.php
error_reporting(E_ALL);


// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQL
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// MAN PAGE: http://php.net/manual/en/function.mysql-query.php
// MAN PAGE: http://php.net/manual/en/function.mysql-errno.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
// MAN PAGE: http://php.net/manual/en/function.mysql-num-rows.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-array.php
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php



// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $err <br/>";
}

// SELECT THE MYSQL DATA BASE
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $err <br/>";
    die('NO DATA BASE');
}
// IF THE SCRIPT GETS THIS FAR IT CAN DO QUERIES




// ESCAPE ALL DATA FIELDS BEFORE USE IN MYSQL QUERIES
$safe_username = mysql_real_escape_string($_POST["username"]);




// CREATE AND SEND A SELECT QUERY AND TEST THE RESULTS
$sql = "SELECT id FROM my_table WHERE username='$safe_username'";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, SHOW THE ERROR
if (!$res)
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/> $sql <br/>";
    die($err);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS




// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num = mysql_num_rows($res);
$fmt = number_format($num);
if (!$num)
{
    echo "<br/>QUERY FOUND NO DATA: ";
    echo "<br/> $sql <br/>";
}
else
{
    echo "<br/>QUERY FOUND $fmt ROWS OF DATA ";
    echo "<br/> $sql <br/>";
}




// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row = mysql_fetch_assoc($res))
{
    // ROW BY ROW PROCESSING IS DONE HERE
    var_dump($row);
}




// ANOTHER WAY OF DETERMINING HOW MANY ROWS WE HAVE IN A TABLE
$sql = "SELECT COUNT(*) FROM my_table";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($err);
}
// GET THE RESULTS SET ROW IN AN ARRAY WITH A NUMERIC INDEX - POSITION ZERO IS THE COUNT
$row = mysql_fetch_array($res, MYSQL_NUM);
$num = $row[0];
$fmt = number_format($num);
echo "<br/>THERE ARE $fmt ROWS IN THE TABLE";




// MAKING AN INSERT QUERY AND TESTING THE RESULTS
$sql = "INSERT INTO my_table (username) VALUES ('$safe_username')";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/> $sql <br/>";
    die($err);
}

// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED - PER THE DB CONNECTION
$id  = mysql_insert_id($db_connection);
echo "<br/>YOU JUST INSERTED A RECORD WITH AUTO_INCREMENT ID = $id";

Open in new window

<?php // RAY_mysql_show_columns.php
error_reporting(E_ALL);
echo "<pre>"; // READABILITY


// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://us2.php.net/manual/en/ref.mysql.php
// MAN PAGE: http://us2.php.net/manual/en/mysql.installation.php


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "??"; // PROBABLY 'localhost' IS OK
$db_name = "??"; // GET THIS INFORMATION FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES


// CHOOSE A TABLE
$tbl = 'your_favorite_table';

// GET COMPLETE COLUMNS INFORMATION: MAN PAGE: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
$sql = "SHOW FULL COLUMNS FROM $tbl";
$res = mysql_query($sql) or die( mysql_error() );
while ($show_columns = mysql_fetch_assoc($res))
{
    $my_columns[]   = $show_columns;
}

// SHOW THE WORK PRODUCT
var_dump($my_columns);

Open in new window

HTH, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial