I've been working with a sample application from the book "PHP and MySQL Web Development" by Developer's Library and modifying it to meet my own requirements. Everything works fine up to the point that I try to retrieve elements from multiple database fields. If I try to get the data from a single field everything is fine.
Here is my code:
<?php
function db_connect() {
$result = new mysqli('localhost', username', 'password', 'database');
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}
function get_user_searches($username) {
$conn = db_connect();
$result = $conn->query("select description
from gennames
where username = '".$username."'");
if (!$result) {
return false;
}
$saved_searches = array();
for ($count = 1; $row = $result->fetch_row(); ++$count) {
$saved_searches[$count] = $row[0];
}
return $saved_searches;
}
function display_user_searches($saved_searches) {
global $bm_table;
$bm_table = true;
?>
<br />
<form name="bm_table" action="http://www.google.com/search" method="get" target="_blank">
<table width="300" cellpadding="2" cellspacing="0">
<?php
$color = "#cccccc";
echo "<tr bgcolor=\"".$color."\"><td><strong>Bookmark</strong></td>";
if ((is_array($saved_searches)) && (count($saved_searches) > 0)) {
foreach ($saved_searches as $s_search) {
if ($color == "#cccccc") {
$color = "#ffffff";
} else {
$color = "#cccccc";
}
echo '<tr bgcolor="'.$color.'"><td>'.$s_search.'</td></tr>';
}
echo "<tr><td><input type=\"submit\" value=\"Search Google\"></td></tr>";
} else {
echo "<tr><td>No bookmarks on record</td></tr>";
}
?>
</table>
</form>
if ($saved_searches = get_user_searches($_SESSION['valid_user'])) {
display_user_searches($saved_searches);
}
?>
The above code, as is, works as intended. My problem is when I try to retrieve more than one field (change query to SELECT description, searchterm ...) in the function get_user_searches and use it in the table of results. In the code above an html table is created that displays a list of web site descriptions. Also stored in the database table are columns for search phrases. I want to take those phrases, urlencode/stripslash them and then add them to the base url to search Google.
My problem is that can't figure out how to create the command to get the column data from the array that contains the query data.
Thanks in advance for any help.