我正在使用PHP / MySQL处理资产数据库问题。
在此脚本中,我想通过资产ID搜索我的资产,并让它返回所有相关字段。
首先,我查询数据库资产表并找到资产的类型。然后根据类型我运行3个查询中的1个。
<?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;
}
?>
到目前为止,我可以将asset.type转换为$ type。我将如何获得其余变量(laptop.model为$ model,asset.notes为$ notes等)?
谢谢。
答案 0 :(得分:2)
$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 />';
}
尝试一下,看看你得到了什么,然后你可以随意使用数据;
您可能还想查看mysql_fetch_array
,mysql_fetch_row
或mysql_fetch_object
。选择最适合您需求的产品。
答案 1 :(得分:1)
你正在做的事情不起作用:
$type = mysql_query("
SELECT asset.type
From asset
WHERE asset.id = 93120
")
这会将结果集放入$ type,而不是类型本身。
执行此操作后,您需要获取一行,然后获取字段:
$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.
}
答案 2 :(得分:1)
我想你会想做这样的事情:
$result = mysql_query($query);
$i = array();
while ($data = mysql_fetch_assoc($result)) {
$i[] = $data;
}
这会使$ i成为包含所有查询数据的多维数组,您可以通过执行以下操作来使用它
foreach ($i as $key => $value) {
echo $value['model'];
echo $value['serial'];
etc......
}
答案 3 :(得分:0)
答案 4 :(得分:0)
您可以查看http://php.net/manual/en/function.mysql-query.php。
mysql_query
返回包含查询结果的资源。可以通过以下方式读取此资源:
$sql_result = mysql_query( [here your stuff] );
while ($row = mysql_fetch_assoc($sql_result)){
echo $row['model']; // prints the model
}
因此,您必须遍历结果并遍历每个row
。 row
然后是包含单个字段的关联数组。
希望这有帮助。
答案 5 :(得分:0)
它必须只是一个表computers
所以你的代码就是:
<?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
?>
每当你看到加倍的代码时,你就知道你做错了什么。