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
https://forums.phpfreaks.com/topic/110831-getting-info-from-mysql-database/
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 =[

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?

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

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

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.

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.