views:

87

answers:

6

I am working on an Asset Database problem using PHP / MySQL.

In this script I would like to search my assets by an asset id and have it return all related fields.

First I query the database asset table and find the asset's type. Then depending on the type I run 1 of 3 queries.

<?php

//make database connect
mysql_connect("localhost", "asset_db", "asset_db") or die(mysql_error());
mysql_select_db("asset_db") or die(mysql_error());

//get type of asset
$type = mysql_query("
SELECT asset.type
From asset
WHERE asset.id = 93120
")
or die(mysql_error());

switch ($type){
    case "Server":
        //do some stuff that involves a mysql query
        mysql_query("
        SELECT asset.id
        ,asset.company
        ,asset.location
        ,asset.purchase_date
        ,asset.purchase_order
        ,asset.value
        ,asset.type
        ,asset.notes
        ,server.manufacturer
        ,server.model
        ,server.serial_number
        ,server.esc
        ,server.user
        ,server.prev_user
        ,server.warranty
        FROM asset
        LEFT JOIN server
        ON server.id = asset.id
        WHERE asset.id = 93120
        ");
        break;
    case "Laptop":
        //do some stuff that involves a mysql query
        mysql_query("
        SELECT asset.id
        ,asset.company
        ,asset.location
        ,asset.purchase_date
        ,asset.purchase_order
        ,asset.value
        ,asset.type
        ,asset.notes
        ,laptop.manufacturer
        ,laptop.model
        ,laptop.serial_number
        ,laptop.esc
        ,laptop.user
        ,laptop.prev_user
        ,laptop.warranty
        FROM asset
        LEFT JOIN laptop
        ON laptop.id = asset.id
        WHERE asset.id = 93120
        ");
        break;  
    case "Desktop":
        //do some stuff that involves a mysql query
        mysql_query("
        SELECT asset.id
        ,asset.company
        ,asset.location
        ,asset.purchase_date
        ,asset.purchase_order
        ,asset.value
        ,asset.type
        ,asset.notes
        ,desktop.manufacturer
        ,desktop.model
        ,desktop.serial_number
        ,desktop.esc
        ,desktop.user
        ,desktop.prev_user
        ,desktop.warranty
        FROM asset
        LEFT JOIN desktop
        ON desktop.id = asset.id
        WHERE asset.id = 93120
        ");
        break;  
}

?>

So far I am able to get asset.type into $type. How would I go about getting the rest of the variables (laptop.model to $model, asset.notes to $notes and so on)?

Thank you.

+1  A: 

What you're doing will not work:

$type = mysql_query("
SELECT asset.type
From asset
WHERE asset.id = 93120
")

This puts a resultset into $type, not the type itself.

After you do that, you need to fetch a row, then fetch the field:

$result = mysql_query("
SELECT asset.type
From asset
WHERE asset.id = 93120
")
if($row = mysql_fetch_object($result)){
  $type = $row->type;

  // NOW you have the type in $type. Do something similar with the rest of the queries.
}
Seb
A: 

You may take a look at http://php.net/manual/en/function.mysql-query.php.

A mysql_query returns a resource which contains the results of your query. This resource can be read out the following way:

$sql_result = mysql_query( [here your stuff] );
while ($row = mysql_fetch_assoc($sql_result)){
  echo $row['model']; // prints the model
}

So, you have to loop through the result and traverse each row. row then is an associative array containing the single fields.

Hope this helps.

phimuemue
A: 
$sql = "YOUR QUERY";
$res = mysql_query($sql);

while($row = mysql_fetch_assoc($res))
{
    echo 'Start Record<br />';
    echo $row['type'].'<br />';
    echo $row['company'].'<br />';
    echo $row['location'].'<br />';
    echo 'End Record<br /> <br />';
}

Try that out to see what you get then you can use data as you wish;

You may also want to look at mysql_fetch_array, mysql_fetch_row or mysql_fetch_object. Choose which best suits your needs.

Lizard
+1  A: 

I guess you would want to do something like this:

$result = mysql_query($query);
$i = array();
while ($data = mysql_fetch_assoc($result)) {
 $i[] = $data;
}

This would make $i a multidimensional array containing all of your query data and you could use it by doing the following

foreach ($i as $key => $value) {
  echo $value['model'];
  echo $value['serial'];
  etc......
}
Andrew Phillips
The table names wouldnt be visible in the result set so laptop.model wouldn't be valid. echo $value['model'] would suffice.
Neil Aitken
Good point, I've fixed the typo.
Andrew Phillips
A: 

it must be just one table computers

so your code would be just:

<?php
//make database connect
mysql_connect("localhost", "asset_db", "asset_db") or die(mysql_error());
mysql_select_db("asset_db") or die(mysql_error());

$sql = "SELECT a.id, a.company, a.location, a.purchase_date, a.purchase_order,
               a.value, a.type, a.notes, c.manufacturer, c.model, 
               c.serial_number, c.esc, c.user, c.prev_user, c.warranty
        FROM asset a
        LEFT JOIN computers c
        ON c.id = a.id
        WHERE a.id = 93120";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
$data=array();
while($row = mysql_fetch_assoc($res)) $data[] = $row;
// now $data array contains all the rows returned
?>

Every time you see doubled code, y know you're doing something wrong.

Col. Shrapnel
I realize server, desktops, and laptops have the same variables. But I have them in separate tables mostly just for practice as I will soon be adding assets such as projectors, network appliances, monitors, and so on...
CT
@CT that's another matter. Been discussed many times. But database normalization should be first priority anyway.
Col. Shrapnel