Jump to content

Recommended Posts

Ahh! I probably am no were near it but here's what I have:
[code]<?php
$con = mysql_connect("-","-","-");
if (!$con)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("members", $con);

  if (isset($_GET['id'])) {
  $sql="SELECT * FROM basic WHERE id='$_GET[id]'";
  $result=mysql_query($sql);
  $num=mysql_numrows($result);
    if ($result = mysql_query($sql)) {
      $i=0;
      while ($i < $num) {
        $i++;
      }
      else  {
        echo "No results found matching $_GET['id']";
      }
    }
    else  {
      mysql_error();
    }
  }

$user=mysql_result($result,$i,"username");
$date=mysql_result($result,$i,"date");

mysql_close($con)
?>

Username: <?php echo $user ?>
<br>
Date Joined: <?php echo $date ?>[/code]

If it's not obviouse of what I'm trying to do... I'm trying to make it so when you go to 'user.php?id=1' it will display the user with the id=1. I have a row in my DB with this:
id='1' username='Zoud' date='2007-01-15 12:15:41'

>.< I'm such a noob.
Link to comment
https://forums.phpfreaks.com/topic/34282-displaying-profile/
Share on other sites

[code]<?php
$con = mysql_connect("-","-","-");
if (!$con)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("members", $con);

  if (isset($_GET['id'])) {
  $sql="SELECT * FROM basic WHERE id='".$_GET[id]."'";
  $result=mysql_query($sql);
  $num=mysql_numrows($result);
    if ($result = mysql_query($sql)) {
      $i=0;
      while ($i < $num) {
        $i++;
      }
      else  {
        echo "No results found matching ".$_GET['id'];
      }
    }
    else  {
      mysql_error();
    }
  }

$user=mysql_result($result,$i,"username");
$date=mysql_result($result,$i,"date");

mysql_close($con)
?>

Username: <?php echo $user ?>
<br>
Date Joined: <?php echo $date ?>[/code]

Just some minor tweaks to your code. However, here's my way of doing it, it's a little cleaner:
[code]<?php
  $con = mysql_connect("-","-","-") or die('Error: <br />'. mysql_error());
  mysql_select_db("members", $con) or die('Error: <br />'. mysql_error());

  if (isset($_GET['id'])) {
    $sql = "SELECT * FROM `basic` WHERE `id`='".$_GET[id]."'";
    $result = mysql_query($sql) or die('Error: '.mysql_error());
    $num = mysql_num_rows($result);
    if($num == 0) {
echo "No results found matching ".$_GET['id'];
} else {
$array = mysql_fetch_array($result);
echo "Username: ".$user;
echo "<br>"
echo "Date Joined: ".$date;
}
} else {
echo "No ID!";
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/34282-displaying-profile/#findComment-161239
Share on other sites

You got a few mistakes there- like an loop counting from 0 to $num and doing nothing except that etc'. Try this:

[code]<?php

$con = mysql_connect("-","-","-");
if (!$con)
  die("Error: ". mysql_error());

mysql_select_db("members", $con);

if (isset($_GET['id']))
{
$sql = "SELECT * FROM basic WHERE id='$_GET[id]'";
$result = mysql_query($sql) or die("Error: ".mysql_error());
$num = mysql_num_rows($result);
if($num == 0)
echo "No results found matching $_GET['id']";
else
{
for($i=0; $i < $num; $i++)
{
$user = mysql_result($result,$i,"username");
$date = mysql_result($result,$i,"date");
echo "Username: ".$user."<br>";
echo "Date Joined: ".$date;
}
}
}

?>[/code]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/34282-displaying-profile/#findComment-161241
Share on other sites

Well! there r a few things to note here:

Firstly, the query is being executed twice - once outside the if condition and once inside it.
Secondly, whats the need for the while loop? its only incrementing the value of variable $i and this value becomes equal to $num after the loop. So $i really isn't required.
Lastly, the mysql_result function in the end retrieves the row No. specified by the value in the variable $i and the value in $i at this point is beyond the last row in the resultset.

Modify the code like this:-

[code]<?php
$con = mysql_connect("-","-","-");
if (!$con)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("members", $con);

  if (isset($_GET['id'])) {
  $sql="SELECT * FROM basic WHERE id='$_GET[id]'";
  $result=mysql_query($sql);
  $num=mysql_num_rows($result);
  if($num <=0)  {
        echo "No results found matching $_GET['id']";
      }
    }
   
$user=mysql_result($result,0,"username");
$date=mysql_result($result,0,"date");

mysql_close($con)
?>

Username: <?php echo $user ?>
<br>
Date Joined: <?php echo $date ?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/34282-displaying-profile/#findComment-161244
Share on other sites

Try this, and make sure you are passing something using GET! Also make sure "basic" is the name of the table, and ti contains the columns "username" & "date".

[code]<?php

$con = mysql_connect("-","-","-");
if (!$con)
  die("Error: ". mysql_error());

mysql_select_db("members", $con) or die("Error: ".mysql_error());

if (isset($_GET['id']))
{
$id = $_GET['id'];
$sql = "SELECT * FROM `basic` WHERE `id`='$id'";
$result = mysql_query($sql) or die("Error: ".mysql_error());
$num = mysql_num_rows($result);
if($num == 0)
echo "No results found matching ".$id;
else
{
for($i=0; $i < $num; $i++)
{
$user = mysql_result($result,$i,"username");
$date = mysql_result($result,$i,"date");
echo "Username: ".$user."<br>";
echo "Date Joined: ".$date;
}
}
}

?>[/code]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/34282-displaying-profile/#findComment-161250
Share on other sites

Guest
This topic is now 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.