473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

first even php code

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
7 1376
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

"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
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
"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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: R.Marquez | last post by:
I hope I don't bore you with this personal experience. But, I hope the details are helpful for other Python and/or Linux newbies, or for those thinking about becoming such. I have been using...
26
by: English Teacher | last post by:
Should I learn C first? Or can I go straight to C++ or C#? Thanks in advance.
3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
14
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user...
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
30
by: lovecreatesbea... | last post by:
K&R says the following in the preface to the first edition, "... the C compiler, and ... are written in C." I'm wondering, does it say even the first / original C compiler was written in C?
8
by: Olivier BESSON | last post by:
Hello, VB.NET 1.1 IIS 6 I'm developping a winform client app of a web service of mine. I used to set the IDE with "halt in debugger" on "common language runtime exceptions". Every time i...
2
by: Morgan Cheng | last post by:
In asynchronous model, BeginXXX method returns a IAsnycResult-derived object. IAsyncResult.AsyncWaitHandle will be signaled when the asynchronous job is complete; and BeginXXX method has a argument...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.