Jump to content

PHP Help -Calling SQL Data In a Template


Errdoth

Recommended Posts

I have a form, where the (logged in) user enters their "posting", so like Title, Location, Description...; and uses php to insert those values into a table, with an auto-increment identifier. I need the script to be able to output that SQL data into a template, so that it has labels for Title, Location, Description, et cetera; and have it so the url would be

 

http://whatever.tld/test.php?id="Mysql row number"

 

and I could also list the title's on a separate page. Basically a watered down version of a forum.

 

 

 

So how would I go about doing something like that?

Link to comment
Share on other sites

http://us.php.net/mysql

 

 

 

[hide=An example from the page]

<?php

// Connecting, selecting database

$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')

   or die('Could not connect: ' . mysql_error());

echo 'Connected successfully';

mysql_select_db('my_database') or die('Could not select database');



// Performing SQL query

$query = 'SELECT * FROM my_table';

$result = mysql_query($query) or die('Query failed: ' . mysql_error());



// Printing results in HTML

echo "</pre><table>$col_value</table>\n";<br><br><br><br>// Free resultset<br><br>mysql_free_result($result);<br><br><br><br>// Closing connection<br><br>mysql_close($link);<br><br

[/hide]

Link to comment
Share on other sites

I'm confused with that, happy. What I want to do, is when a new row in my table is created, lets say row 217; I can goto .../test.php?id=217, and see the page that says:

 

 

 

Title: "Return column one of row defined in url"

 

Description: "Return Column two of row defined in url"

 

 

 

But be able to format the thing. Is it possible to store the columns info as a variable, and then call it within some formatting?

 

(Another question with variables; if I end my script, can I still call variables used before in a new script?

 

e.g.


<?php

$title = mysql_query(print column 1 (or whatever the code is that I'm trying to figure out))

?>



<?php echo $title; ?> 

 

?

Link to comment
Share on other sites

Hi Errdoth.

 

 

 

First off.

 

But be able to format the thing. Is it possible to store the columns info as a variable, and then call it within some formatting?

 

(Another question with variables; if I end my script, can I still call variables used before in a new script?

 

Yes and yes.

 

 

 

This is how I'm interpreting what you want -

 

 

 


<?php

//Grabs the id from the URL

$id = $_GET['id'];



//Selecting the title, description etc.

$query = "SELECT * FROM db_table WHERE id=$id";

$result = mysql_query($query);



//Print the values out (with formatting)

while($row = mysql_fetch_array($result)){

  echo ".$row['title'].""; //Prints out title

  echo ".$row['description'].""; //Prints out description

}



?>

 

 

 

This will grab and display the title and description for the post with the id from your URL.

 

 

 

It will format it to some extent - The title will be in a div container called title and the description will be in a div container called description. The way it can be displayed can be changed easily.

 

and I could also list the title's on a separate page.

 

Here's another interpreted script.

 

 

 


<?php



//Selecting the title, description etc.

$query = "SELECT title,id FROM db_table";

$result = mysql_query($query);



//Print the values out (with formatting)

while($row = mysql_fetch_array($result)){

  echo ''.$row['title'].""; //Prints out the title with a link to test.php?id=theid

}

?>

 

 

 

I hope that helped at all. If it didn't, let me know and I'll see what I can do :thumbsup:

1.gif

1.png

Link to comment
Share on other sites

Thanks dude! You're awesome!

 

But with the data being stored as a variable and called later, could I do what I tried to do in my pseudo-code? Or should I just stick with doing the formatting in the same code?

 

 

 



<?php



[...]



while($row = mysql_fetch_array($result)){

$title = $row['title'];

$desc = $row['description'];

}



?>



<?php echo $title; ?>




<...>

Link to comment
Share on other sites

But with the data being stored as a variable and called later, could I do what I tried to do in my pseudo-code? Or should I just stick with doing the formatting in the same code?

 

 

You could do that, but since it's a while loop that runs through all the rows in your database, the $title and $desc variable will always end up being related to the last row that it loops through.

 

 

 

So I'd advise you to stick to doing the formatting in the loop. :D

1.gif

1.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.