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;
    }
}
?>

Link to comment
Share on other sites

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.

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.