Forum Moderators: coopster

Message Too Old, No Replies

How to use PHP & Html to fetch data from mysql

php,html,mysql,database,array,variable,pass values

         

NeilsPHP

8:04 pm on Jun 19, 2008 (gmt 0)

10+ Year Member



Hi,
I am trying to create a html link that will access mysql to compare the value passed using html link and display the corresponsding row/column only.
for ex.Lets say i have a
databse with columns:date,firstname,lastname,email,subject,message

I have a php page that will display the current mysql contents by "subject" & "date" only (and not all details yet)

Once a reader likes to read more about a subject(which will be a clickable link)then that action should open up another php page that will automatically access database at that moment and display the "message" that is being requested by reader.
here is how i am trying to do it but i can not get the last part right.plz help.

// set database server access variables:
$host = "#*$!#*$!";
$user = "yyyy";
$pass = "zzzzz";
$db = "mmmmmmm";

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM MYTABLE";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// print them one after another
echo "<table cellpadding=10 border=1>";

while(list($Date,$FirstName,$LastName,$Email,$Subject,$Message) = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><a href= '$Message? value=$Subject'> $Subject </a> </td>";
echo "</tr>"; }
echo "</table>"; }

i AM able to display correct links with mysql table subjectwise (and up to date) but once i click the link,its not giving me message part.Another way would be to pass a value(which i tried but not sure how to exactly do here) to another page and use that value with $GET and compare it with lets say date value in database and if its a match,then display that message.
any help would be appreciated.
thanks

NeilsPHP

8:08 pm on Jun 19, 2008 (gmt 0)

10+ Year Member



JUST A QUICK FYI:
I also tried to pass a value to another page using this code but still no success:

<?php

# GRAB THE VARIABLES FROM THE URL
$DateNew = $_GET['Date'];
echo "$DateNew";
// set database server access variables:
$host = "BBBBBB";
$user = "#*$!#*$!X";
$pass = "YYYYYYY";
$db = "ZZZZZZZ";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM MYTABLE";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if ($DateNew == $Date){
echo "$Message";}
else
{ echo" THIS IS WRONG LINK";
?>

coopster

4:35 pm on Jun 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, NeilsPHP.

I tend to use braces whenever I am embedding PHP variables in a double-quoted string:

echo "<td><a href= '{$Message}? value={$Subject}'> $Subject </a> </td>";

Also, you need to retrieve those values from the linked page once the user lands on that page. Unless you have the source to the linked page in your $Message variable being retrieved from the database, I do not see where that is yet either. I'm guessing it is contained in that variable though.

NeilsPHP

12:30 am on Jun 21, 2008 (gmt 0)

10+ Year Member



at first,thanks for the reply.
Let me simplify it a bit.
-I have a table on mysql.
-I want to have page1.php that will retrieve all those values and display using while loop,as posted in code above
-While posting these values,I also want to display values using just subjects(so reader don't have to go thr'every single message/news if not interesting and can save space on page too
-To do this,I am displaying table values in html links displaying links that can are 'clickable'
-Now here is my question,I want to have page2 in such a way that,when user clicks on message1,the will be made to pass value=1 from page1 to page2
-page2 should utilize this value to compare table messages(by using this value as ID for ex)
-page2 should display full message(that reader clicked on & wants to
read
I also have a small link at bottom of page1 that will connect to a form on page3 so user can enter NEW info into database and it will show right away thanking him for his/her posting

I am trying to do this using this code:
while(list($Date,$FirstName,$LastName,$Email,$Subject,$Message) = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><a href= '$Message? value=$Subject'> $Subject </a> </td>";

I can also see value being passed on next page in the browser,but page2 will not display message linked to that value in table (however its been able to show the table and anything else on page2)
plz help

NeilsPHP

12:32 am on Jun 21, 2008 (gmt 0)

10+ Year Member



in brief,
its like blogging,there will be a form to post a blog that will go into a database and display ALL blog posted till that day each in a hyperlink that can be clicked to read about that perticular blog

I am just trying to do this using php

NeilsPHP

1:26 am on Jun 21, 2008 (gmt 0)

10+ Year Member



and my url looks like this after clicking on any link(transfer of value)

http://example.com/Folder1/page2.php?%20value=5

my question is"What is that %20 ? as I am only trying to transfer value = 5,but why is my page2 not picking up this value ?

Here is my page2 by the way:
<?php if (isset($_GET['value'])):
{
$Date = value;
echo "<p> $Date </p>"; //just to check if it displays value here

$Counter = 0 ;
echo "<table cellpadding=40 border=3>";
while($Counter <=2)
{ echo "<tr>";

echo '<td> "The value of this is: ".$Date </td>';

echo "</tr>";

$Counter++;
}

echo "</table>";
}

?>

[edited by: eelixduppy at 1:30 am (utc) on June 21, 2008]
[edit reason] changed to example.com [/edit]

coopster

2:25 pm on Jun 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



%20 is a space. You are putting a space in your link when you build it, see the space in your original code? I even copied it when I updated it with braces, it is right here, right before the value name:
echo "<td><a href= '{$Message}? value={$Subject}'> $Subject </a> </td>";

Actually, if I was you I would build my links more like this:
$href = "{$Message}?" . htmlentities('value=' . urlencode($Subject)); 
echo "<td><a href=\"{$href}\">" . htmlentities($Subject) . "</a></td>";

The reason your page2 is not working is because you are not retrieving the value correctly:
<?php 
if (isset($_GET['value'])) {
$Date = $_GET['value'];
}

NeilsPHP

5:26 pm on Jun 21, 2008 (gmt 0)

10+ Year Member



Hi Coopster,
Thank you very much.Your suggestions worked out.
Greatly appreciate it.