1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

problem in php database query

Discussion in 'PHP' started by NothingLikeThis, Feb 4, 2012.

  1. #1
    i have a web page having a search bar in which user will enter years in number and will get results displayed on the same page but the code is not working properly
    here is the core rectify the mistake
    <?php
    $con=mysql_connect('localhost','admin','admin');
    //1 database connectivity
    //if(!$con) die('could not connected to database')
    mysql_select_db("dvd");
    $sql='select * from dvd';
    $result=mysql_query($sql);
    //3 retrive data from table where year is entered by user
    if(!$result) die('unable to fetch data');
    else
    echo $result;
    ?>
     
    NothingLikeThis, Feb 4, 2012 IP
  2. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #2
    Well, mysql_query() only sends a MySQL query and returns a resource on success (or false on error). You should retrieve your results with functions like mysql_fetch_assoc(), mysql_result(), mysql_fetch_row() etc to display them on your site.
     
    bogi, Feb 4, 2012 IP
  3. olui

    olui Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Please I recieve the error message below from my host review:Fatal error: require_once() [function.require]: Failed opening required 'include/config.php' (include_path='.:/usr/local/lib/php-5.2.17/lib/php') in /hermes/web06/b781/moo.oludirectory/PhpLD/init.php on line 112How do I address this??
     
    olui, Feb 4, 2012 IP
  4. NothingLikeThis

    NothingLikeThis Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    i didnt received fatal error but on line
    $result=mysql_query($sql);

    here .how to correct this error
    how can i make my code retrieve data from database when user searches for year ?
     
    NothingLikeThis, Feb 4, 2012 IP
  5. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #5
    I haven't talked about fatal error, just "false on error". The guy above just posted a question in the first thread he bumped into.

    If you want to retrieve data with certain years, you should change your query. Something like this:

    $sql = "SELECT * FROM dvd WHERE year = '1999'"; // You can change the year to $_POST['year] or $_GET['year']
    PHP:
    If you accept user submitted data, make sure to sanitize your inputs to prevent SQL injection.

    As I mentioned it in my first post mysql_query() only returns a resource on success, you should retrieve your results with functions like mysql_fetch_assoc(), mysql_result(), mysql_fetch_row(), mysql_fetch_array() etc to display them on your site.
     
    bogi, Feb 5, 2012 IP
  6. amaroks

    amaroks Peon

    Messages:
    242
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why don't you stop using this old and poor mysqli
    look at PDO , or use something like ezSQL

    Cheers
     
    amaroks, Feb 5, 2012 IP
  7. NothingLikeThis

    NothingLikeThis Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #7
    i know sql and working on my sql.my query was i was getting error on a line in the code and want solutions if possible .no other talks please
     
    NothingLikeThis, Feb 5, 2012 IP
  8. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #8
    What error message did you get?! It would be great if you could explain clearly what's your problem with your code...

    In your first code you try to echo a resource. You should fetch the results first. But it would be great to know what you want to echo out.
     
    bogi, Feb 5, 2012 IP
  9. NothingLikeThis

    NothingLikeThis Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #9
    actually i want to display data from database
     
    NothingLikeThis, Feb 10, 2012 IP
  10. lonelygrit

    lonelygrit Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    you can try
    
    <?php
    $con=mysql_connect('localhost','admin','admin');
    //1 database connectivity
    //if(!$con) die('could not connected to database')
    mysql_select_db("dvd");
    $sql='select * from dvd';
    $result=mysql_query($sql);
    //3 retrive data from table where year is entered by user
    while ($row = mysql_fetch_array($result)) {
       print_r($row);
    }
    ?> 
    
    
    PHP:
     
    lonelygrit, Feb 10, 2012 IP
  11. vruvishal

    vruvishal Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #11
    well there is not any issue in your query. but mysql_query() just return resource .
    You need to use mysql_fetch_object or mysql_fetch_assoc() to retrive result..
    $sql="select * from dvd";
    $rs =mysql_query($sql);
    while($row = mysql_fetch_assoc($rs))
    {
    echo $row['columname];
    }
     
    vruvishal, Feb 11, 2012 IP