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.

Small Help Needed Please :)

Discussion in 'PHP' started by calcalmx123, Jun 7, 2010.

Thread Status:
Not open for further replies.
  1. #1
    EDIT: FIXED NOW - Please close thread mods/admins :).

    Hey, I dont have a clue why this code is making an error:
    
    <?php
    //Connect to the database
    mysql_connect("localhost", "..._...", "...") or die(mysql_error());
    mysql_select_db("..._...") or die(mysql_error());
    
    //protection
    function p($string){
    	return htmlspecialchars($string);
    }
    
    //get the info
    $id = p($_GET['id']);
    $data = mysql_query("SELECT * FROM ... WHERE id='{$id}'");
    $data = mysql_fetch_assoc($data);
    ?>
    PHP:
    Error:
    Thanks for your help.
     
    Last edited: Jun 7, 2010
    calcalmx123, Jun 7, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You need to implement some error handling. This means that your query is not returning a valid resource/result.

    Replace:

    $data = mysql_query("SELECT * FROM ... WHERE id='{$id}'");

    With:

    if(!$data = mysql_query("SELECT * FROM ... WHERE id='{$id}'")) {
    die(mysql_error());
    }
     
    jestep, Jun 7, 2010 IP
    calcalmx123 likes this.
  3. RoscoeT

    RoscoeT Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You need to read this
    http://php.net/mysql_query

    And follow the example.

    Jestep is right, you need error handling. Does your query return anything?
     
    RoscoeT, Jun 7, 2010 IP
    calcalmx123 likes this.
  4. calcalmx123

    calcalmx123 Well-Known Member

    Messages:
    1,498
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    195
    #4
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '... WHERE id='1'' at line 1

    That's the error I get.
     
    calcalmx123, Jun 7, 2010 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Can you post the exact query that is being run? Assuming that " ... " is being replaced with your table's name, the query should work.
     
    jestep, Jun 7, 2010 IP
  6. calcalmx123

    calcalmx123 Well-Known Member

    Messages:
    1,498
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    195
    #6
    //get the info
    $id = p($_GET['id']);
    if(!$data = mysql_query("SELECT * FROM like WHERE id='{$id}'")) {
    die(mysql_error());
    }
    $data = mysql_fetch_assoc($data);

    yea the dots are replaced by the table called like.
     
    calcalmx123, Jun 7, 2010 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Try putting backticks around like `like`. Since like is a reserved work, is it probably causing an error.

    if(!$data = mysql_query("SELECT * FROM `like` WHERE id='{$id}'")) {
    die(mysql_error());
    }
     
    Last edited: Jun 7, 2010
    jestep, Jun 7, 2010 IP
  8. kidatum

    kidatum Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I recommend employing exception handling for your database work.

    Logic wise, like this:

    TRY block - will contain all your queries, including the connection query. when you're executing queries always check for the $variable receiving the resource or boolean. when you receive a boolean (FALSE) you will throw an exception which will terminate script immediately and go to Catch block.

    CATCH block - will catch your errors and it's up to you what you want to do with the message, wether to display the message or just print the actual mysql error to the browser.

    This approach is better because if one of your queries fail there is no need to follow with the rest of them and you don't need to terminate your script completely.
     
    kidatum, Jun 7, 2010 IP
    calcalmx123 likes this.
Thread Status:
Not open for further replies.