Jump to content

Getting Info From MYSQL Database.


bradblog2

Recommended Posts

Hey there everyone.

 

I have a little coding project that I am working on and cannot get the PHP part working.

 

Basically all I want to do is get the information (supplied by the user at the time of registration) to display when the server calls for it in their profile page.

 

Here is my code. Thanks

 

<?php
$link=mysql_connect("localhost","root","lolfunny1");
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db=mysql_select_db("members");
if(!$db) {
	die("Unable to select database");
}
$result = mysql_query("SELECT * from members WHERE member_id='$_SESSION['SESS_MEMBER_ID']'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

?>

 

Any suggestions? I'm very N00B at this and could use any tips.

 

Brad

Link to comment
Share on other sites

You shouldn't really post your database connect details on forums :P perhaps a moderator will remove it.

Anyway, back to the point:

 

<?php
if($con = mysql_connect('servername', 'username', 'password')) 
 mysql_select_db('db_name') 
   or die('MySQL Error: ' . mysql_error());


if(isset($_SESSION['id'])) { // make sure the session is set
 if(is_int($_SESSION['id']))
   $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database!
 
 $query = sprintf("SELECT * FROM `db_name` . `table_name` WHERE `id` = '%d' LIMIT 1", $uid); // Make sure it only calls one user
 $result = mysql_query($query) or die('MySQL Error: ' . mysql_error());
 while($row = mysql_fetch_array($result)) { // while loop
   if(mysql_num_rows($result) > 0) {
     print $row['username']; // etc...
     print $row['email']; // etc...
   }
   else
     print 'User does not exist!';
 }
}
else
 die 'You are not logged in!';

mysql_close($con);
?>

 

That's how I would do it personally, but your query should be:

 

$result = mysql_query("SELECT * from members WHERE member_id=' . $_SESSION['SESS_MEMBER_ID'] . '");

 

 

EDIT: whizard got there before me =[

Link to comment
Share on other sites

Alright, I tried replacing the code with the example above and I got the following error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in xxxxxxxxxx on line 11

 

I tried the second set of code (replacing the first one) and simply nothing displayed.

 

Is there something I'm doing wrong?

Link to comment
Share on other sites

Try using:

<?php
$con = mysql_connect('servername', 'username', 'password')

if($con) {
  mysql_select_db('db_name') or die('MySQL Error: ' . mysql_error());
}

if(isset($_SESSION['id'])) { // make sure the session is set
  if(is_int($_SESSION['id'])) {
    $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database!
  
  $query = "SELECT * FROM table_name WHERE id = '".$uid."' LIMIT 1"; // Make sure it only calls one user
  $result = mysql_query($query) or die('MySQL Error: ' . mysql_error());
  while($row = mysql_fetch_array($result)) { // while loop
    if(mysql_num_rows($result) > 0) {
      print $row['username']; // etc...
      print $row['email']; // etc...
    }
    else {
      print 'User does not exist!';
}
  }
  }
}
else
  die 'You are not logged in!';

mysql_close($con);
?>

 

I edited Wolphies code a little :P

Link to comment
Share on other sites

Sorry, forgot to add a linebreak.

<?php
$con = mysql_connect('servername', 'username', 'password');

if($con) {
  mysql_select_db('db_name') or die('MySQL Error: ' . mysql_error());
}

if(isset($_SESSION['id'])) { // make sure the session is set
  if(is_int($_SESSION['id'])) {
    $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database!
  
  $query = "SELECT * FROM table_name WHERE id = '".$uid."' LIMIT 1"; // Make sure it only calls one user
  $result = mysql_query($query) or die('MySQL Error: ' . mysql_error());
  while($row = mysql_fetch_array($result)) { // while loop
    if(mysql_num_rows($result) > 0) {
      print $row['username']; // etc...
      print $row['email']; // etc...
    }
    else {
      print 'User does not exist!';
}
  }
  }
}
else
  die 'You are not logged in!';

mysql_close($con);
?>

Link to comment
Share on other sites

@ abdbuet - I have a login script that starts a session when they log in.

As of my knowledge, you have to start your session in every page to keep the session variables. It is not enough that you start it during one logs in and then don't start it in other pages.

Link to comment
Share on other sites

Hmm, try this:

<?php
$con = mysql_connect('servername', 'username', 'password');

if($con) {
  mysql_select_db('db_name') or die('MySQL Error: ' . mysql_error());
}

if(isset($_SESSION['id'])) { // make sure the session is set
  if(is_int($_SESSION['id'])) {
    $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database!
  
  $query = "SELECT * FROM table_name WHERE id = '".$uid."' LIMIT 1"; // Make sure it only calls one user
  $result = mysql_query($query) or die('MySQL Error: ' . mysql_error());
  while($row = mysql_fetch_array($result)) { // while loop
    if(mysql_num_rows($result) > 0) {
      print $row['username']; // etc...
      print $row['email']; // etc...
    }
    else {
      print 'User does not exist!';
}
  }
  }
}
else {
  die("You are not logged in!");
}

mysql_close($con);
?>

 

Otherwise it looks like it'd work to me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.