Gudy
Gudy

Reputation: 31

Can't get data from MySQL

I'm connected to phpmyadmin but I can not get any data from phpmyadmin. my php version is 7.2.9 , I made everything that I wanted in database but php can't show the data in site ( I'm using localhost ). here is the code:

    <?php
    $key = $_GET['key'];

    $terms = explode(" ", $key);
    $query = "SELECT * FORM search WHERE ";

    foreach ($terms as $each){
        $i++;
        if($i == 1){
            $query .= "keywords LIKE '%$each%' ";
        } else{
            $query .= "OR keywords LIKE '%$each%' ";
        }
        echo $query;
    }

    //connection
mysql_connect("localhost", "root", "");
mysql_select_db('search');
    $query = mysqli_query($query);
    $numrows = mysqli_num_rows($query);



    if($numrows > 0){
        while ($row = mysql_fetch_assoc($query)){
        $id = $row['id'];
        $title = $row['title'];
        $description = $row['description'];
        $keywords = $row['keywords'];
        $link = $row['link'];
        echo "<h2><a href='$link'>$title</h2></a>
        $description<br /><br />";
        }
        }
        else{ 
        echo "No results found for \"<b>$key</b>\""; }

    //disconnect
    mysql_close();

?>

Upvotes: 1

Views: 1071

Answers (4)

Padmanabhan
Padmanabhan

Reputation: 158

Here's the Full working Example TESTED at my end (Last night I wasn't at my work machine and couldn't test the code.Later I created a small db and Tested it. I had searched with the dummy key 'ram mary albert' in my example

    <?php
        $key = $_GET['key'];

        $terms = explode(" ", $key);

        $qu1 = "SELECT * FROM search WHERE ";
        $qu2 = "order  by id ASC";



        $conn=mysqli_connect("localhost", "root", "");

        mysqli_select_db($conn, 'search');

        for($i=0; $i< count($terms);  $i++){

          $query = $qu1 . " keywords LIKE '%$terms[$i]%'  "  .  $qu2;

          echo( $query . "<br>" );

          $resulter= mysqli_query($conn, $query);

          while($row = mysqli_fetch_array($resulter)){;

                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];    

                $EscLink='\'' . $link . '\'';

                echo ('<a href="javascript:void(0)" onClick="alert(' . 
                $EscLink . ')">' . $title .  '</a><br>' .  $description . 
                '<br /><br />');


           } // Close While
         } // Close for



         //disconnect
         mysqli_close($conn);

         ?>

See the SCREENSHOT of the result

Upvotes: 0

Padmanabhan
Padmanabhan

Reputation: 158

You are mixing up mysqli and mysql. I have edited your stuff. Please try. Don't forget to fix the portion marked [missing]

      <?php
       $key = $_GET['key'];

       $terms = explode(" ", $key);


      foreach ($terms as $each){
        $i++;
        if($i == 1){  
           $query .= "keywords LIKE '%$each%' ";
       } else{
          $query .= " description OR keywords LIKE '%$each%' ";
       }
         echo $query;
        }

      //connection
        $conn=mysqli_connect("localhost", "root", "");
        mysqli_select_db($conn, 'search');
       if($result= mysqli_query($conn, $query)){
          $numrows = mysqli_num_rows($result);
       }




        if($numrows > 0){

            while($row = mysqli_fetch_array($result)){
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];    
                //echo "<h2><a href='$link'>$title</h2></a>
                //$description<br /><br />";
                echo '<h2><a href="' . $link . '">' . $title . 
                '</h2></a>' .  $description . '<br /><br />';
              }
            }
             else{ 
              echo "No results found for \"<b>$key</b>\""; }

           //disconnect
            mysqli_close();

           ?>

Upvotes: 0

Anshu Kumar
Anshu Kumar

Reputation: 827

first of all mysql_connect() is not anymore available after php 5. Instead of using mysql use mysqli_connect(). Please how to make connection and query database using php7 here. https://www.w3schools.com/php/func_mysqli_fetch_row.asp

If still you have problem. Ask for help in comments.

   <?php

    $query = "SELECT * FORM search WHERE ";

    foreach ($terms as $each){
        $i++;
        if($i == 1){
            $query .= "keywords LIKE '%$each%' ";
        } else{
            $query .= "OR keywords LIKE '%$each%' ";
        }
        echo $query;
    }

    //connection
    $conn = mysqli_connect("localhost", "root", "","search");
    if(!$conn)die("Connection Error");
    $query = mysqli_query($conn,$query);
    if(!query)die("query error");
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){
        while ($row = mysqli_fetch_assoc($query)){
        $id = $row['id'];
        $title = $row['title'];
        $description = $row['description'];
        $keywords = $row['keywords'];
        $link = $row['link'];
        echo "<h2><a href='$link'>$title</h2></a>
        $description<br /><br />";
        }
        }
        else{ 
        echo "No results found for \"<b>$key</b>\""; 
        }

Upvotes: 0

Gobbin
Gobbin

Reputation: 530

You have a couple of mistakes in your PHP/HTML. I'm gonna sum them up here so you can take a look at them:

  • <h2><a href='$link'>$title</h2></a>$description<br /><br /> This is wrong HTML. Close your a tag inside the h2.

  • You are connecting to you database through mysql, but querying through mysqli. Connect to your database with mysqli. Mysql_ family of functions have been removed in PHP 7

  • You have a typo in your query. you have written FORM instead of FROM.

  • You are exploding your $_GET variable on spaces. But i doubt if a $_GET variable has any spaces to begin with... Check if this is true.

Upvotes: 1

Related Questions