Two tables, two queries, one correct response, one incorrect response.

Discussion in 'PHP' started by Matt Ridge, Feb 14, 2012.

  1. #1

    http://kaboomlabs.com/testbed/box2.php?id=7
    As it shows with the first line fab1 shows #2, in the second line it shows 1, --None-- instead of 2, Andy Khal.

    What this code needs to do is this:

    The first line is fine, it is meant to show what is in the testbed table and echo what is in $fab1 which the first line does perfectly.
    The second line is where the problem resides, it is meant to reference testbed.fab1 and then look in table user, under column userid, and once it finds the correct number it is echos out the user column that is of said number in the userid column.

    I thought I could use joining tables, but I can't seem to get it to work. I only posted the code I know that works mostly..

    If anyone can figure out why join doesn't work, it be appreciated. I've done about as much as I can to figure this out and I'm lost.

    Or if someone can help me write a join that will work, it be more appreciated.



    
    <?php
    // Connect to the database.
    require_once('tb/connectvars.php');
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if (mysqli_connect_errno()) {
        die("MySQL failed to connect: " . mysqli_connect_error());
    }
    
    // Create the SQL query
    $testbed = "SELECT * FROM testbed";
    $user = "SELECT * FROM user";
    
    
    // Execute the SQL query and store the result set in
    // the $result variable.
    $testbed = mysqli_query($dbc, $testbed) or die("Failed to execute query on tesbed table: " . mysqli_error($dbc));
    $user = mysqli_query($dbc, $user) or die("Failed to execute query on user table: " . mysqli_error($dbc));
    
    // Read the results.
    $row = mysqli_fetch_assoc($testbed);
    if(!$row)
    {
      echo 'Query failed<br />';
    }
    else
    {
        echo "Query for Testbed Fabricator is : " . $row["fab1"] . "<br />";
    }
    $row = mysqli_fetch_assoc($user);
    if(!$row)
    {
      echo 'Query for Testbed Fabricator failed<br />';
    }
    else
    {
        echo "Query for User ID # is : " . $row["userid"], $row["user"] . "<br />";
    }
    
    
    // Free the result set.
    mysqli_free_result($testbed);
    mysqli_free_result($user);
    
    
    
    ?>
    Code (markup):
     
    Matt Ridge, Feb 14, 2012 IP
  2. Andre91

    Andre91 Peon

    Messages:
    197
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Dude, can't really understand your English, but from what I assume you're asking, try this:

    
    <?php
    // Connect to the database.
    require_once('tb/connectvars.php');
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if (mysqli_connect_errno()) {
    	die("MySQL failed to connect: " . mysqli_connect_error());
    }
    
    $testbed = "SELECT * FROM testbed";
    $testbed = mysqli_query($dbc, $testbed) or die("Failed to execute query on tesbed table: " . mysqli_error($dbc));
    
    while ($row = mysqli_fetch_assoc($testbed)){
    	if (!$row){
    		echo 'Query Failed<br/><br/>';
    	}else{
    		echo 'Query for Testbed Fabricator is : '.$row["fab1"].'<br />';
    		$fabricator = $row["fab1"];
    	}
    	
    	$user = "SELECT * FROM user WHERE userid='".$fabricator."'";
    	$user = mysqli_query($dbc, $user) or die("Failed to execute query on user table: " . mysqli_error($dbc));
    	$row = mysqli_fetch_assoc($user);
    	echo 'Query for User ID # is : '.$row["userid"].', '.$row["user"].'<br /><br />';
    }
    
    // Free the result set.
    mysqli_free_result($testbed);
    mysqli_free_result($user);
    
    ?>
    
    PHP:
     
    Andre91, Feb 14, 2012 IP
  3. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Sorry I've had so many people ask me to break down the requirements I've gone into auto mode.

    The script isn't complex, the problem was that each time I go into asking a question with something like this I tend to have to spend three to four posts attempting to explain exactly what I needed. I tried to be as explicit as I can be, but it seems that I confuse people more than not.


    The script works, thanks :)

    Only problem is now it shows all rows, even though it only needs to show one:

    It is meant to show the first line only.

    http://kaboomlabs.com/testbed/box2.php?id=7
     
    Matt Ridge, Feb 15, 2012 IP