Jump to content

url variables to call from database?


.Stealth

Recommended Posts

Hello, im new and seeking help, im really stuck.

ok, the story is this:

im creating a script for my site, to add tutorials, and display them, all is well, so far i have:

created a mysql database with the following fields:
id (autoincrement)
title
content

i have made the form, to submit the data and it works, the problem i have is that i cant get the required data, im doing it like this:
im echoing a list of links onto a page, the code ive used for that is:

[code]<?php
  mysql_connect($host,$user,$password) or die('could not connect to database');
  mysql_select_db($database);
  $query="SELECT * FROM tutorials";
  $result=mysql_query($query);
  $num=mysql_num_rows($result);
  $i=0;
  while($i < $num) {
 
$id=mysql_result($result,$i,"id");
$title=mysql_result($result,$i,"title");
echo "<a href='http://www.sketchfx.co.uk/tutorial.php?id=$id'>$title</a><br />";
  $i++;
  }
  mysql_close();
  ?>[/code]


as you can tell, im using url variables, but what i want to know is how do i get the corresponding content filed's data to display on that page.
take a look at the effect of the above code here:
[url=http://www.sketchfx.co.uk/pstutorials.php]http://www.sketchfx.co.uk/pstutorials.php[/url]

ive been told to use the $_GET to store the id, but i cant figure out how to use it i can get it to display the ID, but i dont want it to do that, i want it to display the content field.

ive tried a few things but so far nothing has worked, heres what ive got to display the id (but i dont want that)

[code]<?php
mysql_connect($host,$user,$password) or die('could not connect to database');
mysql_select_db($database);
$id=$_GET['id'];
echo $id;
?>[/code]


ive also tried this but it didnt work:

  [code]<?php
mysql_connect($host,$user,$password) or die('could not connect to database');
mysql_select_db($database);
$id=$_GET['id'];
$query="SELECT $id FROM tutorials";
$tutorial=mysql_query($query);
echo $tutorial;
?>[/code]
can anybody shed some light on this, ive been doing it for ages and just cant figure it out, thanks
Link to comment
https://forums.phpfreaks.com/topic/36246-url-variables-to-call-from-database/
Share on other sites

you have to select it from the database WHERE id='$id'

so your query should look like this:
[code=php:0]
$query = mysql_query("SELECT * FROM tutorials WHERE `id`='{$id}'") or die("Error: ".mysql_error());
$data = mysql_fetch_array($query);
echo $data['name']; //echo out the tutorial name
[/code]
Its allot simpler than your making out. An example....

[code]
<?php
  // connect to db.
  if (isset($_GET['id'])) {
    $sql = "SELECT fld FROM tbl WHERE id = '{$_GET['id']}'";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        echo $row['fld'];
      }
    } else {
      echo "Query failed ".mysql_error()."<br />$sql";
    }
  }
?>
[/code]

If you passed this script a url like script.php?id=13 it would show the filed associated with id 13.
oh my god i love you guys, ive been at this for 3 hours or more, on another forum i go on they just kept telling me $_GET, not telling me anything else, i think they just wanted me to work it out but by the time youve been trying as long as me it kinda gets annoying lol


thanks sooo much, i cant beleive it :D
yeah ive read it through a few times, im having a break now though, ive been trying for too long lol.
dont worry though i will have a good look, i never get help then dont try to learn from it because ill just end up asking the same question if i dont lol

thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.