Hi,

I have created a blog and having a problem in managing the blogs where the user can delete their blogs. I want that if a person logs in, he or she should be able to see only their blogs. For this I am have made the blog table in which the session variable is inserted and have done it successfully. Now I am trying to get the blogs of the person who has logged in. For this I am using the following query:

if (isset($_SESSION['MM_Username'])) {
  $colname_rec_manage_blog = $_SESSION['MM_Username'];
}
mysql_select_db($database_con_reg, $con_reg);
$query_rec_manage_blog = sprintf("SELECT * FROM blog WHERE email.blog = %s", GetSQLValueString($colname_rec_manage_blog, "text"));
$query_limit_rec_manage_blog = sprintf("%s LIMIT %d, %d", $query_rec_manage_blog, $startRow_rec_manage_blog, $maxRows_rec_manage_blog);
$rec_manage_blog = mysql_query($query_limit_rec_manage_blog, $con_reg) or die(mysql_error());
$row_rec_manage_blog = mysql_fetch_assoc($rec_manage_blog);

With this code I am trying to fetch the data of the data of a person whose session variable "MM_Username" is active. Please note that the session variable "MM_Username" contains the email address which should be the login of a user.

Now this code does not give me any error and if I echo the session variable it gives me the email address as well. But it does not give me any records at the end.

Can anyone please help me in this matter. I am stuck up for quite a bit of time, any help will be highly appreciated.

Regards,

Bilal A. Khan

Recommended Answers

All 5 Replies

Hello,
a couple things i noticed.

$query_rec_manage_blog = sprintf("SELECT * FROM blog WHERE email.blog = %s",

unless your joining different tables, just use WHERE email = %s.
Also, if you echo MM_Username and you get joe | joe@blah.com then i would just use two session variable like MM_Username and MM_UserEmail. Because its probably querying WHERE email.blog = joe joe@blah.com. Try that and let me know.

Hello,
a couple things i noticed.

$query_rec_manage_blog = sprintf("SELECT * FROM blog WHERE email.blog = %s",

unless your joining different tables, just use WHERE email = %s.
Also, if you echo MM_Username and you get joe | joe@blah.com then i would just use two session variable like MM_Username and MM_UserEmail. Because its probably querying WHERE email.blog = joe joe@blah.com. Try that and let me know.

Hi,

Thank you very much for the reply. I have tried the way you have suggested and came up with the following code:

if (isset($_SESSION['MM_Username'])) {
  $colname_rec_manage_blog = $_SESSION['MM_Username'];
}
mysql_select_db($database_con_reg, $con_reg);
$query_rec_manage_blog = sprintf("SELECT * FROM blog WHERE email = %s", GetSQLValueString($colname_rec_manage_blog, "text"));
$query_limit_rec_manage_blog = sprintf("%s LIMIT %d, %d", $query_rec_manage_blog, $startRow_rec_manage_blog, $maxRows_rec_manage_blog);
$rec_manage_blog = mysql_query($query_limit_rec_manage_blog, $con_reg) or die(mysql_error());
$row_rec_manage_blog = mysql_fetch_assoc($rec_manage_blog);

But it is still not working. My blog table has following fields:

  • blog_id
  • title
  • message
  • time
  • email

When I try to show the records, it gives me an empty table.

I believe I am still on the right track and the code is valid. Can you please help me in this and make some suggestions?

Regards,

Bilal A. Khan

Member Avatar for diafol

How about echoing the query out and then copying it and pasting to phpmyadmin? It'll give you the error.

Enter an email for the "%s" and see if you get a return.

SELECT * FROM blog WHERE email = %s

If you do, then again you have to use 2 session names; username and email. Having 1 session name that echo username and email will not suffice.

Enter an email for the "%s" and see if you get a return.

SELECT * FROM blog WHERE email = %s

If you do, then again you have to use 2 session names; username and email. Having 1 session name that echo username and email will not suffice.

Hello Again,

Sorry for the late reply. I got caught up with some other work. Alright, I tried the code three different ways:

First:

I used the following code:

if (isset($_SESSION['MM_Username'])) {
  $colname_rec_manage_blog = $_SESSION['MM_Username'];
}
mysql_select_db($database_con_reg, $con_reg);
$query_rec_manage_blog = sprintf("SELECT * FROM blog WHERE email = email");
$query_limit_rec_manage_blog = sprintf("%s LIMIT %d, %d", $query_rec_manage_blog, $startRow_rec_manage_blog, $maxRows_rec_manage_blog);
$rec_manage_blog = mysql_query($query_limit_rec_manage_blog, $con_reg) or die(mysql_error());
$row_rec_manage_blog = mysql_fetch_assoc($rec_manage_blog);

This code gave me all the records. But it is not what I am looking for

Second:

I used the following code:

if (isset($_SESSION['MM_Username'])) {
  $colname_rec_manage_blog = $_SESSION['MM_Username'];
}
mysql_select_db($database_con_reg, $con_reg);
$query_rec_manage_blog = sprintf("SELECT * FROM blog WHERE email ='".$_SESSION['MM_Username']."'");
$query_limit_rec_manage_blog = sprintf("%s LIMIT %d, %d", $query_rec_manage_blog, $startRow_rec_manage_blog, $maxRows_rec_manage_blog);
$rec_manage_blog = mysql_query($query_limit_rec_manage_blog, $con_reg) or die(mysql_error());
$row_rec_manage_blog = mysql_fetch_assoc($rec_manage_blog);

This does not show any record what so ever and the record table comes out be empty.

Third:

Another piece of code as under which I tried as well:

if (isset($_SESSION['MM_Username'])) {
  $colname_rec_manage_blog = $_SESSION['MM_Username'];
}
mysql_select_db($database_con_reg, $con_reg);
$query_rec_manage_blog = sprintf("SELECT * FROM blog WHERE email ='".$colname_rec_manage_blog."'");
$query_limit_rec_manage_blog = sprintf("%s LIMIT %d, %d", $query_rec_manage_blog, $startRow_rec_manage_blog, $maxRows_rec_manage_blog);
$rec_manage_blog = mysql_query($query_limit_rec_manage_blog, $con_reg) or die(mysql_error());
$row_rec_manage_blog = mysql_fetch_assoc($rec_manage_blog);

Now this is interesting, I have tried the variable which in which $_SESSION is stored. When I echo this variable, it shows me the email address as an output, but when I am putting it as a condition in SELECT statement, it gives me no result what so ever.

Logically it should give me the result.

Can you please elaborate what do you mean by two different session variables? I am taking email as a login only. If you mean I should take a user name as login. Is that what u mean by your suggestion?

Thank you very much for helping out so far. I will be highly obliged if you keep on helping me through this problem.

Regards,

Bilal A. Khan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.