SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to use PHP and mySQL to create a "jump" URL (as featured at http://www.webmasterbase.com/article.php?aid=368) to shorten long URLs.

    I have Apache, PHP 4.0, mySQL, and myODBC all running properly on thier own, but they won't run together. Here is the code that I am using for the jump.php page:

    ==============================================
    <?
    $db_name = "links";
    $table_name = "redirects";
    $pathstring = getenv("HTTP_HOST") == "localhost" ? getenv("PATH_INFO") : $PHP_SELF ;

    $id = $id;

    $connection = mysql_connect("localhost", "root", "mysql")
    or die("Couldn't connect.");

    $db = mysql_select_db($db_name, $connection) or die("Couldn't select database.");

    $sql = "SELECT id, address FROM $table_name";
    $sql .= " WHERE id = " . $id . ";";
    $result = mysql_query($sql,$connection) or die("Couldn't execute query.");

    while ($row = @mysql_fetch_array($result))
    {
    $id = $row["id"];
    $address = $row["address"];
    }

    print "<HTML><HEAD>\n";
    print "<SCRIPT language=\"JavaScript1.1\">\n";
    print "<!--\n";
    print "location.replace(\"".$address."\");\n";
    print "//-->\n";
    print "</SCRIPT>\n";
    print "<NOSCRIPT>\n";
    print "<META http-equiv=\"Refresh\" content=\"0; URL=".$address."\">\n";
    print "</NOSCRIPT>\n";
    print "</HEAD>\n";
    print "<BODY>\n";
    print "Click <A href=\"".$address."\">here</A> to continue.\n";
    print "</BODY>\n";
    print "</HTML>\n";
    ?>
    ==============================================

    One of the URLs I'm trying to shorten is http://localhost/caddx/images/hi-res...X-4/index.html by using jump.php. In the mySQL DB this URL has an ID of 1, so the link off of the homepage points to http://localhost/jump.php?id=1 which is where I get an error page that says nothing but "Couldn't execute query."

    Any ideas what could be causing this? I've uninstalled, re-installed, and configured Apache, mySQL, PHP 4.0, and myODBC three times, but still get the same results every time. I'm running this on a Win2K box that is being used for development and testing.

    Any help would be appreciated.

    Chad 3001
    ==============================================
    "Never leave a child in a shopping cart unattended."
    ==============================================

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Washington State
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your script replace or die("Couldn't execture query"); with or die(mysql_error()); and it'll give use a much more useful message.

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's with this?

    Code:
    $id = $id;
    Also, something's wrong with this line:

    Code:
    $sql .= " WHERE id = " . $id . ";";
    There's too many double quotes in there, try doing something like:

    Code:
    $result = mysql_query("SELECT id, address FROM $table_name WHERE id='$id'") or die (mysql_error());
    The best way to check for the correct query is to insert it in phpMyAdmin, with something like:

    SELECT id, address FROM redirects WHERE id="5"

  4. #4
    SitePoint Member
    Join Date
    Apr 2001
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, it's working now. Sitepoint workers may want to take a look at the URL to the article I listed above, because I copied the code exactly from there! Not good if they have someone writing a "how-to" article, and it has errors and doesn't work.

    Thanks again!
    Chad 3001
    ===========================
    "Never leave a child in a shopping cart unattended."
    ===========================

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, that's pretty sloppy code in that article - looks like something I would write

    1) From the database schema we can see that id is the primary key and therefor must be unique - so there can only be one record per id.

    CREATE TABLE redirects ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, address VARCHAR(200), label VARCHAR(60), );

    So there is no need to create a while block to loop through the result set as logically it can only contain one record. In fact it is more important that we check that the record set is not empty! Something like:
    PHP Code:
    if (mysql_num_rows($result) != 1) {
       
    // we have a problem - maybe log the $HTTP_REFERER
       // and set $address to some default value - the home page?

    2) Unecessary Circular Logic:

    a) $id = $id;
    b) $id = $row["id"]; (we already know that $id == $row["id"] from the where clause in the sql statement.

    3) My mySQL/PHP setup does not like having a semicolon at the end of the sql string when passed to a call to mysql_query() as is done with this code:

    $sql .= " WHERE id = " . $id . ";";

    which could just as easily be written as Robo did.

    4) I presume that the html that is outputted by the script could all be replace by the line:

    header("Location: $address");

    Someone please tell me if I am mistaken about this.

    However, here is what is good about the article.

    1) The code works (more or less).
    2) Keith got off his **** and wrote the code and the article for the benefit of the sitepoint community.
    3) Debugging someone elses code is character building and more often eductational.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •