Jump to content

SQL query help


Go to solution Solved by extraspecialbitter,

Recommended Posts

I've written exactly one PHP program thus far - search against a text file using "preg_match".  It looked something like this (and it worked):

 

<pre>
<?php
$string = $_POST['keywords'];
$matches = 0;
$file = fopen("archive.txt", "r") or die("Cannot open file!\n");
while ($line = fgets($file, 1024)) {
    if (preg_match("/$string/", $line)) {
        echo $line;
        echo "\n";
        $matches = 1;
    }
}

if ($matches == 0) {
        echo "sorry, I haven’t written a haiku about ".$string." yet.";
}

fclose($file);
?>
</pre>

 

Now I need to match a string in a MySQL table and return the row as a match, and I'm struggling with the syntax.  Here's what I've come up with so far.  It produces a blank page.   :confused:

<pre>
<?php
$string = $_POST['keywords'];
// Connect to Database
mysql_connect("localhost", "user", "password") or die(mysql_error());
 mysql_select_db("archive") or die(mysql_error());
 $data = mysql_query("SELECT * FROM archive_text WHERE text LIKE $string")
 or die(mysql_error());
 if ($data = "") {
    echo "sorry, I haven’t written anything about ".$string." yet.";
 }
    echo $data;
    echo "\n";
 }

?>
</pre>

 

In both cases, the string is passed from a simple HTML search page to the PHP program via a post.  

 

Any pointers would be appreciated.

Link to comment
Share on other sites

Haku, use of indexes would explain that

 

mysql> EXPLAIN SELECT * FROM votes WHERE type = 12;
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | votes | ref  | idx2          | idx2 | 5       | const | 2090 | Using where |
+----+-------------+-------+------+---------------+------+---------+-------+------+-------------+


mysql> EXPLAIN SELECT * FROM votes WHERE type LIKE 12;
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | votes | ALL  | idx2          | NULL | NULL    | NULL | 183062 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
Link to comment
Share on other sites

Thank you for all of the replies!  I've fixed the comparison statement and the string in the SQL query but am still returning no results.  As Jessica mentioned, I suspect I need the mysql equivalent of the "preg_match" I used against a text file in my original example.

 

To reduce scrolling, here's the revised snippet:

 

<pre>
<?php
$string = $_POST['keywords'];
// Connect to Database
mysql_connect("localhost", "user", "password") or die(mysql_error());
 mysql_select_db("archive") or die(mysql_error());
 $data = mysql_query("SELECT * FROM archive_text WHERE text LIKE '$%string%'")
 or die(mysql_error());
 if ($data == "") {
    echo "sorry, I haven’t written anything about ".$string." yet.";
 }
    echo $data;
    echo "\n";
 }


?>
</pre>

Link to comment
Share on other sites

I did some additional troubleshooting and am now finally past the blank page problem.  Now, regardless of the query string, the only thing that gets returned to the browser is the mysterious "Resource id #3".

 

Here's the snippet as it exists now:

 

<pre>
<?php
error_reporting(-1);
$string = $_POST['keywords'];
// Connect to Database
mysql_connect("localhost", "user", "password") or die(mysql_error());
 mysql_select_db("archive") or die(mysql_error());
 $data = mysql_query("SELECT * FROM archive_test WHERE text LIKE '%$string%'");
 if (!$data) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
 }

 if ($data == "") {
    echo "sorry, I haven’t written anything about ".$string." yet.";
 }
 {
    echo $data;
    echo "\n";
 }

?>
</pre>
Link to comment
Share on other sites

  • Solution

My thanks to everyone for the help.  I finally have the search function working exactly the way I'd like.  Here's the final snippet:

 

<pre>
<?php
error_reporting(-1);
$string = $_POST['keywords'];
// Connect to Database
mysql_connect("localhost", "user", "password") or die(mysql_error());
 mysql_select_db("archive") or die(mysql_error());
 $query = mysql_query("SELECT * FROM archive_test WHERE text LIKE '%$string%'");
 if (!$query) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
 }

 while($row=mysql_fetch_row($query))
 {
    echo $row[0];
    echo "     $row[1]";
    echo "\n";
    echo "\n";
 }

?>
</pre>
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.