1

I'm a PHP and MySQL newbie trying to write a simple query against a table consisting of 2 columns: one with a text string and the second with a date string. The code below returns the first column (the text string) but not the second date string:

<?php
$link = mysql_connect('localhost', 'myuser', 'mypassword');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('archive')or die("cannot select db");
$string = $_POST['keywords'];
$search_query = "SELECT text, date_written FROM archive_table WHERE text LIKE '%$string%'";
$result = mysql_query($search_query,$link);
$rows = mysql_num_rows($result);
if ($rows == 0) {
    echo "sorry, I haven&#8217;t written about ".$string." yet.";
    }
    $count = 0;
    while ($count < $rows) {
        echo mysql_result($result, $count);
        echo "\n\n";
        $count = $count + 1;
    }

mysql_close($link);

?>

I've tried the following code to replace "echo mysql_result($result, $count);", but it returns nothing at all:

echo mysql_result($result['text'], $result['date_written'], $count);

I'm hoping it's a fairly simple syntax blunder that is easily fixed. Thanks in advance!

2 Answers 2

1
  1. Don't use the mysql_* functions, as they're deprecated and not safe. Use either MySQLi or PDO

  2. You also need to fetch your results:

    $query = "blah"
    $result = mysql_query($query);
    
    while($row = mysql_fetch_assoc($result)) {
        echo "{$row['text']}, {$row['date_written]}";
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

I'm pretty sure this

echo mysql_result($result['haiku_text'], $result['date_written'], $count);

should be

echo mysql_result($result['text'], $result['date_written'], $count);

You have a prefix in front of the text field but it doesn't have one when you select it.

1 Comment

"haiku_text" was a typo. I've fixed it in my example above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.