Jump to content

Recommended Posts

I have a database with 4 tables

 

article, musicvideo, review, video

 

All of these have the title and data columns. I'm wanting to make a query that allows me to search those tables and columns with the supplied search parameter. I have already made the form, as follows:

 

       <input name="search_query" class="textbox" type="text" /> 
       <input name="search" class="searchbutton" value="Item Search" type="submit" /><br>
       <input type="radio" value="*" name="searchresult" id="titledata"> <label for="titledata" title="Search in both titles and content of all items">Title and Content</label> -
       <input type="radio" value="title" name="searchresult" id="title"> <label for="title" title="Search in titles of all items">Title only</label> -
       <input type="radio" value="data" name="searchresult" id="data"> <label for="data" title="Search in content of all items">Content only</label>

 

Added to the above, I'm also wanting to be able to choose whether to only search the title, data or both columns of the tables.

 

I know this is possible as I'd accomplished it before, but all of that data was lost due to a disk failure (on both my PC and the host's, what rotten luck!)

 

Anyone out there that can help?

Link to comment
https://forums.phpfreaks.com/topic/49482-searching-multiple-tables/
Share on other sites

I'm confused as to the syntax.

 

I have, as follows:

SELECT * FROM article, musicvideo WHERE musicvideo.* = '$search_query' OR article.* = '$search_query'

Where $search_query is the POST data from the form.

 

After which is:

   while ($row = mysql_fetch_array($result))
    {
     $title  = $row['title'];
     $author = $row['author'];
     $date   = $row['date'];
    }
?>

 

This, however, is generating a warning:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\foxserv\www\s.all.php on line 22

 

Obviously the syntax of my query is incorrect, I just cant figure out how to get it to work  ???

$sql = 'SELECT * FROM article, musicvideo WHERE musicvideo = ' . $search_query . ' OR article = ' . $search_query;
$result = mysql_query($sql) or die('ERROR! ' . mysql_error());

while ($row = mysql_fetch_array($result)) {
    $title  = $row['title'];
    $author = $row['author'];
    $date   = $row['date'];
}

 

Try that. If it doesn't work, try replacing 'mysql_fetch_array($result)' with 'mysql_fetch_assoc($result)' ;).

 

 

That generates:

 

ERROR! Unknown column 'musicvideo' in 'where clause'

 

So I added ".*"'s:

 

$sql = 'SELECT * FROM article, musicvideo WHERE musicvideo.* = ' . $search_query . ' OR article.* = ' . $search_query;

 

Now the error is:

 

ERROR! You have an error in your SQL syntax near '* = test OR article.* = test' at line 1

OK.

 

I've now, almost, cracked it:

 

SELECT * FROM article, musicvideo WHERE article.title OR article.data OR musicvideo.title OR musicvideo.author LIKE '%$search_query%'

 

Is there anyway of making it simpler? Perhaps using the wildcard to choose the tables in the WHERE clause.

$sql = 'SELECT * FROM article, musicvideo WHERE musicvideo.title = ' . $search_query . ' OR musicvideo.author = ' . $search_query . ' article.title = ' . $search_query OR article.data = ' . $search_query;
$result = mysql_query($sql) or die('ERROR! ' . mysql_error());

while ($row = mysql_fetch_array($result)) {
    $title  = $row['title'];
    $author = $row['author'];
    $date   = $row['date'];
}

 

:)?

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.