Jump to content

Trying to print output based on database result


very_new_user

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.