The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 4 of 4

Thread: Variables in a database

  1. #1

    Variables in a database

    Hello,
    Is it possible to connect to a database, gather variables, and then have another connection to a database using those variables? Is it possible to have variables in the sql query to SELECT information from a database as well as use variables in the database connection? I tried but it didn't work, can someone help?

    THanks!

  2. #2
    Quote Originally Posted by dreamerp
    Hello,
    Is it possible to connect to a database, gather variables, and then have another connection to a database using those variables? Is it possible to have variables in the sql query to SELECT information from a database as well as use variables in the database connection? I tried but it didn't work, can someone help?

    THanks!
    I'm not quite sure I understood the question, but using PHP variables in MySQL -commands go like this

    PHP Code:
    SELECT FROM users WEHRE id=$id 
    $is being a variable.

    When you connect to a database and get a st of values, you can read them into an array like this:

    PHP Code:
            $query="SELECT * FROM users WHERE id=$id";
            
    $result=mysql_query($query);
            
    $array mysql_fetch_row($result); 
    Now you have the results of a row in an array and you can use them by

    PHP Code:
    $id=$array[0]; 
    etc.

    If you need multiple rows, use for- or while-loops.

  3. #3

  4. #4
    Hey there, I get the feeling you want to create a query with an inner loop that runs another query. The inner query would then selects data from the db based on variables from the outer query. If that's correct then, yes, that's possible - I do it all the time. Here's a basic example:

    PHP Code:
    $query mysql_query ("SELECT * FROM USERS WHERE User_ID='$userID'");

    while (
    $row mysql_fetch_array($queryGoals,MYSQL_ASSOC)) {

    //create a variable
    $myUserName $row{'User_Name'};

    //print it to the screen
    Echo "My name is: ".$myUserName;

    //Inner Query.  Note the use of the $myUserName variable in the SQL
    $query2 mysql_query ("SELECT * FROM UserPets WHERE User_Name='$myUserName'");

       while (
    $row2 mysql_fetch_array($query2,MYSQL_ASSOC)) {

       
    $myPetsName $row2{'Pets_Name'};

       Echo 
    "My name is: ".$myPetsName;

       }

    This demonstrates how a query within a loop can use variables from the outer loop to run sub-queries. Strange example (conceptually it all works) but it's been a strange day.

    Good luck

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012