mysql php获取变量

时间:2016-05-30 16:07:23

标签: php mysql

我想从mysql变量中获取内容以在php代码中使用,实际上是(游戏的)名称,(游戏的)url和(游戏的)alt。图像URL和搜索数据库正在运行。我很感激编码方面的任何帮助。我不知道如何编写指向name,url和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;
    }
}
?>

2 个答案:

答案 0 :(得分:0)

将它们添加到returned_results[]数组,就像您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

根据我的评论,这是一种更清晰,更少冗余的编写逻辑的方法:

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.
    }
}

答案 1 :(得分:-2)

<?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;
    }
}
?>

您在此处两次触发search_results($ keywords)。首先在if(空($ errors))块中检查=== false和second。这是多余的。一次调用search_results($ keywords),存储结果,并在需要时使用结果。