Dynamic Link with php content from database doesn't appear

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NewBieBR
    New Member
    • May 2015
    • 5

    Dynamic Link with php content from database doesn't appear

    Hi everyone, i have been working on this for hours but i can't solve the problem myself. Hope someone can help me out.

    So i have a main page with has a search function
    Code:
    <?php 
    $search = $_GET['search'];
    $terms = explode(" ", $search);
    $query = "SELECT * FROM search WHERE ";
    foreach ($terms as $each){
    $i++;
    if ($i == 1) 
    $query .= "keywords LIKE '%$each%' ";
    else
    $query .= "OR keywords LIKE '%$each%' ";
    }
    
    $db = mysql_connect("a", "a", "a");
    mysql_select_db("a");
    mysql_query("SET NAMES UTF8", $db);
    $query = mysql_query($query);
    $numrows = mysql_num_rows($query);
    if ($numrows > 0) {
    while ($row = mysql_fetch_array($query)) {
    $id = $row['id'];
    $title = $row['title'];
    $description = $row['description'];
    $keywords = $row['keywords'];
    $link = $row['link'];
    
    echo "<div id='kq' ><a href='http://bytes.com/a/rs.php?id=$id'>$title</a></div></br >";
    }
    }
    else
    echo "<div id='kq1'> '$search'</div>";
    
    mysql_close ();
    ?>
    as you can see, every result has link rs.php?id=$id specific to its id. Until now it's fine.
    Then i create the rs.php

    Code:
    <?php
    $newid = (int) $_GET['id'];
    
    $servername = "a";
    $username = "a";
    $password = "a";
    
    $conn = new mysqli($servername, $username, $password);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    mysql_select_db("a", $db);
    
    $newid = (int) $_GET['id'];
    $result = mysql_query("SELECT * FROM 'search' WHERE id = 1", $db) ;
    $myrow = mysql_fetch_array($result);
    
    
    echo $myrow["title"];
    mysql_close();
    ?> <html> <head> <meta charset='utf-8'> <title>dd</title> </head> <body> <?php echo $myrow['title']; ?> </body> </html>
    When I click on the link of a result, no error found but the title doesn't appear. Ive been looking on this for hours, I found a same question on this website which was posted since 2007. Except the main page, our rs.php pages are exactly the same so i don't know mine doesn't work. Hope someone can help me out. Thank you...
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    On line 16, you're only looking for Id = 1

    Comment

    • NewBieBR
      New Member
      • May 2015
      • 5

      #3
      Yeah I forgot to change that back, I do that to test if there is any problem with the $newid=$_GET['id']; The original code:
      Code:
      $newid = (int) $_GET['id'];
      $result = mysql_query("SELECT * FROM 'search' WHERE id = $newid", $db) ;
      $myrow = mysql_fetch_array($result);
      
      echo $newid;
      echo sdqsd;
      echo $myrow["title"];
      mysql_close();
      ?>
      
      <html>
          <head>
              <meta charset='utf-8'>
              <title>dd</title>
          </head>
          
          <body>
              <?php echo $myrow['title']; ?>
          </body>
          
      </html>

      Still doesn't work though. I try
      Code:
      echo $newid
      this time, apparently it's fine. It got the right id. But the title doesn't appear.
      I also used
      Code:
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } 
      echo "Connected successfully";
      ?>
      The connection to database seems to be good too. hmmm Still working on this.

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Try dumping $myrow to see if it holds what you expect.

        Change:
        Code:
        <?php echo $myrow['title']; ?>
        To:
        Code:
        <?php print_r($myrow); ?>
        or:
        Code:
        <?php var_dump($myrow); ?>

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          You should check that the table has
          1) the id you're searching for
          2) a field named title
          3) the field is populated

          Comment

          • NewBieBR
            New Member
            • May 2015
            • 5

            #6
            yes It does. I checked that too. The id is int, auto_incr. the title is varchar limit 250 characters.the field is populated. hmmm.

            Comment

            • NewBieBR
              New Member
              • May 2015
              • 5

              #7
              With
              Code:
              <?php print_r($myrow['title']); ?>
              nothing change . With
              Code:
              <?php var_dump($myrow['title']); ?>
              i got the "NULL". hmm. I check id, title, ... everything was right but it still doesn't work ...

              Comment

              • NewBieBR
                New Member
                • May 2015
                • 5

                #8
                Thanks god, finally I work it out, thanks so much for your helps.
                the problem was
                Code:
                $servername = "a";
                $username = "a";
                $password = "a";
                 
                $conn = new mysqli($servername, $username, $password);
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                }
                i changed it to
                Code:
                $db = mysql_connect("a", "a", "a");
                and it worked ^_^. Im so happy, finally i can sleep well tonight.
                Once again, thanks a lot for your helps :D

                Comment

                • RonB
                  Recognized Expert Contributor
                  • Jun 2009
                  • 589

                  #9
                  You just took a step backwards. The mysql_ functions have been depreciated for some time and will removed from the next release.

                  You should be using mysqli_ or PDO.

                  Comment

                  Working...