PDA

View Full Version : select from 2 tables...


funnygiggler
05-01-2006, 12:32 PM
I know there are loads of examples out there but im a complete novice and really dont understand code unless someone explains it to me....

im trying to figure out how to do this


$query = "SELECT * FROM story WHERE storyID='" . $_REQUEST["storyID"] . "' AND SELECT * FROM section WHERE sectionID='1a';";


each table (story and section) has totally different columns.... what im trying to do is pull out information mainly from the story table but it also has to match the sectionID from the section table.... both tables have storyID.

if this doesnt make sense please ask or if u need more info... would really appreciate any help with this

Noodles24
05-01-2006, 12:40 PM
$query = "SELECT *
FROM story
INNER JOIN section ON section.sectionID = story.sectionID
WHERE storyID = '" . $_REQUEST['storyID'] ."'";


Or something, it gives you an idea.

See http://www.w3schools.com/sql/sql_join.asp for more information.

funnygiggler
05-01-2006, 12:54 PM
thank u ill give it a try

funnygiggler
05-01-2006, 02:12 PM
it doesnt seem to work.... here is how i am using the code



<td>
<?php
$query = "SELECT * FROM story
INNER JOIN section ON section.sectionID = story.sectionID
WHERE storyID = '" . $_REQUEST['storyID'] ."'";

//set variable to carry out operation
$result = mysql_query($query);
echo $query;
$num = mysql_num_rows($result);
if ($num > 0){
$array = mysql_fetch_assoc($result);
//close connection to db
//mysql_close($dbc);
$storyStage = 1;
?>
<a href="section.php?storyID=<?= $array["storyID"] ?>&sectionID=1a">view section</a>
<?
} else {
?>
<a href="sectionCreate.php?storyID=<?= $array["storyID"] ?>&sectionID=1a">add to story</a>
<? }?>
</td>


where it now returns an error message saying:

*****
SELECT * FROM story INNER JOIN section ON section.sectionID = story.sectionID WHERE storyID = '40'
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in e:\.... ....on line 79

*****

i dont understand...

GJay
05-01-2006, 02:32 PM
what version of MySQL do you have?

funnygiggler
05-01-2006, 02:46 PM
5 i think how do you find out?

funnygiggler
05-01-2006, 02:49 PM
well i changed the code a lil


<td>
<?php
$query = "SELECT story.storyID, section.storyID, section.sectionID
FROM story, section
WHERE story.storyID'" . $_REQUEST['storyID'] ."'=section.storyID'" . $_REQUEST['storyID'] ."'";

/*"SELECT * FROM story
INNER JOIN section ON section.sectionID = story.sectionID
WHERE storyID = '" . $_REQUEST['storyID'] ."'";
*/

//set variable to carry out operation
$result = mysql_query($query);
echo $query;
$num = mysql_num_rows($result);
if ($num > 0){
$array = mysql_fetch_assoc($result);
//close connection to db
//mysql_close($dbc);
$storyStage = 1;
?>
<a href="section.php?storyID=<?= $array["storyID"] ?>&sectionID=1a">view section</a>
<?
} else {
?>
<a href="sectionCreate.php?storyID=<?= $array["storyID"] ?>&sectionID=1a">add to story</a>
<? }?>
</td>


and i know get the error

******

SELECT story.storyID, section.storyID FROM story, section WHERE story.storyID'40'=section.storyID'40'
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

******
so it is reading from the database.... cuz its picked up the ID value

funnygiggler
05-01-2006, 02:52 PM
noo thats wrong i dont want to do thaat...

arghh i dont understand how to even explain what im trying to do!

guelphdad
05-01-2006, 04:19 PM
The error means your query is not returning any results.

More importantly, first, get your query to work correctly in mysql alone. make sure it is returning the results you want.

then edit it for use in php.