fullyloaded Posted January 20, 2012 Share Posted January 20, 2012 hi i was wondering if anyone has any idea how to read text from a database and if that text = "mytexthere" then goto header("Location: index.php"); sorry i have no code on this post reason is all the code i have tried on my own is crap Quote Link to comment https://forums.phpfreaks.com/topic/255409-read-text-from-database/ Share on other sites More sharing options...
kney Posted January 20, 2012 Share Posted January 20, 2012 <?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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255409-read-text-from-database/#findComment-1309498 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2012 Share Posted January 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255409-read-text-from-database/#findComment-1309499 Share on other sites More sharing options...
fullyloaded Posted January 20, 2012 Author Share Posted January 20, 2012 thanks a lot guy for the code and info. Quote Link to comment https://forums.phpfreaks.com/topic/255409-read-text-from-database/#findComment-1309502 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.