Jump to content

Archived

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

maxic0

$_GET help please FIXED

Recommended Posts

Hi, i haven't been doing PHP for to long, so i don't know about some thing at all. I have never quite got my head around the $_GET thing
My friend was kind enout to make this little script just to show how it works.
[code]
<?php
$sentence = 'Hello, ' . $_GET['name'] . ', and welcome to my little page.';
echo $sentence;
?>
<br /> <br />
<a href="indexx.php?name=Simon">Simon</a><br />
<a href="indexx.php?name=John">John</a><br />
<a href="indexx.php?name=Max">Max</a><br />
[/code]

This code is easy to understand, but in my site i want to create a page where the information is stored in a database and then the information is collected from there. So for example if a had a table called "people " and then a little information about Simon, John and Max was stored stored in there in a field called "info" how would i echo the information about them when their name is clicked.

Any help would be very appreciated.
Thanks
maxic0

Share this post


Link to post
Share on other sites
1. You will always want to escape $_GET variables so that people do not enter malicious content onto your site.
2. Here's what I think you want:
[code]
<?php
if (isset($_GET['name'])) {
  $name = $_GET['name'];
  $query = "SELECT * FROM users WHERE name='$name'";
  $result = mysql_query($result) or die(mysql_error());
  if ($result) {
    while ($row = mysql_fetch_array($result)) {
      echo $row['name']; // Output all information.
    }
  }
  else {
    echo 'Error with query: '.$query.'<br>'.mysql_error();
  }
}
else {
  echo 'You must specify a name.';
}
?>
[/code]

Share this post


Link to post
Share on other sites
Oh no sorry, there is a small problem :-[.
I get the output "Query was empty" even if there is something in the field.
I changed echo $row['name']; to the names of the different rows, but i get the same thing everytime. =/
Can you help me please.

Share this post


Link to post
Share on other sites
Yeah that part of it is right, but it just Says "Query was empty".
I don't understand.

Share this post


Link to post
Share on other sites
Try:

[code]
<?php
if (isset($_GET['name'])) {
  $name = $_GET['name'];
  $query = "SELECT * FROM users WHERE name='$name'";
  $result = mysql_query($result) or die(mysql_error());
  if ($result) {
    while ($row = mysql_fetch_array($result)) {
      $name = $row["name"];
      echo $row['name']; // Output all information.
    }
  }
  else {
    echo 'Error with query: '.$query.'<br>'.mysql_error();
  }
}
else {
  echo 'You must specify a name.';
}
?>
[/code]

Share this post


Link to post
Share on other sites
For the record this was the problem.

This line was wrong..
[code]
$result = mysql_query($result) or die(mysql_error());
[/code]

It should be..
[code]
$result = mysql_query($query) or die(mysql_error());
[/code]

Thanks for your help everyone. :D

Share this post


Link to post
Share on other sites
Change
[code]<?php $result = mysql_query($result) or die(mysql_error()); ?>[/code]
to
[code]<?php $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error()); [/code]
You are trying to use the contents of the variable [b]$result[/b] instead of [b][color=red]$query[/color][/b].

Ken

Share this post


Link to post
Share on other sites

×

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.