[英]How do I get PHP variables from this MySQL query?
I am working on an Asset Database problem using PHP / MySQL. 我正在使用PHP / MySQL处理资产数据库问题。
In this script I would like to search my assets by an asset id and have it return all related fields. 在此脚本中,我想通过资产ID搜索我的资产,并使其返回所有相关字段。
First I query the database asset table and find the asset's type. 首先,我查询数据库资产表并找到资产的类型。 Then depending on the type I run 1 of 3 queries.
然后根据类型运行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;
}
?>
So far I am able to get asset.type into $type. 到目前为止,我已经能够将asset.type转换为$ type。 How would I go about getting the rest of the variables (laptop.model to $model, asset.notes to $notes and so on)?
我将如何获取其余的变量(laptop.model到$ model,asset.notes到$ notes等)?
Thank you. 谢谢。
$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
. 您可能还需要查看
mysql_fetch_array
, mysql_fetch_row
或mysql_fetch_object
。 Choose which best suits your needs. 选择最适合您的需求。
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. 这会将结果集放入$ type,而不是类型本身。
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.
}
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 $ia multidimensional array containing all of your query data and you could use it by doing the following 这将使$ ia多维数组包含所有查询数据,您可以通过执行以下操作来使用它
foreach ($i as $key => $value) {
echo $value['model'];
echo $value['serial'];
etc......
}
You may take a look at http://php.net/manual/en/function.mysql-query.php . 您可以看看http://php.net/manual/en/function.mysql-query.php 。
A mysql_query
returns a resource which contains the results of your query. mysql_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
。 row
then is an associative array containing the single fields. 那么
row
是一个包含单个字段的关联数组。
Hope this helps. 希望这可以帮助。
it must be just one table computers
它只能是一台台式
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. 每次您看到代码加倍时,就会知道您做错了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.