By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

first even php code

P: n/a
Hi gurus

This is my first ever, ever PHP script. I have called the page tt.php. Can
someone tell me if I have done it right? I have a My SQL database with a
table called messages with fields name and description.

Thank you.

- Nicolaas

<?php
function tt($vname){
include_once("connectDB.php");
$sql = "SELECT * FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
//Close the database connection
mysql_close();
return $message

}
?>
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
WindAndWaves wrote:
Hi gurus

This is my first ever, ever PHP script. I have called the page tt.php. Can
someone tell me if I have done it right? I have a My SQL database with a
table called messages with fields name and description.

Thank you.

- Nicolaas

<?php
function tt($vname){
include_once("connectDB.php");
$sql = "SELECT * FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
//Close the database connection
mysql_close();
return $message

}
?>


Looks ok to me syntax wise. That alone won't do anything though as you
have to have some code outside the function which will call the tt
function with some name as the argument.
Example:

tt('WindAndWaves');

Also, usually it's better to select the file names, and not select *.
In your case you have such a small table it won't hurt, but if you
select * and only use a couple fields, you're wasting memory by pulling
unneccesary fields. Like here you only use the Description field, so
doing "SELECT Description FROM message WHERE Name='$vname'" would be better.

Lastly, your include_once call should probably be outside the function's
scope, likewise you should move the mysql_close call outside the
function. Multiple calls to this function will likely not work because
the first call would close the connection to the database, and the later
calles would not reopen it and thus produce errors.

So, syntatically, it seems fine. Logically, needs some work.

<?php

include_once("connectDB.php"); // I assume this file does mysql_connect
// and mysql_select_db.

function tt($vname){
$sql = "SELECT Description FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
return $message

}

echo tt('WindAndWaves');
echo '<br />'.tt('kicken');

//Close the database connection
mysql_close();
?>
Jul 17 '05 #2

P: n/a

"kicken" <sl***@aoeex.com> wrote in message
news:MO********************@bresnan.com...
WindAndWaves wrote:
Hi gurus

This is my first ever, ever PHP script. I have called the page tt.php. Can someone tell me if I have done it right? I have a My SQL database with a table called messages with fields name and description.

Thank you.

- Nicolaas

<?php
function tt($vname){
include_once("connectDB.php");
$sql = "SELECT * FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
//Close the database connection
mysql_close();
return $message

}
?>
Looks ok to me syntax wise. That alone won't do anything though as you
have to have some code outside the function which will call the tt
function with some name as the argument.
Example:

tt('WindAndWaves');

Also, usually it's better to select the file names, and not select *.
In your case you have such a small table it won't hurt, but if you
select * and only use a couple fields, you're wasting memory by pulling
unneccesary fields. Like here you only use the Description field, so
doing "SELECT Description FROM message WHERE Name='$vname'" would be

better.
Lastly, your include_once call should probably be outside the function's
scope, likewise you should move the mysql_close call outside the
function. Multiple calls to this function will likely not work because
the first call would close the connection to the database, and the later
calles would not reopen it and thus produce errors.

So, syntatically, it seems fine. Logically, needs some work.

<?php

include_once("connectDB.php"); // I assume this file does mysql_connect
// and mysql_select_db.

function tt($vname){
$sql = "SELECT Description FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
return $message

}

echo tt('WindAndWaves');
echo '<br />'.tt('kicken');

//Close the database connection
mysql_close();
?>


Thank you for your reply. What I perhaps should have mentioned is that I
wanted to use it in another php file to add text bits and pieces. In that
way I can keep the code short in the other php files. I hope that makes
sense. The way I get it in the other php file would be as follows:

include_once("tt.php");

$msg = tt('kicken');

is this correct?

Thanks again

- Nicolaas


Jul 17 '05 #3

P: n/a
WindAndWaves wrote:
"kicken" <sl***@aoeex.com> wrote in message
news:MO********************@bresnan.com...
[snip]

Lastly, your include_once call should probably be outside the function's
scope, likewise you should move the mysql_close call outside the
function. Multiple calls to this function will likely not work because
the first call would close the connection to the database, and the later
calles would not reopen it and thus produce errors.

So, syntatically, it seems fine. Logically, needs some work.

<?php

include_once("connectDB.php"); // I assume this file does mysql_connect
// and mysql_select_db.

function tt($vname){
$sql = "SELECT Description FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
return $message

}

echo tt('WindAndWaves');
echo '<br />'.tt('kicken');

//Close the database connection
mysql_close();
?>

Thank you for your reply. What I perhaps should have mentioned is that I
wanted to use it in another php file to add text bits and pieces. In that
way I can keep the code short in the other php files. I hope that makes
sense. The way I get it in the other php file would be as follows:

include_once("tt.php");

$msg = tt('kicken');

is this correct?


That will work fine, however my comment about the including of
connectDB.php and the placement of mysql_close is still something that
you need to consider if you plan to call the tt function multiple times.

include_once('connectDB.php');
include_once('tt.php');
$msg = tt('kicken');
mysql_close();
Jul 17 '05 #4

