Catchamouse Catchamouse - 6 months ago 17
MySQL Question

mysql php getting variables

I want to get the content from mysql variables to use in php code, actually name (of the game), url (of the game) and alt (of the game). The image url and searching the database are working. I appreciate any help with coding. I don't know how to write the pointer to name, url and alt.

search_site.php

<link rel="shortcut icon" href="catchamouse3.png">

<link rel="stylesheet" type="text/css" href="homestyles2.css">

<link rel="stylesheet" type="text/css" href="submit.css">

<link rel="stylesheet" type="text/css" href="allflashgames(3).css">

<link rel="stylesheet" type="text/css" href="searchbar.css">

<link rel="stylesheet" type="text/css" href="styles2.css">

<?php
include('func.php');

if(isset($_POST['keywords'])){
$suffix = "";
$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
$errors = array();
if(search_results($keywords) === false){
$errors[] ='<h1>We didn\'t find anything for &quot;'.$keywords.'&quot;</h1>';
}
if(empty($errors)){
$results = search_results($keywords);
$results_num = count($results);
$suffix = ($results_num!=1)?'s':'';
echo '<h1>',$results_num,' item',$suffix,' For &quot;',$keywords,'&quot;</h1>';
foreach($results as $result){
echo '

<span class="overimage">

<a href="$game_url" target="_blank">

<span class="hoverimage">
<span class="hovertext1line-home">',$result['name'],'</span><img class="onlinegameimage-home" src="',

$result['image_url'],'" alt=',$result['alt'],'>
</span>

</a>

</span>

';
}
}
else{
foreach($errors as $error){
echo $error,'<br>';
}
}
}
?>


func.php

<?php
$con = mysql_connect('localhost','root','');
mysql_select_db("my_search_test",$con);

function search_results($keywords){
$returned_results = array();
$where ="";

$keywords = preg_split('/[\s]+/',$keywords);
$total_keywords = count($keywords);

foreach($keywords as $key=>$keyword){
$where .= "`keywords` LIKE '%$keyword%'";
if($key != ($total_keywords -1)){
$where .=" AND ";
}
}
$results = "SELECT name, image_url, game_url, alt FROM search_games WHERE $where";
$results_num = ($results =mysql_query($results))? mysql_num_rows($results):0;
if($results_num === 0){
return false;
}
else{
while($results_row = mysql_fetch_assoc($results)){
$returned_results[] = array(
'image_url' => $results_row['image_url']

);
}
return $returned_results;
}
}
?>

Answer

Add them to the returned_results[] array just like you did image_url

while ($results_row = mysql_fetch_assoc($results)) {
    $returned_results[] = array(
        'name' => $results_row['name'],
        'image_url' => $results_row['image_url'],
        'game_url' => $results_row['game_url'],
        'alt' => $results_row['alt']
    );
}

ADDED

Per my comment, here's a cleaner, less redundant way to write your logic:

if (isset($_POST['keywords'])) {
    // ... code
    $results = search_results($keywords);
    if ($results === false) {
        echo '<h1>We didn\'t find anything for &quot;'.$keywords.'&quot;</h1>';
    }
    else {
        // we have results.. add rest of your code, ie. foreach(), etc.
    }
}