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.

Keep getting errors

Discussion in 'PHP' started by Kurt Whittingham, Mar 16, 2012.

  1. #1
    Hello.

    im trying to use this code for a categories table for my website.
    i am getting this error
    Warning: mysql_db_query(): supplied argument is not a valid MySQL-Link resource in /home/motorbik/public_html/cat page.php on line 48

    this is the code

    <?php
        $categ = "Gear Reviews";
    
        // setup SQL statement
        $SQL = "SELECT * FROM Gear Reviews WHERE categ = '$categ'";
    
        // execute SQL statement
        $retid = mysql_db_query($db, $SQL, $cid);
    
        // check for errors
        if (!$retid) { echo( mysql_error()); }
        else {
    
            // display results
            echo ("<p><dt><b>$categ</b><br>\n");
            while ($row = mysql_fetch_array($retid)) {
                $url = $row["url"];
                $name = $row["name"];
                $description = $row["description"];
               
                echo ("<p><a href='$url'>$name</a></p>\n");
            }
            echo ("</dt></p>");
        }
    ?>    
    
    PHP:
     
    Kurt Whittingham, Mar 16, 2012 IP
  2. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #2
    what do you mean?
     
    Kurt Whittingham, Mar 16, 2012 IP
  3. Kavin Raja

    Kavin Raja Active Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    What's $cid ?

    Also, in the $SQL, if the table name is Gear Reviews, try enclosing it within quotes.
    Like
    $SQL = "SELECT * FROM 'Gear Reviews' WHERE categ = '$categ'";
    PHP:
     
    Kavin Raja, Mar 16, 2012 IP
  4. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #4
    okay well thanks for that, i have no errors now.

    but i do have another problem the page says No database selected

    <?php
        $categ = "Gear Reviews";
    
        // setup SQL statement
        $SQL = "SELECT * FROM 'Gear Reviews' WHERE categ = '$categ'";
    
        // execute SQL statement
        $retid = mysql_db_query($db, $SQL);
    
        // check for errors
        if (!$retid) { echo( mysql_error()); }
        else {
    
            // display results
            echo ("<p><dt><b>$categ</b><br>\n");
            while ($row = mysql_fetch_array($retid)) {
                $url = $row["url"];
                $name = $row["name"];
                $description = $row["description"];
               
                echo ("<p><a href='$url'>$name</a></p>\n");
            }
            echo ("</dt></p>");
        }
    ?>
    PHP:
     
    Kurt Whittingham, Mar 17, 2012 IP
  5. Haxalot

    Haxalot Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    @Kurt Wittingham, firstly ensure that you are connecting to your database before trying to query it. You can do this with the two functions mysql_connect() and mysql_select_db(). Also when querying you database, all that needs to be done is use the mysql_query() function, with one parameter, the query itself.

    Another thing is the use grave accents or back ticks to surround your table name should be used, not single quotes. However that should be your last resort. Choosing a better table name, or using an underscore between Gear Reviews would be more suitable.
     
    Haxalot, Mar 17, 2012 IP
  6. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #6
    would you be able to code what you mean in the second part of what you said please. im really new to php and dont quite understand
     
    Kurt Whittingham, Mar 17, 2012 IP
  7. Haxalot

    Haxalot Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #7
    So if you have any spaces or mysql command words in your table/column names, then you should use grave accents around the, like so:
    
    $SQL = "SELECT * FROM `Gear Reviews` WHERE categ = '$categ'";
    
    PHP:
    However as i said above, that should be a last resort. If you need to use them around your table/column names, then you're not picking them wisely enough...
     
    Haxalot, Mar 17, 2012 IP
  8. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #8
    More explicitly, accent grave and commas are used for encapsulating asyntactic strings so MySQL doesn't try to interpret them. For example, Imagine if you decided to decided to call your table DROP DATABASES (don't do this):
    SELECT * FROM DROP DATABASES WHERE TRUNCATE TABLE = 1;
    PHP:
    Big problems. ;)
     
    Alastair Gilfillan, Mar 17, 2012 IP
  9. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #9
    it still says no database selected.
     
    Kurt Whittingham, Mar 17, 2012 IP