Any PHP Experts?
Author
Discussion

olimain

Original Poster:

1,010 posts

167 months

Wednesday 20th December 2017
quotequote all
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();


Edited by olimain on Wednesday 20th December 14:12

andycambo

1,077 posts

206 months

Wednesday 20th December 2017
quotequote all
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);
}



Edited by andycambo on Wednesday 20th December 14:34

andycambo

1,077 posts

206 months

Wednesday 20th December 2017
quotequote all
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;


olimain

Original Poster:

1,010 posts

167 months

Wednesday 20th December 2017
quotequote all
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.

Edited by olimain on Wednesday 20th December 15:18

james_gt3rs

4,816 posts

223 months

andycambo

1,077 posts

206 months

Wednesday 20th December 2017
quotequote all
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.

Edited by olimain on Wednesday 20th December 15:18
Your query is returning an object, not an array so try (I think)...

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

droopsnoot

14,837 posts

274 months

Wednesday 20th December 2017
quotequote all
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



Edited by droopsnoot on Wednesday 20th December 19:26

olimain

Original Poster:

1,010 posts

167 months

Thursday 21st December 2017
quotequote all
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!

zippy3x

1,384 posts

299 months

Thursday 21st December 2017
quotequote all
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

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

.:ian:.

3,223 posts

235 months

Thursday 21st December 2017
quotequote all
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>";
}


olimain

Original Poster:

1,010 posts

167 months

Thursday 21st December 2017
quotequote all
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

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
Thanks - just ran this command and it returned exactly what I want:



Feels like progress. Anyone know how to get that on to a webpage?

olimain

Original Poster:

1,010 posts

167 months

Thursday 21st December 2017
quotequote all
.: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?



The bottom three boats should be on 0

Edited by olimain on Thursday 21st December 10:09

zippy3x

1,384 posts

299 months

Thursday 21st December 2017
quotequote all
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.

Edited by zippy3x on Thursday 21st December 10:25

feef

5,211 posts

215 months

Thursday 21st December 2017
quotequote all
.: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.

olimain

Original Poster:

1,010 posts

167 months

Thursday 21st December 2017
quotequote all
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 PHP

feef 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?

zippy3x

1,384 posts

299 months

Thursday 21st December 2017
quotequote all
see my above edit for reason ian's doesn't work.

does my query not work in php? I would have thought that it is just passing the query as a string to the SQL engine and should just work

.:ian:.

3,223 posts

235 months

Thursday 21st December 2017
quotequote all
Lack of QA tongue out

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


zippy3x

1,384 posts

299 months

Thursday 21st December 2017
quotequote all
My SQL is very rusty, but isn't that left join an inner join, and won't that mean the boats with no catches won't appear at all?

which may or may not be what the OP wants

.:ian:.

3,223 posts

235 months

Thursday 21st December 2017
quotequote all

olimain

Original Poster:

1,010 posts

167 months

Thursday 21st December 2017
quotequote all
.:ian:. said:
Lack of QA tongue out

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


beer

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