P: n/a
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:ue*******************@news.xtra.co.nz...
Hi gurus

This is my first ever, ever PHP script. I have called the page tt.php. Can someone tell me if I have done it right? I have a My SQL database with a
table called messages with fields name and description.

Thank you.

- Nicolaas

<?php
function tt($vname){
include_once("connectDB.php");
$sql = "SELECT * FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
//Close the database connection
mysql_close();
return $message

}
?>


Using include/require statements in lieu of a function call is a bad
practice. Something shouldn't happen just because you include a file. The
following is better:

function tt($vname){
connectDB();
$sql = "SELECT * FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
return $message
}

Where connectDB() is a function defined earlier or in an include file. If
there's no connection, it makes one. Otherwise it does nothing.

Jul 17 '05 #5

P: n/a

"kicken" <sl***@aoeex.com> wrote in message
news:PP********************@bresnan.com...
WindAndWaves wrote:
"kicken" <sl***@aoeex.com> wrote in message
news:MO********************@bresnan.com...
[snip]

Lastly, your include_once call should probably be outside the function's
scope, likewise you should move the mysql_close call outside the
function. Multiple calls to this function will likely not work because
the first call would close the connection to the database, and the later
calles would not reopen it and thus produce errors.

So, syntatically, it seems fine. Logically, needs some work.

<?php

include_once("connectDB.php"); // I assume this file does mysql_connect
// and mysql_select_db.

function tt($vname){
$sql = "SELECT Description FROM messages WHERE Name='$vname'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$message = $myrow['Description'];
return $message

}

echo tt('WindAndWaves');
echo '<br />'.tt('kicken');

//Close the database connection
mysql_close();
?>

Thank you for your reply. What I perhaps should have mentioned is that I wanted to use it in another php file to add text bits and pieces. In that way I can keep the code short in the other php files. I hope that makes
sense. The way I get it in the other php file would be as follows:

include_once("tt.php");

$msg = tt('kicken');

is this correct?


That will work fine, however my comment about the including of
connectDB.php and the placement of mysql_close is still something that
you need to consider if you plan to call the tt function multiple times.

include_once('connectDB.php');
include_once('tt.php');
$msg = tt('kicken');
mysql_close();


I have tried to implement the tt function, but now, when I try to change the
header, anywhere in the SQL, i get the following error:

Cannot modify header information - headers already sent by (output started
at .... tt.php:24) in ...email.php on line 204

Can you explain me what I do wrong (remember I am a complete novice!)

Thank you

- Nicolaas
Jul 17 '05 #6

P: n/a
WindAndWaves wrote:

I have tried to implement the tt function, but now, when I try to change the
header, anywhere in the SQL, i get the following error:

Cannot modify header information - headers already sent by (output started
at .... tt.php:24) in ...email.php on line 204

Can you explain me what I do wrong (remember I am a complete novice!)

Thank you

- Nicolaas


Whenever you use any kind of function in PHP which sends data to the
browser (eg, echo, print, printf, readfile, ...) or have any data
outside of the <?php and ?> tags (including spaces, newlines, etc) then
you can no longer use any function which require modification to the
request headers (eg, header(), setcookie(), session_start()).

The reason for this is that the header of the request must come prior to
the body. Once you send data to the brower by some means, PHP much
compile the header and send it, then send your output.

What kind of headers are you trying to send, where are you trying to
send them from? Make sure there's no output prior to the call to
header. According to the error, your output began on line 24 of tt.php.
Not sure how long your tt function is now, but I'm thinking maybe you
just have something outside the ?> php tag. Perhaps an ending new line
or such?
Jul 17 '05 #7

P: n/a

"kicken" <sl***@aoeex.com> wrote in message
news:tq********************@bresnan.com...
WindAndWaves wrote:

I have tried to implement the tt function, but now, when I try to change the header, anywhere in the SQL, i get the following error:

Cannot modify header information - headers already sent by (output started at .... tt.php:24) in ...email.php on line 204

Can you explain me what I do wrong (remember I am a complete novice!)

Thank you

- Nicolaas


Whenever you use any kind of function in PHP which sends data to the
browser (eg, echo, print, printf, readfile, ...) or have any data
outside of the <?php and ?> tags (including spaces, newlines, etc) then
you can no longer use any function which require modification to the
request headers (eg, header(), setcookie(), session_start()).

The reason for this is that the header of the request must come prior to
the body. Once you send data to the brower by some means, PHP much
compile the header and send it, then send your output.

What kind of headers are you trying to send, where are you trying to
send them from? Make sure there's no output prior to the call to
header. According to the error, your output began on line 24 of tt.php.
Not sure how long your tt function is now, but I'm thinking maybe you
just have something outside the ?> php tag. Perhaps an ending new line
or such?


Hmm,

What happens is this
Main page::

<? ..... tt ......headerchange ....>
with the tt page called and included once

I changed it around to:

<? ....... header change .... tt ......?>

and now it works, but it leaves me pretty limited in terms of the use of tt,
as all my pages change the headers several time.

I also had some empty lines after the } character in my tt function, does
that matter?

TIA

- Nicolaas

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.