1

I am not a backend dev but a front end, yet I am trying to create a simple php search, I have followed the tutorial on here and made the changes for the deprecated bits it had. I have a database with sub rows and columns and I am trying to get some of the data from them:

The first thing is I created the html

      <form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
      </form>

Then the php bit:

$con = mysqli_connect("localhost", "USER", "PSW", "DBNAME") or die("Error connecting to database: ".mysqli_error());
$query = $_GET['query']; 

$min_length = 3;

if(strlen($query) >= $min_length){

    $query = htmlspecialchars($query); 

    $query = mysqli_real_escape_string($con, $query);


    $raw_results = mysqli_query($con, "SELECT * FROM article
        WHERE (`title` LIKE '%".$query."%') ") or die(mysqli_error($con));

    if(mysqli_num_rows($raw_results) > 0){

        while($results = mysqli_fetch_array($raw_results, MYSQLI_ASSOC)){
            echo "<p><h3>".$results['page_id']."</h3>".$results['title']."</p><p>".$results['space']."</p>";
        }

    }
    else{
        echo "No results";
    }

}
else{
    echo "Minimum length is ".$min_length;
}

The above is fine and it gives me the article title and article id I have within my DB. Yet I need to get the space and the year. Attached is the DB structure:

enter image description here and enter image description here

By reading around and by looking at what my backend dev did back then, i can see this line somewhere else which should get on the right track if it ever helps.

$query = "SELECT A.id AS article_id, A.title AS title, C.name as space, A.page_id, T.year, T.month, T.id as time_id FROM article AS A INNER JOIN space AS S ON S.article_id = A.id INNER JOIN country AS C ON C.id = S.country_id INNER JOIN time AS T ON A.id = T.article_id WHERE (T.year BETWEEN ".$year_start." AND ".$year_stop.") GROUP BY A.title, C.name";
8
  • i cannot seem to locate the space column Commented Jan 10, 2018 at 17:06
  • @Akintunde007 see first image, the last open bit is space Commented Jan 10, 2018 at 17:06
  • @rob.m the column name in space table is original_name and not name right. And did you execute the query what is the result? Commented Jan 10, 2018 at 17:11
  • @Shyamala if i do echo "<p><h3>".$results['page_id']."</h3>".$results['title']."</p><p>".$results['original_name']."</p>"; i get no name for the space, no erros Commented Jan 10, 2018 at 17:13
  • @rob.m did u change c.name as c.original_name in the query not in php ? Commented Jan 10, 2018 at 17:18

1 Answer 1

1

Replace

SELECT * FROM article
    WHERE (`title` LIKE '%".$query."%')   

with this

SELECT s.original_name as location, a.title, a.page_id, t.year from space s,article a, time t where a.id = s.article_id and s.article_id = t.article_id and a.title LIKE '%".$query."%'

This query will help you display the required columns from php.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.