Skip to content
View in the app

A better way to browse. Learn more.

Tip.It Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Help -Calling SQL Data In a Template

Featured Replies

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?

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]

  • Author

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; ?> 

 

?

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

  • Author

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; ?>




<...>

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

Create an account or sign in to comment

Important Information

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.