Fetch results according to title or id

Discussion in 'PHP' started by haradeep, Dec 14, 2008.

  1. #1
    This is my code to get classifieds in one page and limit them 10 in one page.

    I want to display the whole details of my classifieds in another page say 2.php. When a user clicks the link $title he directs to another page 2.php in that with respect to $title I want fetch the whole row from mysql database and display.

    PLEASE ANSWER THIS GIVE ME 2.PHP.....






    <?php
    $con=mysql_connect('localhost','root','password');
    if(!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    $selectdb=mysql_select_db("classifieds", $con);
    if(!$selectdb)
    {
    die('Could not connect database: ' . mysql_error());
    }
    $rows = mysql_query("SELECT * FROM inf");

    $result = mysql_query("SELECT * FROM inf LIMIT 10");

    $num_rows = mysql_num_rows($rows);

    echo "<h3>Recent Classifieds($num_rows)</h3>";

    while($row = mysql_fetch_array($result))
    {
    echo "<body bgcolor=\"#FFFAFA\">";
    echo "<a href=\"http://localhost/mana/2.php?value=$row[title]\">";

    echo ucfirst($row['title']);
    echo "</a>";
    echo "<br />";
    echo ucfirst($row['description']);
    echo "<br />";
    echo $row['city'];
    echo "<br />";
    echo "</body>";
    }
    mysql_close($con);
    ?>
     
    haradeep, Dec 14, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    first, change

    $rows = mysql_query("SELECT * FROM inf");

    to $rows = mysql_result(mysql_query("SELECT count(*) FROM inf"), 0);

    this is much faster! but you don't use $rows in the snippet gave above!

    and best way is indeed the way you wrote to send $row['title'] into the query.

    Best way is keep the same script but check for $_GET['value'] and insert this (if valid) into your query, for something like this.

    if (isSet($_GET['value']))
    {
    $result = mysql_query("SELECT * FROM inf WHERE title '" . mysql_real_escape_string($_GET['value']) . "' LIMIT 10");
    }
    else
    {
    $result = mysql_query("SELECT * FROM inf LIMIT 10");
    }

    please not the functions mysql_real_escape_string and the part where i'm using the quotes ' (')

    Hope this helps you!
     
    EricBruggema, Dec 17, 2008 IP
  3. haradeep

    haradeep Well-Known Member

    Messages:
    231
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Wow

    I changed
    $rows = mysql_query("SELECT * FROM inf");
    to $rows = mysql_result(mysql_query("SELECT count(*) FROM inf"), 0);

    Ya its fast as u said.

    yes it helped me a lot

    Thank u for ur reply

    coming to The useragent detect project

    Can u explain details about it
     
    haradeep, Dec 17, 2008 IP
  4. haradeep

    haradeep Well-Known Member

    Messages:
    231
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Wow

    I changed
    $rows = mysql_query("SELECT * FROM inf");
    to $rows = mysql_result(mysql_query("SELECT count(*) FROM inf"), 0);

    Ya its fast as u said.

    yes it helped me a lot

    Thank u for ur reply

    coming to The useragent detect project

    Can u explain details about it
     
    haradeep, Dec 17, 2008 IP