Jump to content

Read text from database


fullyloaded

Recommended Posts

<?php

// Connect to your Database 
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); 
mysql_select_db("Database_Name") or die(mysql_error());

$query = ("SELECT * FROM tablename");
$resultset = mysql_query($query);

while($result = mysql_fetch_array($resultset)){
    if ($result["columnname"] == "mytexthere"){
        header("Location: index.php");
        break;
    }
}
?>

Your goal for any select query is to query for just the information that you want. You would never select all rows from a table (unless you wanted to display everything at once from your table) and then use php to loop through them trying to find if a value exists. Php is a relatively slow parsed, tokenized, and interpreted language. The database engine uses compiled code to perform searches and is significantly faster at finding and filtering information than using php code.

 

A generic answer to your question would be to add a WHERE clause to your query to match row(s) that have the "mytexthere" for a value. If you only need to know if a match was found, you can either use mysql's COUNT() function in the query or form a query not-using mysql's COUNT() function (in case you actually need to retrieve other column values form the table) and use mysql_num_rows to find how many row(s) are in the result set.

 

Also, if you only expect a query to return at most one row, such as for a something like a log in username/password check, you would not use a loop to retrieve the result form the query.

Archived

This topic is now archived and is closed to further replies.

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