Jump to content

Printing a variable from a query


provision

Recommended Posts

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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'];

}

 

 

 

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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'];
    }
  }

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.