简体   繁体   中英

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. 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. (there are currently 3).

2) comps , where I want to use those CompressID listings to know the valid Compressor #s. I'll then do a max() on those values so I can name the next compressor max()+1.

My code attempts...This gets me an error: "Notice: Array to string conversion in (pathname) on line 55", then "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...

$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 ).

echo "<p>".$row;

You can print the $row array by using print_r :

print_r($row);

You can also access different elements of the $row array (table columns) like this:

$row['column_name'];

For example, lets say your table consists of two columns: first_name and 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:

$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.

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'];

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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