Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy PHP / MySQL variable problem

    Hi,

    having a real problem with a bit of code.

    I've passed 10 variables to a PHP script:

    $Answer1
    $Answer2
    ....
    $Answer10

    Each variable contains a code, which relates to a field called "code", ina record in a MySQL database. The other field in each record is called "body".

    I'm trying to query out the data in the "body" field, for each record that matches the data in one of the $Answer variables. So I've got the following code so far:

    for ($n = 0; $n < 10; $n++) {
    $answervar = "$answer$n";
    $sqlquery = "SELECT body FROM $table WHERE code = {$$answervar}";
    $result = mysql_query($sqlquery);
    while($row = mysql_fetch_object($result)) {
    print $row->body; }
    }

    So I'm trying to move through each variable, query the table, print the value, and move onto the next one....only for the code above I'm getting the following error:

    Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site102/web/arraytest.php on line 30

    I've tried fiddling with everything but I just cant get it to work...can anyone help?

    Thanks...

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    variables are case sensitive in PHP $Answer and $answer are 2 different variables

    if you ever have hassles with queries echo them out so you can see what in fact you are passing to the db...

    PHP Code:
    <?
    for ($n 1$n <= 5$n++) { 
    $answervar = ${"Answer".$n}; 
    echo 
    "SELECT body FROM $table WHERE code = '$answervar'"

    ?>
    (if code is an int drop the single quotes around $answervar)

    + use mysql_error() after your queries for better error messages...


    $result = mysql_query($sqlquery)or die(mysql_error());
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool - thanks, yeah, you were right, there was a case mismatch there...

    so up to


    <?
    for ($n = 1; $n <= 5; $n++) {
    $answervar = ${"Answer".$n};
    echo "SELECT body FROM $table WHERE code = '$answervar'";
    }
    ?>

    I'm getting a loop of correct select statements...now I can't seem to get the things to "Print"... :/

    for ($n = 1; $n <= 8; $n++) {
    $answervar = ${"answer".$n};
    $result = mysql_query("SELECT body FROM $table WHERE code = '$answervar'");
    while($row = mysql_fetch_object($result)) {
    print $row->body; }
    }
    }


    doesn't seem to work. NOW what am I doing wrong?


  •  

    Posting Permissions

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