Any PHP Experts?
Discussion
Tearing my hair out with this one - taken on something as a favour for a club I am on the committee for and struggling with what's probably a simple problem! Any programmers residing in the house?
I have 2 SQL tables and want to count the number of rows that a 'Boat Name' from one appears in the other and display a table with all the results. Just getting an error at the moment (Catchable fatal error: Object of class mysqli_result could not be converted to string in .... on line 28):
$conn = new mysqli($servername, $username, $password, $dbname);
// Set BoatName as variable from the table containing list of boats.
$query = "SELECT BoatName from SpeciesHuntBoats";
$boatname = $conn->query($query);
// Calculate number of rows that boat name appears in another table
$result = mysqli_query($conn, "SELECT * FROM SpeciesHunt WHERE `BoatName` = '$boatname'");
$num_rows = mysqli_num_rows($result);
// Display a table showing all boat names and the number of rows that name appears in
echo "<table><tr><th>Boat Name</th><th>Number of Species</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["BoatName"] . "</a></td><td>" . $row["$num_rows"]. "</td>/tr>";
}
echo "</table>";
$conn->close();
I have 2 SQL tables and want to count the number of rows that a 'Boat Name' from one appears in the other and display a table with all the results. Just getting an error at the moment (Catchable fatal error: Object of class mysqli_result could not be converted to string in .... on line 28):
$conn = new mysqli($servername, $username, $password, $dbname);
// Set BoatName as variable from the table containing list of boats.
$query = "SELECT BoatName from SpeciesHuntBoats";
$boatname = $conn->query($query);
// Calculate number of rows that boat name appears in another table
$result = mysqli_query($conn, "SELECT * FROM SpeciesHunt WHERE `BoatName` = '$boatname'");
$num_rows = mysqli_num_rows($result);
// Display a table showing all boat names and the number of rows that name appears in
echo "<table><tr><th>Boat Name</th><th>Number of Species</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["BoatName"] . "</a></td><td>" . $row["$num_rows"]. "</td>/tr>";
}
echo "</table>";
$conn->close();
Edited by olimain on Wednesday 20th December 14:12
I haven't done much PHP coding for a while but this is what I have come up with (following when you have the variable $boatName (conn being your database connection link)
$sql = "SELECT * FROM SpeciesHunt WHERE BoatName = '$boatname'";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
$num_rows = mysqli_num_rows($result);
echo "<table><tr><th>Boat Name</th><th>Number of Species</th></tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row["BoatName"] . "</td>";
echo "<td>" . $num_rows . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
$sql = "SELECT * FROM SpeciesHunt WHERE BoatName = '$boatname'";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
$num_rows = mysqli_num_rows($result);
echo "<table><tr><th>Boat Name</th><th>Number of Species</th></tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row["BoatName"] . "</td>";
echo "<td>" . $num_rows . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
Edited by andycambo on Wednesday 20th December 14:34
Sorry, if you're just getting the number of rows then a while loop shouldn't be necessary. You could cut a lot out if you want to do.
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatName;
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatName;
EDIT: Just seen your other message. So I've changed it to this:
$conn = new mysqli($servername, $username, $password, $dbname);
// Set BoatName as variable from the table containing list of boats.
$query = "SELECT BoatName from SpeciesHuntBoats";
$boatname = $conn->query($query)->fetch_object()[0]->BoatName;
// Calculate number of rows that boat name appears in another table
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatName;
$conn->close();
?>
And get:
Fatal error: Cannot use object of type stdClass as array in showboats4.php on line 24
It seems to be the way I'm generating $boatname that it doesn't like.
$conn = new mysqli($servername, $username, $password, $dbname);
// Set BoatName as variable from the table containing list of boats.
$query = "SELECT BoatName from SpeciesHuntBoats";
$boatname = $conn->query($query)->fetch_object()[0]->BoatName;
// Calculate number of rows that boat name appears in another table
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatName;
$conn->close();
?>
And get:
Fatal error: Cannot use object of type stdClass as array in showboats4.php on line 24
It seems to be the way I'm generating $boatname that it doesn't like.
Edited by olimain on Wednesday 20th December 15:18
olimain said:
EDIT: Just seen your other message. So I've changed it to this:
$conn = new mysqli($servername, $username, $password, $dbname);
// Set BoatName as variable from the table containing list of boats.
$query = "SELECT BoatName from SpeciesHuntBoats";
$boatname = $conn->query($query)->fetch_object()[0]->BoatName;
// Calculate number of rows that boat name appears in another table
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatName;
$conn->close();
?>
And get:
Fatal error: Cannot use object of type stdClass as array in showboats4.php on line 24
It seems to be the way I'm generating $boatname that it doesn't like.
Your query is returning an object, not an array so try (I think)...$conn = new mysqli($servername, $username, $password, $dbname);
// Set BoatName as variable from the table containing list of boats.
$query = "SELECT BoatName from SpeciesHuntBoats";
$boatname = $conn->query($query)->fetch_object()[0]->BoatName;
// Calculate number of rows that boat name appears in another table
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatName;
$conn->close();
?>
And get:
Fatal error: Cannot use object of type stdClass as array in showboats4.php on line 24
It seems to be the way I'm generating $boatname that it doesn't like.
Edited by olimain on Wednesday 20th December 15:18
$sql = "SELECT BoatName from SpeciesHuntBoats";
$result = mysqli_query($conn, $sql);
$boatname = $result{0}['BoatName'];
Sorry, it has been a little while since I've done PHP properly.
I suspect you have to call fetch() before you can use the result, but I use PDO, not mysqli.
How many values for boatname will there be? It seems that you are assuming only one. If there will be more than one, and you want the count for each boat, you could do something like this:
SELECT boatname, (select count(*) from specieshunt where boatname = specieshuntboats.boatname) as boatcount from specieshuntboats
How many values for boatname will there be? It seems that you are assuming only one. If there will be more than one, and you want the count for each boat, you could do something like this:
SELECT boatname, (select count(*) from specieshunt where boatname = specieshuntboats.boatname) as boatcount from specieshuntboats
Edited by droopsnoot on Wednesday 20th December 19:26
Thanks for the replies, entering day 4 of this battle with what I thought would be the simplest little script - the help is much appreciated. To answer a couple of things:
- Happy to to PDO, mysqli or any other flavour just to get the damn thing to work. It's only a very short/simple page so easily rewritten
- There will be multiple results, I ideally want a table to be produced with two columns - 'Boats' and 'Species Caught' with Boats just being a list of boatnames and 'Species Caught' being the number of rows that boat appears in in the 'SpeciesHunt' table.
It may be easier if I show you the entire php file as a whole, just in case I'm missing anything stupid at the top:
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$conn = new mysqli($servername, $username, $password, $dbname);
// Set choose boatname from list and set it as $boatname
$sql = "SELECT BoatName from SpeciesHuntBoats";
$result = mysqli_query($conn, $sql);
$boatname = $result{0}['BoatName'];
// Calculate number of rows that boat name appears in another table
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatname;
$conn->close();
?>
Thanks again, hoping for an early Christmas present!
- Happy to to PDO, mysqli or any other flavour just to get the damn thing to work. It's only a very short/simple page so easily rewritten
- There will be multiple results, I ideally want a table to be produced with two columns - 'Boats' and 'Species Caught' with Boats just being a list of boatnames and 'Species Caught' being the number of rows that boat appears in in the 'SpeciesHunt' table.
It may be easier if I show you the entire php file as a whole, just in case I'm missing anything stupid at the top:
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$conn = new mysqli($servername, $username, $password, $dbname);
// Set choose boatname from list and set it as $boatname
$sql = "SELECT BoatName from SpeciesHuntBoats";
$result = mysqli_query($conn, $sql);
$boatname = $result{0}['BoatName'];
// Calculate number of rows that boat name appears in another table
$result = $conn->query("SELECT COUNT(*) FROM SpeciesHunt WHERE BoatName = '$boatname'");
$row = $result->fetch_row();
echo $row[0] . " species in " . $boatname;
$conn->close();
?>
Thanks again, hoping for an early Christmas present!
Don't know about PHP, so can't help there, but you should be able to do your 2 SQL queries in one
something like
Haven't tested it, but you should be able to run this from you database environment
something like
Select boats.BoatName, (Select Count(*) From SpeciesHunt as species where species.BoatName = boats.BoatName) as SpeciesCaught From SpeciesHuntBoats as boats
Haven't tested it, but you should be able to run this from you database environment
Yeah single query FTW:
$query = "SELECT specieshuntboats.boatname, count(*) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc;";
$result = $conn->query($query);
while ($data = $result->fetch_assoc()) {
echo $data['boatname']." = ".$data['count']."<br>";
}
$query = "SELECT specieshuntboats.boatname, count(*) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc;";
$result = $conn->query($query);
while ($data = $result->fetch_assoc()) {
echo $data['boatname']." = ".$data['count']."<br>";
}
zippy3x said:
Don't know about PHP, so can't help there, but you should be able to do your 2 SQL queries in one
something like
Haven't tested it, but you should be able to run this from you database environment
Thanks - just ran this command and it returned exactly what I want:something like
Select boats.BoatName, (Select Count(*) From SpeciesHunt as species where species.BoatName = boats.BoatName) as SpeciesCaught From SpeciesHuntBoats as boats
Haven't tested it, but you should be able to run this from you database environment
Feels like progress. Anyone know how to get that on to a webpage?
.:ian:. said:
Yeah single query FTW:
$query = "SELECT specieshuntboats.boatname, count(*) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc;";
$result = $conn->query($query);
while ($data = $result->fetch_assoc()) {
echo $data['boatname']." = ".$data['count']."<br>";
}
THANK YOU! This is very very nearly working - the only thing I can see wrong is that boats who haven't yet caught anything come up as having caught 1?$query = "SELECT specieshuntboats.boatname, count(*) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc;";
$result = $conn->query($query);
while ($data = $result->fetch_assoc()) {
echo $data['boatname']." = ".$data['count']."<br>";
}
The bottom three boats should be on 0
Edited by olimain on Thursday 21st December 10:09
Does Ian's query return the correct data in you SQL environment?
Looking at it I don't think it will work, the outer join means that the row will appear even if there are no records in the second table (this will still be a row, hence the 1 count)
try my query in the php code, you can add the order by at the end.
Looking at it I don't think it will work, the outer join means that the row will appear even if there are no records in the second table (this will still be a row, hence the 1 count)
try my query in the php code, you can add the order by at the end.
Edited by zippy3x on Thursday 21st December 10:25
.:ian:. said:
Yeah single query FTW:
$query = "SELECT specieshuntboats.boatname, count(*) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc;";
$result = $conn->query($query);
while ($data = $result->fetch_assoc()) {
echo $data['boatname']." = ".$data['count']."<br>";
}
I'd add, instead of count(*), do count([whichever column is the primary key]) as that will be quicker, but you're not getting into tweaks.$query = "SELECT specieshuntboats.boatname, count(*) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc;";
$result = $conn->query($query);
while ($data = $result->fetch_assoc()) {
echo $data['boatname']." = ".$data['count']."<br>";
}
zippy3x said:
Does Ian's query return the correct data in you SQL environment?
Ah - good point - no it doesn't, yours does but I'm not overly sure how to convert your query to something that will work in PHPfeef said:
I'd add, instead of count(*), do count([whichever column is the primary key]) as that will be quicker, but you're not getting into tweaks.
Thanks, it's a yearly comp so the maximum number of records would probably only ever be 500 - hopefully few enough that it isn't an issue?.:ian:. said:
Lack of QA 
SELECT specieshuntboats.boatname, count(specieshunt.boatname) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc

SELECT specieshuntboats.boatname, count(specieshunt.boatname) as count from specieshuntboats left join specieshunt on specieshunt.boatname = specieshuntboats.boatname group by specieshuntboats.boatname order by count desc

Many pints owed to you both - cannot thank you enough - been tearing my hair out all week with that one (in between the bouts of abuse from the Stack Overflow site!).
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



