provision Posted June 25, 2007 Share Posted June 25, 2007 Hey all, Im new to all this php stuff so bear with me. Ill just post my code rather then trying to explain myself i will just get confused. $query = 'SELECT COUNT(*) FROM performer WHERE performer_type = "Band"'; $result = run_sql( $query, true ); $print= mysql_fetch_assoc( $result ); print $print When I view the page it prints out "Array". Thanks Josh Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 25, 2007 Share Posted June 25, 2007 So what u want to get from ur query. Quote Link to comment Share on other sites More sharing options...
provision Posted June 25, 2007 Author Share Posted June 25, 2007 Ok, sorry I didnt explain myself enough, what I am doing is my site is a cultural events site and I want to be able to print out the amount of types of cultural events from the database examle. Band (2), Artist (5). The query works but when I go to print it it just puts "array" instead of the number. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 25, 2007 Share Posted June 25, 2007 $print= mysql_fetch_assoc( $result ); $print this is array so when you prin you will have the word array use print_r($print ); hope you know hoew to get array content Quote Link to comment Share on other sites More sharing options...
provision Posted June 25, 2007 Author Share Posted June 25, 2007 Ok, Im a real beginner at this im sorry. I dont know how to get array content im just kinda winging this site till i get the hang of it. When i use print_r($print ); it prints Array ( [COUNT(*)] => 2 ) I know it must be something easy to fix but...... I just dont know. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 25, 2007 Share Posted June 25, 2007 Ok, you want to print a specific row from db or want to print all the records. If that is the issue than u have to use while loop to loop ur records. please explain ur need clearly. Quote Link to comment Share on other sites More sharing options...
provision Posted June 25, 2007 Author Share Posted June 25, 2007 Ok sorry. I just want to print the number of a certain event/artist type. Say if in my database there is 5 bands and 13 artists and 4 anual events. I want to print in the nav bar something like Band (5), Artist (13), Anual Event (4). I only want to print the number of rows in the db with a certain value in the column. My query works the only problem I have is printing that number out. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 25, 2007 Share Posted June 25, 2007 try this bcoz u mention band in ur query so it will give out put for band. $query = 'SELECT COUNT(*) FROM performer WHERE performer_type = "Band"'; $result = run_sql( $query, true ); $print= mysql_num_rows( $result ); print $print Quote Link to comment Share on other sites More sharing options...
provision Posted June 25, 2007 Author Share Posted June 25, 2007 Well that prints a number but its only 1, It wont change to any other number. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 25, 2007 Share Posted June 25, 2007 This is bcoz of your query: WHERE performer_type = "Band" Still i am confused about ur q?. ok let me clear, tell me when u insert a record to that table so u just update the performer_type field or increment it. Like if u have 10 records for band when u insert new it goes to 11, OR it make a new row in db. Try this code for single row output. instead of this $print= mysql_num_rows( $result ); print $print try this: $print= mysql_fetch_object( $result ); $band=$print->performer_type; echo $band; The above code depends on your db table field. Quote Link to comment Share on other sites More sharing options...
provision Posted June 25, 2007 Author Share Posted June 25, 2007 When a band member creates a new account to the site a new row in the db is created with all their details including a colum that says they are a band. That code that you gave me then did nothing. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 25, 2007 Share Posted June 25, 2007 ok try this, yeh how many rows u have in db are u check it. $query = 'SELECT * FROM performer WHERE performer_type = "Band"'; $result = mysql_query($query); $print= mysql_num_rows($result); print $print Quote Link to comment Share on other sites More sharing options...
suma237 Posted June 25, 2007 Share Posted June 25, 2007 hey, query is not clear..are you trying to do this $query = 'SELECT * FROM performer WHERE performer_type = "Band"'; $result=mysql_query($query); while($row=mysql_fetch_array($result)){ echo $row['event']; } Quote Link to comment Share on other sites More sharing options...
drummer101 Posted June 25, 2007 Share Posted June 25, 2007 $query = 'SELECT * FROM performer WHERE performer_type = "Band"'; $result=mysql_query($query); while($row=mysql_fetch_array($result)){ echo $row[####']; } This is what you want, I just stopped by to see if this was resolved and I was beaten by one post #### = a column header in your database. Edit 2: while(); executes until it evaluates to FALSE, in your case, it's executing mysql_query($query); until there are no more rows that meet the criteria. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 25, 2007 Share Posted June 25, 2007 I want to print in the nav bar something like Band (5), Artist (13), Anual Event (4). I think he want to show the number of rows for band or something not showing results for it.. Quote Link to comment Share on other sites More sharing options...
drummer101 Posted June 25, 2007 Share Posted June 25, 2007 I want to print in the nav bar something like Band (5), Artist (13), Anual Event (4). I think he want to show the number of rows for band or something not showing results for it.. $artist = "SELECT * FROM `performer` WHERE performer_type = 'Artist'"; $artist_result = mysql_query($artist) or die ("Query Failed" . mysql_error()); $artist_numrows = mysql_num_rows($artist_result); $band = "SELECT * FROM `performer` WHERE performer_type = 'Band'"; $band_result = mysql_query($band) or die ("Query Failed" . mysql_error()); $band_numrows = mysql_num_rows($band_result); $event = "SELECT * FROM `performer` WHERE performer_type = 'Event'"; $event_result = mysql_query($event) or die ("Query Failed" . mysql_error()); $event_numrows = mysql_num_rows($event_result); echo "Artist(" . $artist_numrows . ")"; echo "Band(" . $band_numrows . ")"; echo "Annual Event(" . $event_numrows . ")"; Hope that gets you started in the right direction. or die ("Query Failed" . mysql_error()) This will let you know if your query is incorrect. If it comes back with any errors just post and let me know and I'll try and help you some more. Quote Link to comment Share on other sites More sharing options...
drummer101 Posted June 26, 2007 Share Posted June 26, 2007 $band = "SELECT * FROM `performer` WHERE performer_type = 'Band'"; $band_result = mysql_query($band) or die ("Query Failed" . mysql_error()); $band_numrows = mysql_num_rows($band_result); echo "Bands(" . $band_result . ")"; I'm not exactly sure as to your database or form structure, so I'm going to take back my above code and let you use the band query as a template. Best of luck! And happy coding. Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 <?php $query = 'SELECT COUNT(*) AS total FROM performer WHERE performer_type = "Band"'; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); print $row['total']; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.