SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I've got some info in a MySQL database...the name is "mycodingdb" and the table is called "programmers"...

    I'm trying to create a variable in the URL (IE: "programmers.php?id=2"), so that I can then later on in the page tell the database to take that variable ($id=2 in this case), go into the SQL database, and pull out the record with the same ID as that variable, and post the info accordingly...

    No matter what number I used after programmers.php?id= it simply displays the first record in the database...I tested it by just sticking the $id variable in the page and it came out just fine...displayed the same number in the URL...so the problem must lie in one of the SELECT commands I've got in there...

    Here's some of the code I'm using...and of course I've taken out my actual password and username and replaced them with USERNAME and PASSWORD.

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;?php

    $db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
    if (!$db) {
    echo( "&lt;p&gt;Unable to connect to the " .
    "database server at this time.&lt;/p&gt;" ); exit();}
    mysql_select_db("mycodingdb",$db);
    if (! @mysql_select_db("mycodingdb") ) {
    echo( "&lt;p&gt;Unable to locate the " .
    "database at this time.&lt;/p&gt;" ); exit();}


    $result = mysql_query("SELECT ID FROM programmers WHERE ID LIKE $ID",$db);
    if (!$result) {
    echo("&lt;P&gt;Error performing query: " .
    mysql_error() . "&lt;/p&gt;"); exit();}

    $sql = "SELECT * FROM programmers";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);

    $Name = $myrow["Name"];

    $Age = $myrow["Age"];

    $Experience = $myrow["Experience"];

    $Languages = $myrow["Languages"];

    $Comments = $myrow["Comments"];

    while ( $row = mysql_fetch_array($result) ) {

    printf("&lt;b&gt;$Name&lt;/b&gt; - $Age years old - $Experience experience&lt;br&gt;&lt;b&gt;Languages: &lt;/b&gt;$Languages&lt;br&gt;
    &lt;b&gt;Comments: &lt;/b&gt;$Comments&lt;br&gt;&lt;p&gt;\n", $myrow[1], $myrow[2], $myrow[3], $myrow[4]);

    echo "$ID";

    ?&gt;

    [/code]

    Any ideas on what's wrong? I don't get an error, it just only posts the first record in the DB...not the one corresponding the ID variable...this make any sense?

    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

    [This message has been edited by jonese (edited June 14, 2000).]

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh by the way, to achieve this code/result I used some of the code from Kevin's article series, a bit of customizing/toying around with it all, and the tutorial below found on WebMonkey:
    http://hotwired.lycos.com/webmonkey/...tutorial4.html

    I'm not sure how I ended up with this code really...I used some of the examples I saw but they didn't achieve what I was looking for specifically, so I messed around with them and started to get there, but hit the aforementioned roadblock...



    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Okay... Let's start by explaining why your code is doing what it's doing.

    In your code, you are sending two queries to MySQL. The first is the following:

    SELECT ID FROM programmers WHERE ID LIKE $ID

    This asks MySQL to list the ID's of all entries in the programmers table that have an ID identical to the value of $ID. Not especially useful... In fact, the only potential use I could see for this query would be to find out whether an entry with a particular ID exists or not, and there are much better ways of doing that.

    Besides checking if the query encountered an error or not, you never actually use the result of this query, so essentially it is useless.

    The second query you make is the following:

    SELECT * FROM programmers

    This asks MySQL to provide you with a complete list of all entries in the programmers table. This potentially lengthy list will of course begin with the first entry in the table.

    The next thing you do is grab the first row in this set of results:

    $myrow = mysql_fetch_array($result);

    So following this line, $myrow will contain the first entry in the programmers table. Next, you assign the values in this row to a set of PHP variables:

    $Name = $myrow["Name"];
    $Age = $myrow["Age"];
    $Experience = $myrow["Experience"];
    $Languages = $myrow["Languages"];
    $Comments = $myrow["Comments"];


    So the above variables will contain the details associated with the first entry in the programmers table. Next, you begin a while loop:

    while ( $row = mysql_fetch_array($result) ) {

    This defines a loop that will execute as many times as their are entries in the programmers table, as each time through the loop, the next row in the results of the above query will be assigned to the $row variable. Unfortunately, you never complete this loop with a closing } so this only happens once, and the second entry in the programmers table gets placed into the $row variable.

    printf("&lt;b&gt;$Name&lt;/b&gt; - $Age years old - $Experience experience&lt;br&gt;&lt;b&gt;Languages: &lt;/b&gt;$Languages&lt;br&gt;&lt;b&gt;Comments: &lt;/b&gt;$Comments&lt;br&gt;&lt;p&gt;\n", $myrow[1], $myrow[2], $myrow[3], $myrow[4]);

    Speaking basically (there's actually more to it), the PHP printf function takes the string provided as its first parameter and substitutes in the strings provided as its subsequent parameters wherever it finds "%s" in that first string, then outputs the resulting string. The string you pass doesn't contain "%s" anywhere, so the $myrow[1], $myrow[2], etc. values are never actually used.

    PHP does, however, substitute the values of any variables it sees in the string. As we established above, however, these variables ($Name, $Age, etc.) contain the values associated with the first entry in the programmers table, so those are the values that get displayed on the page.

    Now then, here's the code you were probably after (with copious comments to explain what's going on):

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;?php

    // Connect to and select the database
    $db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
    if (!$db) {
    echo( "&lt;p&gt;Unable to connect to the " .
    "database server at this time.&lt;/p&gt;" );
    exit();
    }
    mysql_select_db("mycodingdb",$db);
    if (! @mysql_select_db("mycodingdb") ) {
    echo( "&lt;p&gt;Unable to locate the " .
    "database at this time.&lt;/p&gt;" );
    exit();
    }

    // SQL query to retrieve all information
    // associated with any entries in the
    // database that have an ID equal to $id
    $sql = "SELECT * FROM programmers " .
    "WHERE ID=$id";

    // Perform the query
    $result = mysql_query($sql,$db);
    if (!$result) {
    echo( "&lt;P&gt;Error performing query: " .
    mysql_error() . "&lt;/P&gt;" );
    exit();
    }

    // Just in case there is more than one
    // entry with the given ID, loop through
    // "all of them" (there will usually only
    // be one) so that only the values from
    // the last one are retained.
    while ($row = mysql_fetch_array($result)) {
    $Name = $row["Name"];
    $Age = $row["Age"];
    $Experience = $row["Experience"];
    $Languages = $row["Languages"];
    $Comments = $row["Comments"];
    }

    // Print out the values
    echo("&lt;b&gt;$Name&lt;/b&gt; - $Age years old - " .
    "$Experience experience&lt;br&gt;" .
    "&lt;b&gt;Languages: &lt;/b&gt;$Languages&lt;br&gt;" .
    "&lt;b&gt;Comments: &lt;/b&gt;$Comments&lt;br&gt;&lt;p&gt;\n");
    ?&gt;
    [/code]

    That's it! Please ask if anything about the above is unclear.


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

    [This message has been edited by kyank (edited June 14, 2000).]

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kevin,

    Amazing! Works great! I knew I had more code than I needed.

    How can I thank you? Would SitePoint like some free ad time on my site when it opens? I definetly feel I owe you something...just let me know! You can either post here or contact me here: chris@mycoding.com

    Thanks again!


    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

  5. #5
    SitePoint Zealot
    Join Date
    May 2000
    Location
    Eugene, OR
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! Not only is Kevin there to lead us through a ten week series, he also gives personal attention to this students!

    I bet many of us will become quite competent in designing dynamic sites.

    Just wanted to say thanks,

    Adam

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adam,

    You're right...Kevin went way above the call of duty here! Aside from any advertising I can offer to try to thank Kevin and SitePoint, I'll do my best to learn from the code and become more knowledgeable towards these things so I can "give back" to this community in the future when others come along who are less experience, like I am now.



    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

  7. #7
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's nominate Kevin most-helpful-person-of-the-month and hold a party!

    P.S. I don't get depressed when there is no new piece of your series out like twtcommish does but I can't wait for the next. They're awesome!!


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
  •