Jump to content

SQL statement - urgent help please


applebiz89

Recommended Posts

Hi, I have these tables

 


$sql_table1 = "CREATE TABLE header
(
headerId int NOT NULL AUTO_INCREMENT,
fileName varchar(255) NOT NULL,
PRIMARY KEY (headerId)
)";
if(mysql_query($sql_table1, $dbh))
{
print "table 1 created  <p>";
}


$sql_table2 = "CREATE TABLE vals
(
headerFk int NOT NULL,
word varchar(255) NOT NULL,
PRIMARY KEY (headerFk, word)
)";
if(mysql_query($sql_table2,$dbh))
{
print "table 2 created  <p>";
}

 

And for my search file I want to create an SQL statement that takes a word, searches the database and brings back the filename that, that word is held in. I did have just one table before but i decided to change the style and use two tables with a foreign and primrary key

 

This is the statement I have but doesnt work.

 


$query = "SELECT headerFk FROM vals WHERE word LIKE '%" .$var."%' order by headerId";
$result = mysql_query($query);

 

I can't think of how to bring back the filename from the other table by searching the vals table..

Help greatly appreciated

 

Apple,

Link to comment
Share on other sites

I believe you need to join the tables

<?php
$query = "SELECT header.fileName FROM header JOIN vals ON header.headerId = vals.headerFk WHERE vals.word LIKE '%" .$var."%' order by header.headerId";
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.