p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: form action seems right but no output


Message #1 by "Lawrence" <lkrubner@g...> on Tue, 26 Feb 2002 14:57:11 -0500
This code (which is currently a mess, since I put in a bunch of echo statements in an attempt to

debug) seems to give me just what I want, a form from which people can select one entry in a weblog,

and get that one entry printed to the screen. But they get nothing. At first I thought it was the

query I was using:



$results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' = '$entryIdTimeStamp'") or

die (mysql_error());



I thought maybe the $entryIdTimeStamp was not getting the right number. But it seems like it is,

looking at the form (which I've posted below, it is the output of the below script).



Nothing is changing the entryDate in the database. So why can't I just grab the entry with the

correct entryDate? (for convience, I store the date as a unix timestamp, an interger.)



Any clues about where I should look next for the problem?







<?php

include ("lr_config.php") ;

include ("lr_functionsFile.php") ;

// connect to the database

connectLrToDb() ;

// Pull your info out of the database

$results = mysql_query ("SELECT * FROM logRhythmTable ORDER BY entryDate DESC") or die ("There was a

problem displaying the page. Please try again by pressing the Refresh button. 3");

// now start a form into which we can print the information

echo "<form method=\"post\" action=\"$self\">" ;

// Now go through the array one row at a time and

// echo the info to the screen, along with some formatting

while ($row = mysql_fetch_row($results)) {

// these next 6 rows format the data, including changing the unix timestamp into a regular date

$row[1] = stripslashes ($row[1]) ;

$row[1] = htmlspecialchars ($row[1]) ;

$entryIdTimeStamp = $row[3] ;

echo $row[3] ;

$row[3] = date ("m-d-Y H:i", $row[3]) ;

// this is the HTML that actually displays the data. If you'd like to customize

// the look of LogRhythm output, here is where you should do it.

echo "<p><b> $row[1] </b><br> Posted on: $row[3] <br> \n" ;

echo "Click this button to choose this post to view: \n" ;

echo "<input type=\"radio\" name=\"entryIdTimeStamp\" value=\"$entryIdTimeStamp\"> \n" ;

echo "<br> \n" ;

echo $entryIdTimeStamp ;

echo "</p><br><hr> \n \n" ;

}

// now end the form

echo "<input type=\"submit\" value=\"Click here to view the post you have chosen.\">" ;

echo "</form><br><br>" ;

// include a way out if they don't want to read an older post

echo "Or you can return to the <a href=\"$self\">main page</a>." ;



// Close the database connection.

mysql_close();



?>





This outputs this:





<form method="post" action="/testFunction.php">



1014747590<p><b> buffalo bill </b><br> Posted on: 02-26-2002 12:19 <br>

Click this button to choose this post to view:

<input type="radio" name="entryIdTimeStamp" value="1014747590">

<br>

1014747590</p><br><hr>



1014747528<p><b> i want you </b><br> Posted on: 02-26-2002 12:18 <br>

Click this button to choose this post to view:

<input type="radio" name="entryIdTimeStamp" value="1014747528">

<br>

1014747528</p><br><hr>



1014747485<p><b> its a dog eat dog world </b><br> Posted on: 02-26-2002 12:18 <br>

Click this button to choose this post to view:

<input type="radio" name="entryIdTimeStamp" value="1014747485">

<br>

1014747485</p><br><hr>



1014747439<p><b> woke up one mornign </b><br> Posted on: 02-26-2002 12:17 <br>

Click this button to choose this post to view:

<input type="radio" name="entryIdTimeStamp" value="1014747439">

<br>

1014747439</p><br><hr>



1014747389<p><b> behind blue eyes </b><br> Posted on: 02-26-2002 12:16 <br>

Click this button to choose this post to view:

<input type="radio" name="entryIdTimeStamp" value="1014747389">

<br>

1014747389</p><br><hr>



1013668513<p><b> Andrew Sullivan's selective outrage </b><br> Posted on: 02-14-2002 00:35 <br>

Click this button to choose this post to view:

<input type="radio" name="entryIdTimeStamp" value="1013668513">

<br>

1013668513</p><br><hr>



<input type="submit" value="Click here to view the post you have chosen."></form><br><br>Or you can

return to the <a href="/testFunction.php">main page</a>.

















Message #2 by "Hermawan Haryanto" <hermawan@h...> on Wed, 27 Feb 2002 03:10:20 +0700
First thing that I see :

1. I don't know where the $self is coming from, but if you had already

assigned that variable then it will be ok.

2. At your next sql statement :

  $results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' 

'$entryIdTimeStamp'") or die (mysql_error());

I guess it supposed to be this way :

  $results = mysql_query ("SELECT * FROM logRhythmTable WHERE entryDate 

$entryIdTimeStamp") or die (mysql_error());

The field name did not require a bracket " ' ", and also if the field is INT

then the result also did not need " ' ".





Thanks



Hermawan Haryanto

hermawan@h...

----- Original Message -----

From: "Lawrence" <lkrubner@g...>

To: "beginning php" <beginning_php@p...>

Sent: Wednesday, February 27, 2002 2:57 AM

Subject: [beginning_php] form action seems right but no output





> This code (which is currently a mess, since I put in a bunch of echo

statements in an attempt to

> debug) seems to give me just what I want, a form from which people can

select one entry in a weblog,

> and get that one entry printed to the screen. But they get nothing. At

first I thought it was the

> query I was using:

>

> $results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' 

'$entryIdTimeStamp'") or

> die (mysql_error());

>

> I thought maybe the $entryIdTimeStamp was not getting the right number.

But it seems like it is,

> looking at the form (which I've posted below, it is the output of the

below script).

>

> Nothing is changing the entryDate in the database. So why can't I just

grab the entry with the

> correct entryDate? (for convience, I store the date as a unix timestamp,

an interger.)

>

> Any clues about where I should look next for the problem?

>

>

>

> <?php

> include ("lr_config.php") ;

> include ("lr_functionsFile.php") ;

> // connect to the database

> connectLrToDb() ;

> // Pull your info out of the database

> $results = mysql_query ("SELECT * FROM logRhythmTable ORDER BY entryDate

DESC") or die ("There was a

> problem displaying the page. Please try again by pressing the Refresh

button. 3");

> // now start a form into which we can print the information

> echo "<form method=\"post\" action=\"$self\">" ;

> // Now go through the array one row at a time and

> // echo the info to the screen, along with some formatting

> while ($row = mysql_fetch_row($results)) {

> // these next 6 rows format the data, including changing the unix

timestamp into a regular date

> $row[1] = stripslashes ($row[1]) ;

> $row[1] = htmlspecialchars ($row[1]) ;

> $entryIdTimeStamp = $row[3] ;

> echo $row[3] ;

> $row[3] = date ("m-d-Y H:i", $row[3]) ;

> // this is the HTML that actually displays the data. If you'd like to

customize

> // the look of LogRhythm output, here is where you should do it.

> echo "<p><b> $row[1] </b><br> Posted on: $row[3] <br> \n" ;

> echo "Click this button to choose this post to view: \n" ;

> echo "<input type=\"radio\" name=\"entryIdTimeStamp\"

value=\"$entryIdTimeStamp\"> \n" ;

> echo "<br> \n" ;

> echo $entryIdTimeStamp ;

> echo "</p><br><hr> \n \n" ;

> }

> // now end the form

> echo "<input type=\"submit\" value=\"Click here to view the post you have

chosen.\">" ;

> echo "</form><br><br>" ;

> // include a way out if they don't want to read an older post

> echo "Or you can return to the <a href=\"$self\">main page</a>." ;

>

> // Close the database connection.

> mysql_close();

>

> ?>

>

>

> This outputs this:

>

>

> <form method="post" action="/testFunction.php">

>

> 1014747590<p><b> buffalo bill </b><br> Posted on: 02-26-2002 12:19 <br>

> Click this button to choose this post to view:

> <input type="radio" name="entryIdTimeStamp" value="1014747590">

> <br>

> 1014747590</p><br><hr>

>

> 1014747528<p><b> i want you </b><br> Posted on: 02-26-2002 12:18 <br>

> Click this button to choose this post to view:

> <input type="radio" name="entryIdTimeStamp" value="1014747528">

> <br>

> 1014747528</p><br><hr>

>

> 1014747485<p><b> its a dog eat dog world </b><br> Posted on: 02-26-2002

12:18 <br>

> Click this button to choose this post to view:

> <input type="radio" name="entryIdTimeStamp" value="1014747485">

> <br>

> 1014747485</p><br><hr>

>

> 1014747439<p><b> woke up one mornign </b><br> Posted on: 02-26-2002 12:17

<br>

> Click this button to choose this post to view:

> <input type="radio" name="entryIdTimeStamp" value="1014747439">

> <br>

> 1014747439</p><br><hr>

>

> 1014747389<p><b> behind blue eyes </b><br> Posted on: 02-26-2002 12:16

<br>

> Click this button to choose this post to view:

> <input type="radio" name="entryIdTimeStamp" value="1014747389">

> <br>

> 1014747389</p><br><hr>

>

> 1013668513<p><b> Andrew Sullivan's selective outrage </b><br> Posted on:

02-14-2002 00:35 <br>

> Click this button to choose this post to view:

> <input type="radio" name="entryIdTimeStamp" value="1013668513">

> <br>

> 1013668513</p><br><hr>

>

> <input type="submit" value="Click here to view the post you have

chosen."></form><br><br>Or you can

> return to the <a href="/testFunction.php">main page</a>.

>

>

>

>

>

>

>

>

>




$subst('Email.Unsub').

>



Message #3 by "Lawrence" <lkrubner@g...> on Tue, 26 Feb 2002 15:53:13 -0500
I appreciate the help. However, I wrote the SQL statement as you suggest when I first wrote it. Then

MySQL complained that I had SQL syntax errors. So I put the quote marks around the field names and

values and the errors stopped. Now I get nothing at all.



However, even if entryDate needs quotes, you're right that an interger probably doesn't. I may have

been turning the interger into a string. I'll give that a try.





From: "Hermawan Haryanto" <hermawan@h...>

> First thing that I see :

> 1. I don't know where the $self is coming from, but if you had already

> assigned that variable then it will be ok.

> 2. At your next sql statement :

>   $results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' 

> '$entryIdTimeStamp'") or die (mysql_error());

> I guess it supposed to be this way :

>   $results = mysql_query ("SELECT * FROM logRhythmTable WHERE entryDate 

> $entryIdTimeStamp") or die (mysql_error());

> The field name did not require a bracket " ' ", and also if the field is INT

> then the result also did not need " '





Message #4 by "Lawrence" <lkrubner@g...> on Tue, 26 Feb 2002 17:09:35 -0500
As a follow up, I tried it without quotes, and again MySQL complained about a syntax error. With the

quotes around $entryIdTimeStamp, nothing is returned, and without the quotes, I get an error. Not

sure what is going on.









----- Original Message -----

From: "Lawrence" <lkrubner@g...>

To: "beginning php" <beginning_php@p...>

Sent: Tuesday, February 26, 2002 3:53 PM

Subject: [beginning_php] Re: form action seems right but no output





> I appreciate the help. However, I wrote the SQL statement as you suggest when I first wrote it.

Then

> MySQL complained that I had SQL syntax errors. So I put the quote marks around the field names and

> values and the errors stopped. Now I get nothing at all.

>

> However, even if entryDate needs quotes, you're right that an interger probably doesn't. I may

have

> been turning the interger into a string. I'll give that a try.

>

>

> From: "Hermawan Haryanto" <hermawan@h...>

> > First thing that I see :

> > 1. I don't know where the $self is coming from, but if you had already

> > assigned that variable then it will be ok.

> > 2. At your next sql statement :

> >   $results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' 

> > '$entryIdTimeStamp'") or die (mysql_error());

> > I guess it supposed to be this way :

> >   $results = mysql_query ("SELECT * FROM logRhythmTable WHERE entryDate 

> > $entryIdTimeStamp") or die (mysql_error());

> > The field name did not require a bracket " ' ", and also if the field is INT

> > then the result also did not need " '

>

>

>






Message #5 by "Hermawan Haryanto" <hermawan@h...> on Wed, 27 Feb 2002 05:27:31 +0700
what if we split it into variable first :

$sql="SELECT * FROM logRhythmTable WHERE entryDate=$entryIdTimeStamp";

/* uncomment this string below to make sure that the sql query is correct;

print $sql;

*/

$result=mysql_query($sql) OR DIE (mysql_error());



Thanks



Hermawan Haryanto

hermawan@h...



----- Original Message -----

From: "Lawrence" <lkrubner@g...>

To: "beginning php" <beginning_php@p...>

Sent: Wednesday, February 27, 2002 5:09 AM

Subject: [beginning_php] Re: form action seems right but no output





> As a follow up, I tried it without quotes, and again MySQL complained

about a syntax error. With the

> quotes around $entryIdTimeStamp, nothing is returned, and without the

quotes, I get an error. Not

> sure what is going on.

>

>

>

>

> ----- Original Message -----

> From: "Lawrence" <lkrubner@g...>

> To: "beginning php" <beginning_php@p...>

> Sent: Tuesday, February 26, 2002 3:53 PM

> Subject: [beginning_php] Re: form action seems right but no output

>

>

> > I appreciate the help. However, I wrote the SQL statement as you suggest

when I first wrote it.

> Then

> > MySQL complained that I had SQL syntax errors. So I put the quote marks

around the field names and

> > values and the errors stopped. Now I get nothing at all.

> >

> > However, even if entryDate needs quotes, you're right that an interger

probably doesn't. I may

> have

> > been turning the interger into a string. I'll give that a try.

> >

> >

> > From: "Hermawan Haryanto" <hermawan@h...>

> > > First thing that I see :

> > > 1. I don't know where the $self is coming from, but if you had already

> > > assigned that variable then it will be ok.

> > > 2. At your next sql statement :

> > >   $results = mysql_query ("SELECT * FROM logRhythmTable WHERE

'entryDate' 

> > > '$entryIdTimeStamp'") or die (mysql_error());

> > > I guess it supposed to be this way :

> > >   $results = mysql_query ("SELECT * FROM logRhythmTable WHERE

entryDate 

> > > $entryIdTimeStamp") or die (mysql_error());

> > > The field name did not require a bracket " ' ", and also if the field

is INT

> > > then the result also did not need " '

> >

> >

> >




$subst('Email.Unsub').

>

>




$subst('Email.Unsub').

>



Message #6 by "Lawrence" <lkrubner@g...> on Tue, 26 Feb 2002 17:24:13 -0500
Still trying to debug this form. I ran phpinfo() and found that my hosting provider had this version

of PHP:



PHP Version 4.0.1pl2



register_globals was on.





I looked in the database (using phpMyAdmin) and saw that entryDate for last item was 1014747590. I

looked at the form which had been created by the code (which I posted in my last email) and saw this

line:



<input type="radio" name="entryIdTimeStamp" value="1014747590">



Now, unless I'm missing something, this should create a global variable called entryIdTimeStamp with

a value of 1014747590. So this SQL should work:



$results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' = '$entryIdTimeStamp'") or

die (mysql_error());



But it doesn't.



















Message #7 by "Lawrence" <lkrubner@g...> on Tue, 26 Feb 2002 17:38:34 -0500
I apoligize for bothering everyone so much with my problem, but I'm really stumped by this one.

Here's the function that is not returning any value. I can't figure out why it doesn't work.

$entryIdTimeStamp has been submitted by a form. It is a unix timestamp, as are the entries in

entryDate. register_globals is on, so I'm assuming any input from a form is automatically becoming a

global variable. The file full of functions is being included() on the page with the forms that the

user interacts with. But when this function runs, its as if $entryIdTimeStamp has no value, the only

line that works is the final one :



echo "Click here to get back to the <a href=\"$self\">main page</a>.\n" ;





Here's the function:





function printPickedPastPost() {

// declare globals

global $self ;

global $entryIdTimeStamp ;

// connect to the database

connectLrToDb() or die (mysql_error()) ;

// Pull your info out of the database

$results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' = '$entryIdTimeStamp'") or

die (mysql_error());

// Now go through the array one row at a time and

// echo the info to the screen, along with some formatting

while ($row = mysql_fetch_row($results)) {

// these next 6 rows format the data, including changing the unix timestamp into a regular date

$row[1] = stripslashes ($row[1]) ;

$row[2] = stripslashes ($row[2]) ;

$row[1] = htmlspecialchars ($row[1]) ;

$row[2] = htmlspecialchars ($row[2]) ;

$row[2] = nl2br ($row[2]) ;

$row[3] = date ("m-d-Y H:i", $row[3]) ;

// this is the HTML that actually displays the data. If you'd like to customize

// the look of LogRhythm output, here is where you should do it.

echo "<p><b><font size=\"+1\"> $row[1] </font></b><br> Posted on: $row[3] </p> \n" ;

echo "<p><br> $row[2] </p><br><br><hr> \n \n" ;

}

// Close the database connection.

mysql_close();

// give visitor a way back to the main page

echo "Click here to get back to the <a href=\"$self\">main page</a>.\n" ;

}





Message #8 by "Hermawan Haryanto" <hermawan@h...> on Wed, 27 Feb 2002 05:39:31 +0700
Let me see the Database structure with their field type, maybe we can try

another method





Thanks



Hermawan Haryanto

hermawan@h...



Message #9 by "Hermawan Haryanto" <hermawan@h...> on Wed, 27 Feb 2002 05:50:46 +0700
I really getting confuse now,

On your first post, there is non of the script portion that sayz about

function, but on your last post, there is a function printPickedPastPost()

and all of the code is on that function.

Can you please post all of the script so we can help you find out in which

section the error might be.



Thanks



Hermawan Haryanto

hermawan@h...



----- Original Message -----

From: "Lawrence" <lkrubner@g...>

To: "beginning php" <beginning_php@p...>

Sent: Wednesday, February 27, 2002 5:38 AM

Subject: [beginning_php] Re: form action seems right but no output





> I apoligize for bothering everyone so much with my problem, but I'm really

stumped by this one.

> Here's the function that is not returning any value. I can't figure out

why it doesn't work.

> $entryIdTimeStamp has been submitted by a form. It is a unix timestamp, as

are the entries in

> entryDate. register_globals is on, so I'm assuming any input from a form

is automatically becoming a

> global variable. The file full of functions is being included() on the

page with the forms that the

> user interacts with. But when this function runs, its as if

$entryIdTimeStamp has no value, the only

> line that works is the final one :

>

> echo "Click here to get back to the <a href=\"$self\">main page</a>.\n" ;

>

>

> Here's the function:

>

>

> function printPickedPastPost() {

> // declare globals

> global $self ;

> global $entryIdTimeStamp ;

> // connect to the database

> connectLrToDb() or die (mysql_error()) ;

> // Pull your info out of the database

> $results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' 

'$entryIdTimeStamp'") or

> die (mysql_error());

> // Now go through the array one row at a time and

> // echo the info to the screen, along with some formatting

> while ($row = mysql_fetch_row($results)) {

> // these next 6 rows format the data, including changing the unix

timestamp into a regular date

> $row[1] = stripslashes ($row[1]) ;

> $row[2] = stripslashes ($row[2]) ;

> $row[1] = htmlspecialchars ($row[1]) ;

> $row[2] = htmlspecialchars ($row[2]) ;

> $row[2] = nl2br ($row[2]) ;

> $row[3] = date ("m-d-Y H:i", $row[3]) ;

> // this is the HTML that actually displays the data. If you'd like to

customize

> // the look of LogRhythm output, here is where you should do it.

> echo "<p><b><font size=\"+1\"> $row[1] </font></b><br> Posted on: $row[3]

</p> \n" ;

> echo "<p><br> $row[2] </p><br><br><hr> \n \n" ;

> }

> // Close the database connection.

> mysql_close();

> // give visitor a way back to the main page

> echo "Click here to get back to the <a href=\"$self\">main page</a>.\n" ;

> }

>

>

>




$subst('Email.Unsub').

>



Message #10 by "Lawrence" <lkrubner@g...> on Tue, 26 Feb 2002 17:54:09 -0500
I'm not sure why the die(mysql_error()) line didn't flag a problem for me, but when I go into the

function that makes a connection to the database and uncomment the print_r() lines, I get a message

in my browser that says, simply,

Resource id #11



I don't know what that means. This is function I'm using to connect. Other functions also call it,

and the other functions have no trouble with it. I'm sure I'm missing something obvious, but I don't

know what it is.







//

// this function connects Log Rhythm to the database

function connectLrToDb() {

// this brings the database variables into the function

global $host ;

global $username ;

global $passwordDb ;

global $database ;

// this connects to the server

$result1 = mysql_connect ($host, $username, $passwordDb) or die (mysql_error());

// this selects the correct database

$result2 = mysql_select_db ($database) or die (mysql_error());

// uncomment the following lines if you need to debug

print_r($result1) ;

print_r($result2) ;

return ;

}





Message #11 by "Nikolai Devereaux" <yomama@u...> on Tue, 26 Feb 2002 16:33:52 -0800

resource id #11 is the unique identifier that PHP uses internally to access

your query result.



When you connect to a database, you get a resource ID.  when you make a

query, you get a resource ID.  etc...  these ID's are passed to many of the

other functions (e.g. mysql_fetch_row) so that the function knows which

query result to pull the next row from.



nik



> -----Original Message-----

> From: Lawrence [mailto:lkrubner@g...]

> Sent: Tuesday, February 26, 2002 2:54 PM

> To: beginning php

> Subject: [beginning_php] Re: form action seems right but no output

>

>

> I'm not sure why the die(mysql_error()) line didn't flag a

> problem for me, but when I go into the

> function that makes a connection to the database and uncomment

> the print_r() lines, I get a message

> in my browser that says, simply,

> Resource id #11

>

> I don't know what that means. This is function I'm using to

> connect. Other functions also call it,

> and the other functions have no trouble with it. I'm sure I'm

> missing something obvious, but I don't

> know what it is.

>

>

>

> //

> // this function connects Log Rhythm to the database

> function connectLrToDb() {

> // this brings the database variables into the function

> global $host ;

> global $username ;

> global $passwordDb ;

> global $database ;

> // this connects to the server

> $result1 = mysql_connect ($host, $username, $passwordDb) or die

> (mysql_error());

> // this selects the correct database

> $result2 = mysql_select_db ($database) or die (mysql_error());

> // uncomment the following lines if you need to debug

> print_r($result1) ;

> print_r($result2) ;

> return ;

> }

>

>

>




> $subst('Email.Unsub').



Message #12 by "Nikolai Devereaux" <yomama@u...> on Tue, 26 Feb 2002 18:09:53 -0800

I'm sorry, I missed most of the series... but I noticed that in your

printPickedPastPost() function, have you (for debugging purposes) output the

value of entryIdTimeStamp (or your other globals) to make sure they're

valid?



when you connect to the db, I think you said you were getting resource ID

#11 or something like that... That's a good sign.



however, mysql_select_db returns a true/false, not a link ID.



Regardless, your connectLrToDb() function does NOT return a value, probably

because if there's any mysql errors, it just quits.  (this isn't that great

of an idea, for reasons I'll get to later...)



Anyway, moving on...  you have this line in your printPickedPastPost()

function:



connectLrToDb() or die (mysql_error()) ;



okay -- what do you think is going on here?  connectLrToDb() will exit if

there's an error, but you treat it as if its return value can be treated as

a boolean value.  It's never a good idea to exit or die within a wrapper

function because it's too easy to make this mistake.



What's likely happening is that your function returns nothing, which is

interpreted as a false or null value by the 'or' operator, and die() is

being called with the return value of mysql_error() as its parameter.



Well, since connectToLrDb() returned, obviously there WAS no error, so that

string is probably empty.



the result, of course, is that your script is probably dying for no apparent

reason, and nothing is being output to the screen.



Another thing -- the last line in your connectLrToDb() is "return ;".  This

is completely unnecessary -- if a function is supposed to return a value, go

ahead and return it.  otherwise, the interpreter will parse the closing

brace of the function and know to return nothing.





nik



Message #13 by "Lawrence" <lkrubner@g...> on Wed, 27 Feb 2002 00:39:46 -0500
From: "Nikolai Devereaux" <yomama@u...>

> resource id #11 is the unique identifier that PHP uses internally to access

> your query result



So when I get an error message that says Resource id #11 is not available, that means what? That

some other process is still holding it? I thought for awhile that perhaps I was opening a

connection, failing to close it, and then trying to start another. But seems like all the functions

I have end with mysql_close() . Not sure what else could be wrong.



Message #14 by "Lawrence" <lkrubner@g...> on Wed, 27 Feb 2002 00:54:04 -0500
From: "Hermawan Haryanto" <hermawan@h...>

> Let me see the Database structure with their field type, maybe we can try

> another method



Hermawan, I'm grateful to you for taking an interest in this problem. This one seems beyond my

(limited) ability to debug. I've tracked down the problem to the database connection, but I don't

know why there is a problem. Apparently there is no problem with the SQL statement.



As briefly as possible, I'll give you an overview of what I'm up against. I have 4 files:



lr_config.php

lr_functions.php

lr_controlPanel.php

testPage.php



This is a simple web log. The owner of the weblog goes to the control panel to input new entries.

testPage.php is where I go to see what the public would see, it is where the entries are output. The

config and function files are included() on both the control panel and the test page. testPage

doesn't do much except call a function that grabs the most recent 12 entries in the database. Very

simple.



I've been trying to add the ability to look at older posts (beyond the most recent 12) and I'm

having trouble. I can't print here all the functions in the function file, that would be too long,

but instead, below, I'm printing the three that are involved in this operation.



The strange thing, the heart of the problem, is that the first function works fine, it connects to

the database without problems. The second function, which calls on the same function as the first to

make the connection to the database, somehow causes that function to fail. To be clear: the

connection to the database is being handled by the same function in both cases, but it works the

first time and fails the second time. Also, for some reason the failure, on the second try, doesn't

trigger any of the die() statements, instead, I had to uncomment the print_r lines to find out what

was wrong. I don't know what I'm up against.



This is the database structure:



CREATE TABLE logRhythmTable (

entryNumber   INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,

entryTitle    VARCHAR (200),

entryContent  TEXT,

entryDate     INT UNSIGNED

) ;





Here are the 3 functions:



/////////////////////////////////////////////////////////



function printListOfPastPosts() {

// declare globals

global $self ;

// connect to the database

connectLrToDb() ;

// Pull your info out of the database

$results = mysql_query ("SELECT * FROM logRhythmTable ORDER BY entryDate DESC") or die ("There was a

problem displaying the page. Please try again by pressing the Refresh button. 3");

// now start a form into which we can print the information

echo "<form method=\"post\" action=\"$self\"> \n \n" ;

// Now go through the array one row at a time and

// echo the info to the screen, along with some formatting

while ($row = mysql_fetch_row($results)) {

// these next 6 rows format the data, including changing the unix timestamp into a regular date

$row[1] = stripslashes ($row[1]) ;

$row[1] = htmlspecialchars ($row[1]) ;

$entryIdTimeStamp = $row[3] ;

$row[3] = date ("m-d-Y H:i", $row[3]) ;

// this is the HTML that actually displays the data. If you'd like to customize

// the look of LogRhythm output, here is where you should do it.

echo "<p><b> $row[1] </b><br> Posted on: $row[3] <br> \n" ;

echo "Click this button to choose this post to view:\n" ;

echo "<input type=\"radio\" name=\"entryIdTimeStamp\" value=\"$entryIdTimeStamp\"> \n" ;

echo "</p><br><hr> \n" ;

}

// now end the form

echo "<input type=\"submit\" value=\"Click here to view the post you have chosen.\"> \n" ;

echo "</form><br><br> \n \n" ;

// include a way out if they don't want to read an older post

echo "Or you can return to the <a href=\"$self\">main page</a>. \n" ;



// Close the database connection.

mysql_close();

return ;

}





///////////////////////////////////////////////////////////



function printPickedPastPost() {

// declare globals

global $self ;

global $entryIdTimeStamp ;

// connect to the database

connectLrToDb() ;

// Pull your info out of the database

$results = mysql_query ("SELECT * FROM logRhythmTable WHERE 'entryDate' = '$entryIdTimeStamp'") or

die (mysql_error());

// Now go through the array one row at a time and

// echo the info to the screen, along with some formatting

while ($row = mysql_fetch_row($results)) {

// these next 6 rows format the data, including changing the unix timestamp into a regular date

$row[1] = stripslashes ($row[1]) ;

$row[2] = stripslashes ($row[2]) ;

$row[1] = htmlspecialchars ($row[1]) ;

$row[2] = htmlspecialchars ($row[2]) ;

$row[2] = nl2br ($row[2]) ;

$row[3] = date ("m-d-Y H:i", $row[3]) ;

// this is the HTML that actually displays the data. If you'd like to customize

// the look of LogRhythm output, here is where you should do it.

echo "<p><b><font size=\"+1\"> $row[1] </font></b><br> Posted on: $row[3] </p> \n" ;

echo "<p><br> $row[2] </p><br><br><hr> \n \n" ;

}

// Close the database connection.

mysql_close();

// give visitor a way back to the main page

echo "Click here to get back to the <a href=\"$self\">main page</a>.\n" ;

return ;

}





/////////////////////////////////////////////////////////////



//

// this function connects Log Rhythm to the database

function connectLrToDb() {

// this brings the database variables into the function

global $host ;

global $username ;

global $passwordDb ;

global $database ;

// this connects to the server

$result1 = mysql_connect ($host, $username, $passwordDb) or die (mysql_error());

// this selects the correct database

$result2 = mysql_select_db ($database) or die (mysql_error());

// uncomment the following lines if you need to debug

// print_r($result1) ;

// print_r($result2) ;

return ;

}





////////////////////////////////////////////////////////////////////////////

















Message #15 by "Lawrence" <lkrubner@g...> on Wed, 27 Feb 2002 03:10:58 -0500
I apoligize again for taking up so much bandwidth on the list today, and writing so many posts. I

appreciate all the help.



To debug, I added the line:



var_dump($results);



Right after the SQL statement. Now on the final page I get this:



Resource id #11resource(2) of type 9 Click here to get back to the main page.



On www.php.net I was able to track down this brief mention about resources:





>>>>>>>>>>>>>

Resource

A resource is a special variable, holding a reference to an external resource. Resources are created

and used by special functions. See the appendix for a listing of all these functions and the

corresponding resource types.



Notã: The resource type was introduced in PHP 4



Freeing resources

Due to the reference-counting system introduced with PHP4's Zend-engine, it is automatically

detected when a resource is no longer referred to (just like Java). When this is the case, all

resources that were in use for this resource are made free by the garbage collector. For this

reason, it is rarely ever necessary to free the memory manually by using some free_result function.



Notã: Persistent database-links are special, they are not destroyed by the gc. See also persistent

links

>>>>>>>>>>>>>>



However, I wasn't able to find out what "type 9" was. In the appendix to the online manual they list

all the types of resources alphabetically, but don't give the type numbers. I tried using the

function get_resource_type() on my SQL $results variable, but I got an "undefined function" error,

so I assume get_resource_type() was introduced later than the version of PHP I'm using.



Not that it matters. On www.php.net it says:



"Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or

FALSE if the query was not executed correctly. For other type of SQL statements, mysql_query()

returns TRUE on success and FALSE on error. A non-FALSE return value means that the query was legal

and could be executed by the server. It does not indicate anything about the number of rows affected

or returned. It is perfectly possible for a query to succeed but affect no rows or return no rows."



So the fact that it returns a resource means that it returned true. I guess I'll go back and look at

the SQL again. Maybe there is a problem I didn't see before.



























Message #16 by Hermawan Haryanto <hermawan@d...> on Wed, 27 Feb 2002 17:20:54 +0700
<P>Dear Lawrence,</P>

<P>I've create article on how to connect and executing sql query correctly.

It's in Indonesian language, maybe you can ask someone that speaks

Indonesian to translate it for you. If you implement it on your code, then

I'll guarantee it will work as you hope. Take a look at the artikel in <A

href="http://hermawan.haryan.to">http://hermawan.haryan.to</A></P>

<P><BR>Thanks<BR>Hermawan Haryanto<BR><A

href="mailto:hermawan@h...">hermawan@h...</A><BR>--<BR>Dream it ?,

Code it!<BR><BR><BR></P>

<BLOCKQUOTE dir=ltr style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px;

MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">

<DIV style="FONT: 10pt arial">--------- Original message --------<BR>From:

"Lawrence" &lt;lkrubner@g...&gt;<BR>To: "beginning php"

&lt;beginning_php@p...&gt;<BR>Subject: [beginning_php] Re: form

action seems right but no output<BR>Date: 02-27-02 17:00<BR><BR><FONT

face="Courier New" size=2>I apoligize again for taking up so much bandwidth

on the list today, and writing so many posts. I<BR>appreciate all the

help.<BR><BR>To debug, I added the

line:<BR><BR>var_dump($results);<BR><BR>Right after the SQL statement. Now

on the final page I get this:<BR><BR>Resource id #11resource(2) of type 9

Click here to get back to the main page.<BR><BR>On <A class=autolink

href="http://www.php.net/" target=_blank>www.php.net</A> I was able to track

down this brief mention about

resources:<BR><BR><BR>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&
;gt;&gt;<BR>Resource<BR>A

resource is a special variable, holding a reference to an external resource.

Resources are created<BR>and used by special functions. See the appendix for

a listing of all these functions and the<BR>corresponding resource

types.<BR><BR>Notã: The resource type was introduced in PHP 4<BR><BR>Freeing

resources<BR>Due to the reference-counting system introduced with PHP4's

Zend-engine, it is automatically<BR>detected when a resource is no longer

referred to (just like Java). When this is the case, all<BR>resources that

were in use for this resource are made free by the garbage collector. For

this<BR>reason, it is rarely ever necessary to free the memory manually by

using some free_result function.<BR><BR>Notã: Persistent database-links are

special, they are not destroyed by the gc. See also

persistent<BR>links<BR>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&
amp;gt;&gt;<BR><BR>However,

I wasn't able to find out what "type 9" was. In the appendix to the online

manual they list<BR>all the types of resources alphabetically, but don't

give the type numbers. I tried using the<BR>function get_resource_type() on

my SQL $results variable, but I got an "undefined function" error,<BR>so I

assume get_resource_type() was introduced later than the version of PHP I'm

using.<BR><BR>Not that it matters. On <A class=autolink

href="http://www.php.net/" target=_blank>www.php.net</A> it

says:<BR><BR>"Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements

mysql_query() returns a resource identifier or<BR>FALSE if the query was not

executed correctly. For other type of SQL statements,

mysql_query()<BR>returns TRUE on success and FALSE on error. A non-FALSE

return value means that the query was legal<BR>and could be executed by the

server. It does not indicate anything about the number of rows

affected<BR>or returned. It is perfectly possible for a query to succeed but

affect no rows or return no rows."<BR><BR>So the fact that it returns a

resource means that it returned true. I guess I'll go back and look

at<BR>the SQL again. Maybe there is a problem I didn't see

before.<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><B
R><BR><BR>---<BR>Change

your mail options at <A class=autolink

href="http://p2p.wrox.com/manager.asp"

target=_blank>http://p2p.wrox.com/manager.asp</A> or <BR>to unsubscribe send

a blank email to <A class=autolink

href="mailto:$subst('Email.Unsub')">$subst('Email.Unsub')</A>.<BR><BR></FONT></DIV></BLOCKQUOTE&
gt;<br>

________________________________________________<br>

This e-mail was sent by dMonster.com Web Based Mail 3.0<br>



Message #17 by "Lawrence" <lkrubner@g...> on Wed, 27 Feb 2002 02:43:51 -0500
From: "Nikolai Devereaux" <yomama@u...>

> when you connect to the db, I think you said you were getting resource ID

> #11 or something like that... That's a good sign.



Yes, that is the funny thing. Everything seems to go right. But on the final page, instead of the

entry, I only get this:



Resource id #111Click here to get back to the main page



that's resource #11, followed by a 1, because, to debug, I just put an echo statement on

mysql_close() function at the end of the function, so it is returning "true" and then it prints a

link to go back. But the entry isn't there.



You can look for yourself, go to this page, then click, at the bottom, on the "See all entries"

button, pick an entry, and hit the submit button. The page:



http://www.krubner.com/testPage.php







> however, mysql_select_db returns a true/false, not a link ID.



That's good to know.







> Regardless, your connectLrToDb() function does NOT return a value, probably

> because if there's any mysql errors, it just quits.  (this isn't that great

> of an idea, for reasons I'll get to later...)



But why, if there are errors, don't any of the die(mysql_error()) calls get tripped? I'd be

delighted if an error printed to the screen, at least then I'd know what's going wrong. But I'm

getting nothing.









> connectLrToDb() or die (mysql_error()) ;

> > okay -- what do you think is going on here?  connectLrToDb() will exit if

> there's an error, but you treat it as if its return value can be treated as

> a boolean value.  It's never a good idea to exit or die within a wrapper

> function because it's too easy to make this mistake.



The die statement was for debugging only. I did not originally write it that way, I just threw it in

later when I was trying to figure out where the problem is. You are right, it is useless, if a line

in connectLrToDb() failed then one of the die() statements in that function would have been

triggered.











> What's likely happening is that your function returns nothing, which is

> interpreted as a false or null value by the 'or' operator, and die() is

> being called with the return value of mysql_error() as its parameter.

>

> Well, since connectToLrDb() returned, obviously there WAS no error, so that

> string is probably empty.

>

> the result, of course, is that your script is probably dying for no apparent

> reason, and nothing is being output to the screen.



A very interesting idea. However, the problem preceded the moment I put the die() statements in, and

the problem is still there now that I've taken the die statements back out.



I very much appreciate the help. I've killed a whole day trying to solve what, I think, must be

something very obvious.



take care,



lawrence












  Return to Index