Reputation: 27
I'm a beginner in PHP so may be asking dumb questions. I researched on my question for couple of days prior to bugging you guys. I've two scenarios.
a) MySQL database has 3 fields. description
, weblink
and header
. "weblink" field stores weblinks in the database. Using PHP I'm trying to show weblinks on my webpage upon the user clicking the field "header".Its not working- webpage comes blank with the code below.
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo "<a href = $row['weblink'] > $row['Header']</a>";
echo "<br>";
echo "<hr>";
}
b) Scenario 2: Same example from above but trying to show the links from my database as a button.
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo $row['weblink'];
echo "<br>";
<a href = "$row['weblink']"<button>click me</button></a>;
echo "<hr>";
}
Please help.
Upvotes: 1
Views: 92
Reputation: 35
Check the below code:
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br><br>";
echo $row['weblink'];
echo "<br>";
echo '<a href = "'.$row['weblink'].'" class="add-button-style">click me</a>';
echo "<hr>";
}
Upvotes: 1
Reputation: 4275
The brackets in the echo are treated as a string not as an array key So use { } these brackets.Also your quotes are not proper. Use the code below
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo "<a href =' {$row['weblink']}' > {$row['Header']}</a>";
echo "<br>";
echo "<hr>";}
Scenario 2
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo $row['weblink'];
echo "<br>";
echo '<a href = "{$row['weblink']}"><button>click me</button></a>';
echo "<hr>";}
Hope this helps you
Upvotes: -1
Reputation: 36299
for Scenario 1, You should do something like this (note the braces):
echo "<a href='{$row['weblink']}'>{$row['Header']}</a>";
For scenario 2, you should do something like this (again note braces):
echo "<a href=\"{$row['weblink']}\"><button>click me</button></a>";
Remember that Braces work on variables that are within double quotes, and not apostrophes.
Upvotes: 1
Reputation: 173542
Consider the humble printf()
instead and apply proper output escaping:
while ($row = mysqli_fetch_array($query)) {
printf('%s<br><br><a href="%s">%s</a><br><hr>',
htmlspecialchars($row['description'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($row['weblink'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($row['Header'], ENT_QUOTES, 'UTF-8')
);
}
Upvotes: 1