Hey mensen,

Ik ben bezig met een referentie script voor mijn website. In de database zitten ~50 referenties. Het is de bedoeling dat het script 4 random referenties weergeeft.

mijn script:
<?php
$gettestimonials = mysql_query("SELECT name, testimonial FROM testimonials WHERE check = 1 ORDER BY RAND() limit 4");
$testimonials = mysql_fetch_assoc($gettestimonials);

$name1 = $testimonials['name'[0]]; //hier krijg ik een error
$testimonial1 = $testimonials['testimonial'[0]];
$name2 = $testimonials['name'[1]];
$testimonial2 = $testimonials['testimonial'[1]];
$name3 = $testimonials['name'[2]];
$testimonial3 = $testimonials['testimonial'[2]];
$name4 = $testimonials['name'[3]];
$testimonial4 = $testimonials['testimonial'[3]];
?>

<div class="column_one_half">
	<blockquote class="theme"><?php echo $testimonial1; ?></p><p class="blockquote_cite"><?php echo $name1; ?></p></blockquote>
</div>
<div class="column_one_half last">
	<blockquote class="theme"><?php echo $testimonial2; ?></p><p class="blockquote_cite"><?php echo $name2; ?></p></blockquote>
</div>
<div class="clearfix"></div>

<div class="column_one_half">
	<blockquote class="theme"><?php echo $testimonial3; ?></p><p class="blockquote_cite"><?php echo $name3; ?></p></blockquote>
</div>
<div class="column_one_half last">
	<blockquote class="theme"><?php echo $testimonial4; ?></p><p class="blockquote_cite"><?php echo $name4; ?></p></blockquote>
</div>
<div class="clearfix"></div>

Waarom krijg ik hier een error? En wat kan ik doen om het script toch werkend te maken?
(Ik ben heel slecht met MSQL)

Alvast bedankt!
echo $testimonials['name'];
- SanThe - op 20/01/2014 17:11:34

echo $testimonials['name'];


Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in D:\wamp\www\testimonials.php on line 11

Line 11:
$testimonials = mysql_fetch_assoc($gettestimonials);
Je query zal mislukt zijn.
Bouw foutafhandeling in om te zien wat er mis gaat.
CHECK is een reserved word in mysql. Niet gebruiken dus als kolom naam. Daarom mislukt je query en daarom krijg je die foutmelding. Als je correcte foutafhandeling inbouwt voor je queries kan je dit soort zaken zelf vinden.

mysql reserved words: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html
Dank u beiden. We zijn er bijna!
Ik krijg nu 4 dezelfde referenties. Hoe kan ik de tweede, derde of vierde (name + testimonial) uit het opgehaalde rows krijgen?
In een while() zetten.
- SanThe - op 20/01/2014 17:22:19

In een while() zetten.


Bedankt het heeft gewerkt! Erwin H ook bedankt!
Ik heb er even een odd/even systeem ingezet, omdat er verschil zit tussen de html regels.

Nieuwe script:
<?php
$gettestimonials = mysql_query("SELECT name, testimonial FROM testimonials WHERE approved = '1' ORDER by RAND() LIMIT 4");

$i = 0;
while($testimonial = mysql_fetch_array($gettestimonials))
{
if($i%2 == 0)
{

echo '<div class="column_one_half">';
echo '<blockquote class="theme">'.$testimonial["testimonial"].'</p><p class="blockquote_cite">'.$testimonial["name"].'</p></blockquote>';
echo '</div>';
}

else
{
echo '<div class="column_one_half last">';
echo '<blockquote class="theme">'.$testimonial["testimonial"].'</p><p class="blockquote_cite">'.$testimonial["name"].'</p></blockquote>';
echo '</div>';
echo '<div class="clearfix"></div>';
}
$i++;
}
?>

Reageren