very_new_user Posted September 18, 2007 Share Posted September 18, 2007 Hello, Please hang in there with me. I am very new to php. What I am trying to do is print out an existing text file based on what is in my database. I want to show/print the contents of text1.inc (which is located in my includes directory) if the database contains query shows it contains "yes" and I want to show/print the contents of text2.inc (which is located in my includes directory) if the database contains query shows it contains "no". Here is the script <? //enter your MySQL database host name, often it is not necessary to edit this line $db_host = "localhost"; //enter your MySQL database username $db_username = "comp_cttds"; //enter your MySQL database password $db_password = "123456"; //enter your MySQL database name $db_name = "comp_cttms"; $conn = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error()); $db = mysql_select_db($db_name, $conn) or die(mysql_error()); $query = "SELECT show_data, message FROM products"; $result = mysql_query($query); $no = 0; $yes = 1; if ($yes == 1) { print ("includes/text1.inc"); } else { print ("includes/text2.inc"); } ?> The result from the above is, no data shows at all (completely blank). I look forward any help possible. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/69817-trying-to-print-output-based-on-database-result/ Share on other sites More sharing options...
Fadion Posted September 18, 2007 Share Posted September 18, 2007 try this: <?php $query = "SELECT show_data, message FROM products"; $result = mysql_query($query); while($values = mysql_fetch_array($result)){ if($values['column'] == 'yes'){ //the yes code } else{ //the no code } } ?> What i suggested is a while loop to loop through all the returned rows, as your sql query likely will return more then 1 row. The print() thing on the include files wont work. U can use include to make them act as part of the code, but to print them u must use fopen() and fread(), file() or file_get_contents(). Look them up in the manual. Also if those files contain php or html code inside, ull need to use htmlentities() to display them as simple text. Quote Link to comment https://forums.phpfreaks.com/topic/69817-trying-to-print-output-based-on-database-result/#findComment-350738 Share on other sites More sharing options...
freakstyle Posted September 18, 2007 Share Posted September 18, 2007 Here's what i think you're missing... <?php $query = "SELECT show_data, message FROM products"; $result = mysql_query($query); // ref: http://us.php.net/manual/en/function.mysql-fetch-assoc.php while ($row = mysql_fetch_assoc($result)) { $show_data = $row["show_data"]; } if ($show_data == 1) { print ("includes/text1.inc"); } else { print ("includes/text2.inc"); } ?> hope this helps. good luck Quote Link to comment https://forums.phpfreaks.com/topic/69817-trying-to-print-output-based-on-database-result/#findComment-350744 Share on other sites More sharing options...
Fadion Posted September 19, 2007 Share Posted September 19, 2007 @freakstyle, your code will pass to $show_data only the last row's 'show_data' value, which actually doesnt make very practical sense. Quote Link to comment https://forums.phpfreaks.com/topic/69817-trying-to-print-output-based-on-database-result/#findComment-350758 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.