0

I'm running a while loop to print certain data from a table. Because the data for the table is stored in what will become a very large table, I've heard its a good idea to create a separate table that just stores the number of rows of relevant data for this table. I'm trying to implement this, but not having much luck, why isn't the below working?

$numberofrows=mysql_fetch_field(mysql_query("SELECT rows FROM rows WHERE guestlist_id = '$guestlist_id'")) or die();
echo $numberofrows;

The above gives the error: Catchable fatal error: Object of class stdClass could not be converted to string in FULLURL/html/scripts/addname.php on line 36

PS - this query works in phpMyAdmin, so I know the data is there. Thanks!

Ryan Brodie
  • 6,554
  • 8
  • 40
  • 57

4 Answers4

1

Your $numberofrows is an object, you can only output its properties, e.g.:

echo $numberofrows->name;

See: http://php.net/manual/en/function.mysql-fetch-field.php

Config
  • 1,672
  • 12
  • 12
0

Please read the PHP documentation.

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
  • 1
    Can you provide me some information abou what is your goal? Becouse your query with this PHP code doesn't make a sense. – Peter Kiss Mar 08 '12 at 19:10
  • Sure @PeterKiss -I'm building a guest list application that stores the created guest lists in a table called 'lists' and the names for these lists in a table called 'names'. The names added are printed on the guestlist submission page using a while loop. The loop runs – Ryan Brodie Mar 09 '12 at 21:00
  • ... according to a mysql_numrows query. To remove the need for this, I aim to make a table that contains the number of rows of each guest list which can then be used as the value for the while loop, rather than having to run this query, which in turn should speed things up. Any ideas? – Ryan Brodie Mar 09 '12 at 21:02
0

$guestlist_id is probably not a string. Make sure it is one before you run the query.

As a side note, you are not doing any error checking, which will make your code a nightmare to debug. Check out this question for advice on how to do proper error checking:

Reference: What is a perfect code sample using the MySQL extension?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

I've heard its a good idea to create a separate table

That's wrong idea.
Keep your data in a single table.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345