简体   繁体   English

mysql_fetch_array使用php比较2个表之间的数据?

[英]mysql_fetch_array to compare data between 2 tables using php?

I'm attempting to utilize 2 mysql tables via php/mysql 2 get me a max value. 我试图通过php / mysql 2利用2个mysql表,使我得到一个最大值。 I'm assuming using an array is the correct way to do this, but I've been spending many hours and am missing something. 我假设使用数组是执行此操作的正确方法,但是我已经花费了很多时间,并且缺少了一些东西。

My tables are: 我的表是:

1) plantcomp , where I want to know all the CompressID listings that have a CustID of $CustID. 1) plantcomp ,在这里我想知道所有CustID为$ CustID的CompressID清单。 (there are currently 3). (目前有3个)。

2) comps , where I want to use those CompressID listings to know the valid Compressor #s. 2) comps ,我想使用那些CompressID清单来知道有效的Compressor #s。 I'll then do a max() on those values so I can name the next compressor max()+1. 然后,我将对这些值进行max(),以便命名下一个压缩器max()+ 1。

My code attempts...This gets me an error: "Notice: Array to string conversion in (pathname) on line 55", then "Array" 我的代码尝试...这使我得到一个错误:“注意:第55行的(路径名)中的数组到字符串的转换”,然后是“ Array”

//have the custid
echo $CustID;

//under table `plantcomp`, find matching compressid's.
$q55 = "SELECT * FROM `plantcomp` WHERE `CustID`='" . $CustID . "' ";

 // Run query
 $result55 = mysql_query($q55);

while($row = mysql_fetch_array($result55)){
echo "<p>".$row;

I also tried this, mysql_fetch_assoc, but it only gives me 2 of my 3 valid entries... 我也尝试过使用mysql_fetch_assoc,但这只给了我3个有效条目中的2个。

$get = mysql_query("SELECT CompressID FROM plantcomp WHERE CustID = '$CustID'");
$money = mysql_fetch_assoc($get);

while($money = mysql_fetch_assoc($get)){echo $money['CompressID'];}

Thank you in advance for your assistance!! 预先感谢您的协助!

Please change this line 

echo "<p>".$row;
to 
echo "<p>";
print_r($row);

The problem you have comes from the fact that you are mixing a string (< p >) with an array ( $row ). 您遇到的问题来自将字符串(< p >)与数组( $ row )混合的事实。

echo "<p>".$row;

You can print the $row array by using print_r : 您可以使用print_r打印$ row数组:

print_r($row);

You can also access different elements of the $row array (table columns) like this: 您还可以像这样访问$ row数组(表列)的不同元素:

$row['column_name'];

For example, lets say your table consists of two columns: first_name and last_name. 例如,假设您的表由两列组成:first_name和last_name。 You can print them like this: 您可以像这样打印它们:

echo '<p>' . $row['first_name'] . ' ' . $row['last_name'] . '</p>';

So, with that knowledge, we can print your CompressIDs: 因此,借助这些知识,我们可以打印您的CompressID:

$result55 = mysql_query("SELECT * FROM `plantcomp` WHERE `CustID`='" . $CustID . "'");

while ($row = mysql_fetch_assoc($result55))
{
    echo '<p>' . $row['CompressID'] . '</p>';
}
$CompressID = array(); //Initialising an array

$query = "SELECT * FROM `plantcomp` WHERE `CustID`='" . $CustID . "' ";
$result = mysql_query($query);

while($obj = mysql_fetch_assoc($result)){

    $CompressID = $obj['CompressID']; //Storing all the CompressID in an array
    echo $obj['CompressID']; // sanity check
}

First run the above query and compare result with db.If the result is not matching 1)There is some wrong data in db 2)Alter your query to get desired result. 首先运行上面的查询并将结果与​​db比较。如果结果不匹配1)db中有一些错误数据2)更改查询以获得所需的结果

If this is working then add rest of the code 如果这有效,则添加其余代码

if( count($CompressID) >0 ){

    $query = "SELECT max(CompressID) as maxCompressID FROM `comps` WHERE `CompressID` IN($CompressID)";
    $result = mysql_query($query);
    while($newObj = mysql_fetch_assoc($result){

     echo $newObj['maxCompressID'];

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM