Jump to content

[SOLVED] an array of data from two mysql tables


cleary1981

Recommended Posts

Hi,

 

Im having trouble trying to figure out where to start with this problem. I have two tables.

 

module ={module_id, module_name, subtype, height, width, depth, mod_desc, confirmed}

 

object ={object_id, module_name, object_name, xpos, ypos, proj_id}

 

Given a value for proj_id I have to check wether any records in my table contain this value. If there are records that contain this value I need to create an array of the following {object_name, xpos, ypos, height, width}

 

Can anyone help me in creating this array? Heres my code so far

<?php
require "config.php";

// $proj = $_POST['proj'];

// Get how many records contain proj_id = proj
$query = mysql_query("SELECT COUNT(*) FROM object WHERE proj_id = '4'");
$result = mysql_fetch_row($query) or die(mysql_error()); 
//echo "result: ".$result[0];
// if zero do nothing
if ($result != 0) {
    
}

?>

<?php
$proj_id = 4; //a get variable probably
$results = mysql_query("SELECT object_name, xpos, ypos, height, width FROM object WHERE proj_id=$proj_id") or die();
while($values = mysql_fetch_array($results)){
    echo $values['object_name'] . '<br />';
    echo $values['xpos'] . '<br />';
    echo $values['ypos'] . '<br />';
    echo $values['height'] . '<br />';
    echo $values['width'] . '<br /><br />';
}
?>

 

In your case there's no need to query two tables, as you aren't showing the project name from the project id. If so, modify your query to something like this:

<?php
$results = mysql_query("SELECT object_name, xpos, ypos, height, width, project_name FROM object obj
                                 INNER JOIN project proj ON obj.proj_id=proj.id 
                                 WHERE obj.proj_id=$proj_id");
//project_name and any other fields can be called like the previous code snippet
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.