0

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!!

  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/a/14110189/1723893). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – NullPoiиteя Jan 03 '13 at 06:55
  • use `array_dif` http://php.net/manual/en/function.array-diff.php – Netorica Jan 03 '13 at 06:56
  • print_r($row),its an array dear – Arun Killu Jan 03 '13 at 06:56
  • i think you can join two tables instead of comparing n all – Arun Killu Jan 03 '13 at 06:57

3 Answers3

0
Please change this line 

echo "<p>".$row;
to 
echo "<p>";
print_r($row);
THE ONLY ONE
  • 2,110
  • 1
  • 12
  • 6
0

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>';
}
Wojciech Zylinski
  • 1,995
  • 13
  • 19
0
$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'];

    }
}
DazBaldwin
  • 4,125
  • 3
  • 39
  • 43