Having trouble pulling data from my database

Hi I’m very new to php and I’m having trouble pulling information from my database so it can be viewed on my index page. It’s actually going to be set up within a javascript accordion.

Here’s what I have, or one version of it anyway:

<?php
	include "connect.php";
	
		$pullMe = "SELECT * FROM indexevents where ID ='".$e."'";
		$result = mysql_query($pullMe) or die(mysql_error()) ;
		$indexE = mysql_fetch_array($result);	
		
		
		$eventLink = "<a href='http://".$indexE['site']."'>".$indexE['site']."</a>";
		
		if ($indexE['FB'] == 0){
			$faceB = "";
			}else{
		$faceB = "<a href = ".$indexE['FB']."><img src='../buttons/logo-facebook.png' /></a>";
			}
			
	  $accordion = "<div class='AccordionPanel'><div class='AccordionPanelTab'><span class='date'>".$indexE['date']."</span><span class='event'>".$indexE['name']."</span></div><div class='AccordionPanelContent'><p>".$indexE['description']."</p>".$eventLink." &nbsp;&nbsp;&nbsp;&nbsp;".$faceB."</div></div>";
		
		
		$featured = "<p>".$indexE['name']." ".$indexE['description']."</p>";
	?>

Then within the html I have this repeated a couple of different times for different boxes (I decided to only use one data base for my three small boxes).


<?php
	  
	  for($e=1;$e<5;$e++){
		  echo $accordion;
	  }
	  ?>

No matter how I test it, it doesn’t seem to call anything from the database. I think it might have something to do with the mysql_query or mysql_fetch_array, but can’t figure out what it is. I’m still pretty confused about why mysql_fetch_array works sometimes and doesn’t at others.

Thanks for any help:)

Kindly try below…


<?php
include "connect.php";

//where's your $e came from? $_GET or $_POST?
$pullMe = "SELECT * FROM indexevents where ID ='".$e."'";

$result = mysql_query($pullMe) or die(mysql_error()) ;

$accordion = '';
$eventLink = '';
$featured  = '';

while( $indexE = mysql_fetch_array($result) ) {
	$eventLink .= "<a href='http://".$indexE['site']."'>".$indexE['site']."</a>";
	
	if ($indexE['FB'] == 0) {
		$faceB = "";
	} else {
		$faceB = "<a href = ".$indexE['FB']."><img src='../buttons/logo-facebook.png' /></a>";
	}
	
	$accordion	.= "<div class='AccordionPanel'><div class='AccordionPanelTab'><span class='date'>".$indexE['date']."</span><span class='event'>".$indexE['name']."</span></div><div class='AccordionPanelContent'><p>".$indexE['description']."</p>".$eventLink." &nbsp;&nbsp;&nbsp;&nbsp;".$faceB."</div></div>";
	$featured	.= "<p>".$indexE['name']." ".$indexE['description']."</p>";
}

//somewhere in within same page

<?php

if ( strlen($accordion) > 0) {
	for($e=1;$e<5;$e++){
		echo $accordion;
	}
} else {
	echo "Accordion cannot be rendered.";
}

I’m kind of new at this too, but from what I can see, the problem is that mysql_fetch_array fetches only one row from the $result you get from mysql_query. You have to execute mysql_fetch_array once per row in $result.

Here’s an example from http://us2.php.net/manual/en/function.mysql-fetch-array.php


<?php
  mysql_connect("localhost", "mysql_user", "mysql_password");
  mysql_select_db("mydb");
  $result = mysql_query("SELECT id, name FROM mytable");
  while ($row = mysql_fetch_array($result)) {
    printf ("ID: %s  Name: %s", $row[0], $row["name"]);
  }
  mysql_free_result($result);
?> 

See how mysql_fetch_array executes until it returns FALSE, meaning no more records in $result?

Maybe that points you in the right direction. Good luck! :slight_smile: