This is a discussion on Newbie include question within the PHP Language forums, part of the PHP Programming Forums category; I put this in my PHP and it works fine. <?php $db = mysql_connect("localhost", "root"); ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I put this in my PHP and it works fine.
<?php $db = mysql_connect("localhost", "root"); mysql_select_db("sites",$db); $result = mysql_query("SELECT * FROM link",$db); printf("First Name: %s<br>\n", mysql_result($result,0,"site_name")); printf("Last Name: %s<br>\n", mysql_result($result,0,"site_description")); printf("Address: %s<br>\n", mysql_result($result,0,"url")); printf("Position: %s<br>\n", mysql_result($result,0,"place_name")); ?> I want to extract the connection string and put it in another file so I make a file called config.ini.php that contains one line: $db = mysql_connect("localhost", "root"); And I revise the main code to look like this, and it barfs: <?php include 'config.ini.php'; mysql_select_db("sites",$db); $result = mysql_query("SELECT * FROM link",$db); printf("First Name: %s<br>\n", mysql_result($result,0,"site_name")); printf("Last Name: %s<br>\n", ysql_result($result,0,"site_description")); printf("Address: %s<br>\n", mysql_result($result,0,"url")); printf("Position: %s<br>\n", mysql_result($result,0,"place_name")); ?> Where did I go wrong? Whats the right way to put an include in PHP code? Thanks for your help. |
|
|||
|
Bruce W...1 wrote:
[...] > I want to extract the connection string and put it in another file so I make a > file called config.ini.php that contains one line: > $db = mysql_connect("localhost", "root"); [...] > Where did I go wrong? Whats the right way to put an include in PHP code? When you include something you get out of "php mode". In the config.ini.php there must be a "<?php ... ?>" to get into "php mode" again! So, make it three lines long ... or just one: <?php $db = mysql_connect("localhost", "root"); ?> as you prefer :-) > And I revise the main code to look like this, and it barfs: It won't barf again (or I don't expect it to :) -- I have a spam filter working. To mail me include "urkxvq" (with or without the quotes) in the subject line, or your mail will be ruthlessly discarded. |
|
|||
|
Pedro wrote:
> > When you include something you get out of "php mode". > In the config.ini.php there must be a "<?php ... ?>" to get into "php > mode" again! > > So, make it three lines long ... or just one: > <?php $db = mysql_connect("localhost", "root"); ?> > as you prefer :-) > > It won't barf again (or I don't expect it to :) > > -- > I have a spam filter working. > To mail me include "urkxvq" (with or without the quotes) > in the subject line, or your mail will be ruthlessly discarded. ================================================== === Nope, it barfed again with <?php $db = mysql_connect("localhost", "root"); ?> in the config.ini.php file. But thanks for trying. |
|
|||
|
"Bruce W...1" <bruce@noDirectEmail.com> wrote in message
news:3F7E31B7.3024B02C@noDirectEmail.com... > Pedro wrote: > > > > When you include something you get out of "php mode". > > In the config.ini.php there must be a "<?php ... ?>" to get into "php > > mode" again! > > > > So, make it three lines long ... or just one: > > <?php $db = mysql_connect("localhost", "root"); ?> > > as you prefer :-) > > > > It won't barf again (or I don't expect it to :) > > > > -- > > I have a spam filter working. > > To mail me include "urkxvq" (with or without the quotes) > > in the subject line, or your mail will be ruthlessly discarded. > > ================================================== === > > Nope, it barfed again with > <?php $db = mysql_connect("localhost", "root"); ?> > in the config.ini.php file. > > But thanks for trying. I do on my sites exactly what you are trying to do here (and more). In some cases my includes have includes. My entire site it run by dynamic case-based includes. So here are some things that look different from what I am doing. You need parenthesis in the include statement to surround the argument, and use double quotes... at least that it the way it currently looks like for my currently *operational* site. Like this: include("dbaccess.php"); And you also still need the php tags inside the included file... but I have not ever put them on the same line before as suggested by the last poster... will have to try that.... You are also accessing your data fields differently than the two ways I have done... I think you might like the look of what I do, as it is a little cleaner looking (maybe not): // *** RETRIEVE SITE DATA *** $usesql = "SELECT * FROM tblSite"; $qry = mysql_query ("$usesql"); if (0 <= mysql_num_rows ($qry)-1) { $datarow = mysql_fetch_object ($qry); $intSiteCreatorID = $datarow->CreatorUserID; $strSiteName = $datarow->SiteName; $strSitePath = $datarow->SitePath; } The "if" statement is only an "idiot check", and you may not need it. So you might use: *****config.ini.php***** <?php $db = mysql_connect("localhost", "root"); ?> And revise the main page code to look like this: <?php include("config.ini.php"); mysql_select_db("sites",$db); $result = mysql_query("SELECT * FROM link",$db); $datarow = mysql_fetch_object ($result); printf("First Name: %s<br>\n", $datarow->site_name); printf("Last Name: %s<br>\n", $datarow->site_description); // etcetera ?> Lastly, I noticed that your second "printf" line has a mis-spelling in it; you missed the "m" on mysql_result. HTH. ~Duane Phillips. |
|
|||
|
On 3-Oct-2003, "Duane Phillips" <askme@askme.askme> wrote: > "Bruce W...1" <bruce@noDirectEmail.com> wrote in message > news:3F7E31B7.3024B02C@noDirectEmail.com... > > Pedro wrote: > > > > > > When you include something you get out of "php mode". > > > In the config.ini.php there must be a "<?php ... ?>" to get into "php > > > mode" again! > > > > > > So, make it three lines long ... or just one: > > > <?php $db = mysql_connect("localhost", "root"); ?> > > > as you prefer :-) > > > > > > It won't barf again (or I don't expect it to :) > > > > > > -- > > > I have a spam filter working. > > > To mail me include "urkxvq" (with or without the quotes) > > > in the subject line, or your mail will be ruthlessly discarded. > > > > ================================================== === > > > > Nope, it barfed again with > > <?php $db = mysql_connect("localhost", "root"); ?> > > in the config.ini.php file. > > > > But thanks for trying. > > I do on my sites exactly what you are trying to do here (and more). In > some > cases my includes have includes. My entire site it run by dynamic > case-based includes. > > So here are some things that look different from what I am doing. > > You need parenthesis in the include statement to surround the argument, > and > use double quotes... at least that it the way it currently looks like for > my > currently *operational* site. Like this: > > include("dbaccess.php"); The ()s are not needed and the double quotes are only needed if you are doing variable substitution into the string. include 'dbaccess.php'; include "dbaccess.php"; include('dbaccess.php'); all work. However, you should consider using require_once() instead of include. > > And you also still need the php tags inside the included file... but I > have > not ever put them on the same line before as suggested by the last > poster... > will have to try that.... The tags are required. > > You are also accessing your data fields differently than the two ways I > have > done... I think you might like the look of what I do, as it is a little > cleaner looking (maybe not): > > // *** RETRIEVE SITE DATA *** > $usesql = "SELECT * FROM tblSite"; > $qry = mysql_query ("$usesql"); > if (0 <= mysql_num_rows ($qry)-1) { > $datarow = mysql_fetch_object ($qry); > $intSiteCreatorID = $datarow->CreatorUserID; > $strSiteName = $datarow->SiteName; > $strSitePath = $datarow->SitePath; > } > > The "if" statement is only an "idiot check", and you may not need it. The above is poor coding. You should be checking to see if the mysql_query failed with an 'or die("$qry failed because".mysql_error())' or something. The quotes in the mysql_query are unnecessary . The if is unnecessarily complex: if (mysql_num_rows($qry)>0) would work, as would if (mysql_num_rows($qry)) > > So you might use: > > *****config.ini.php***** > <?php > $db = mysql_connect("localhost", "root"); > ?> > > And revise the main page code to look like this: > > <?php > include("config.ini.php"); > mysql_select_db("sites",$db); > $result = mysql_query("SELECT * FROM link",$db); > $datarow = mysql_fetch_object ($result); > printf("First Name: %s<br>\n", $datarow->site_name); > printf("Last Name: %s<br>\n", $datarow->site_description); > // etcetera > ?> Again where's the error checking????? -- Tom Thackrey www.creative-light.com tom (at) creative (dash) light (dot) com do NOT send email to jamesbutler@willglen.net (it's reserved for spammers) |
|
|||
|
"Tom Thackrey" <use.signature@nospam.com> wrote in message
news:plrfb.316$XD3.49814767@newssvr21.news.prodigy .com... > > On 3-Oct-2003, "Duane Phillips" <askme@askme.askme> wrote: > > > "Bruce W...1" <bruce@noDirectEmail.com> wrote in message > > news:3F7E31B7.3024B02C@noDirectEmail.com... > > > Pedro wrote: > > > > > > > > When you include something you get out of "php mode". > > > > In the config.ini.php there must be a "<?php ... ?>" to get into "php > > > > mode" again! > > > > > > > > So, make it three lines long ... or just one: > > > > <?php $db = mysql_connect("localhost", "root"); ?> > > > > as you prefer :-) > > > > > > > > It won't barf again (or I don't expect it to :) > > > > > > > > -- > > > > I have a spam filter working. > > > > To mail me include "urkxvq" (with or without the quotes) > > > > in the subject line, or your mail will be ruthlessly discarded. > > > > > > ================================================== === > > > > > > Nope, it barfed again with > > > <?php $db = mysql_connect("localhost", "root"); ?> > > > in the config.ini.php file. > > > > > > But thanks for trying. > > > > I do on my sites exactly what you are trying to do here (and more). In > > some > > cases my includes have includes. My entire site it run by dynamic > > case-based includes. > > > > So here are some things that look different from what I am doing. > > > > You need parenthesis in the include statement to surround the argument, > > and > > use double quotes... at least that it the way it currently looks like for > > my > > currently *operational* site. Like this: > > > > include("dbaccess.php"); > > The ()s are not needed and the double quotes are only needed if you are > doing variable substitution into the string. > > include 'dbaccess.php'; > include "dbaccess.php"; > include('dbaccess.php'); > > all work. However, you should consider using require_once() instead of > include. > > > > > And you also still need the php tags inside the included file... but I > > have > > not ever put them on the same line before as suggested by the last > > poster... > > will have to try that.... > > The tags are required. > > > > > You are also accessing your data fields differently than the two ways I > > have > > done... I think you might like the look of what I do, as it is a little > > cleaner looking (maybe not): > > > > // *** RETRIEVE SITE DATA *** > > $usesql = "SELECT * FROM tblSite"; > > $qry = mysql_query ("$usesql"); > > if (0 <= mysql_num_rows ($qry)-1) { > > $datarow = mysql_fetch_object ($qry); > > $intSiteCreatorID = $datarow->CreatorUserID; > > $strSiteName = $datarow->SiteName; > > $strSitePath = $datarow->SitePath; > > } > > > > The "if" statement is only an "idiot check", and you may not need it. > > The above is poor coding. > You should be checking to see if the mysql_query failed with an 'or > die("$qry failed because".mysql_error())' or something. > The quotes in the mysql_query are unnecessary . > The if is unnecessarily complex: > if (mysql_num_rows($qry)>0) > would work, as would > if (mysql_num_rows($qry)) > > > > > So you might use: > > > > *****config.ini.php***** > > <?php > > $db = mysql_connect("localhost", "root"); > > ?> > > > > And revise the main page code to look like this: > > > > <?php > > include("config.ini.php"); > > mysql_select_db("sites",$db); > > $result = mysql_query("SELECT * FROM link",$db); > > $datarow = mysql_fetch_object ($result); > > printf("First Name: %s<br>\n", $datarow->site_name); > > printf("Last Name: %s<br>\n", $datarow->site_description); > > // etcetera > > ?> > > Again where's the error checking????? > > > -- > Tom Thackrey > www.creative-light.com > tom (at) creative (dash) light (dot) com > do NOT send email to jamesbutler@willglen.net (it's reserved for spammers) Examples only, my friend, of working code, from somebody who doesn't know *everything*... I used this snippet on purpose to keep it simple for the basic ideas, relative to the simplicity of the OP code. However, I concede on your Nit of the complexity of the if statement; this one has existed without notice until you pointed it out, but it wasn't really the point of the answer to the post, was it?... It is always so helpful when someone pops in to critique the posts of others without any focus on answering the original post, eh? ~Duane Phillips. |
|
|||
|
On 3-Oct-2003, "Duane Phillips" <askme@askme.askme> wrote: > Examples only, my friend, of working code, from somebody who doesn't know > *everything*... > > I used this snippet on purpose to keep it simple for the basic ideas, > relative to the simplicity of the OP code. However, I concede on your Nit > of the complexity of the if statement; this one has existed without notice > until you pointed it out, but it wasn't really the point of the answer to > the post, was it?... > > It is always so helpful when someone pops in to critique the posts of > others > without any focus on answering the original post, eh? The original post had been answered, I didn't feel the need to contribute to that. Your post included errors and omissions which I felt should be mentioned. I answer lots of posts here, I try to verify my answers and I usually get rebuked when I make an error. It's the way usenet works. Look at it this way, now you know something you didn't know before I responded. Incidentally if everyone would include error checking on their mysql functions, half the "what's wrong with..." posts here would go away. -- Tom Thackrey www.creative-light.com tom (at) creative (dash) light (dot) com do NOT send email to jamesbutler@willglen.net (it's reserved for spammers) |
|
|||
|
"Tom Thackrey" <use.signature@nospam.com> wrote in message
news:nftfb.11450$gd1.8083@newssvr25.news.prodigy.c om... > > On 3-Oct-2003, "Duane Phillips" <askme@askme.askme> wrote: > > > Examples only, my friend, of working code, from somebody who doesn't know > > *everything*... > > > > I used this snippet on purpose to keep it simple for the basic ideas, > > relative to the simplicity of the OP code. However, I concede on your Nit > > of the complexity of the if statement; this one has existed without notice > > until you pointed it out, but it wasn't really the point of the answer to > > the post, was it?... > > > > It is always so helpful when someone pops in to critique the posts of > > others > > without any focus on answering the original post, eh? > > The original post had been answered, I didn't feel the need to contribute to > that. Exactly where was it answered? Would that be the part that you cropped from my first post (the part you did not critique: the missing "m"). > > Your post included errors and omissions which I felt should be mentioned. Intentional omissions, and a few unneeded additions... if there were errors in my code, my code would not work... which it does. Hence I wrote, "...at least that is the way it currently looks like for my currently *operational* site." I did not say, "This is the only way it will work.". Additionally, the OP wasn't asking about error trapping, so I cropped most of it out (less the if block). Functionally, my post was a direct attempt to help the person, and answered his question, and then some. > I answer lots of posts here, I try to verify my answers and I usually get > rebuked when I make an error. It's the way usenet works. Look at it this > way, now you know something you didn't know before I responded. Thank you Tom. Maybe you can work on tact. Try something like: "You may want to consider using error trapping, like..." You actually started out well, with: "The ()s are not needed and the double quotes are only needed if you are doing variable substitution into the string. include 'dbaccess.php'; include "dbaccess.php"; include('dbaccess.php'); all work. However, you should consider using require_once() instead of include." This was fairly even-handed. But then you ran aground with: "The above is poor coding. You should be..." "Again where's the error checking?????" It isn't a requirement of usenet to treat someone like an idiot. > Incidentally if everyone would include error checking on their mysql > functions, half the "what's wrong with..." posts here would go away. > -- > Tom Thackrey > www.creative-light.com > tom (at) creative (dash) light (dot) com > do NOT send email to jamesbutler@willglen.net (it's reserved for spammers) G'day! ~Duane Phillips. |
|
|||
|
On 4-Oct-2003, "Duane Phillips" <askme@askme.askme> wrote: > Exactly where was it answered? Would that be the part that you cropped > from > my first post (the part you did not critique: the missing "m"). Actually another poster (Pedro) had the answer at least to the primary error. > > Your post included errors and omissions which I felt should be > > mentioned. > > Intentional omissions, and a few unneeded additions... if there were > errors > in my code, my code would not work... which it does. Hence I wrote, > "...at > least that is the way it currently looks like for my currently > *operational* > site." I did not say, "This is the only way it will work.". Additionally, > the OP wasn't asking about error trapping, so I cropped most of it out > (less > the if block). The error was in your statement that ()s are required for an include, they aren't; Error checking in 'operational code' is not an option, at best it's reckless at worst irresponsible, you should at least have mentioned that it was "cropped out". > Thank you Tom. Maybe you can work on tact. Try something like: > > "You may want to consider using error trapping, like..." > This was fairly even-handed. But then you ran aground with: > "The above is poor coding. You should be..." > "Again where's the error checking?????" These seem mild to me. Both are factual and to the point. How would you rate that bit of code? I guess I could have said "Your code may work, but it's hard to read and doesn't check for database errors." I will endeavor to tread more lightly and resist the urge to use more than one ?. I am probably a little callous because of the flame wars that have appeared here over the years. > It isn't a requirement of usenet to treat someone like an idiot. It wasn't my intention. -- Tom Thackrey www.creative-light.com tom (at) creative (dash) light (dot) com do NOT send email to jamesbutler@willglen.net (it's reserved for spammers) |