0

I've got a link that "toggles" a div to show and hide in a page called "notifications.php". I want to data in the div to be retrieved from a page called "getnotes.php".

I've succeeded is get the div to show and hide, as well as getting the div to echo whatever the getnotes.php file has but when I try to use SQL to get data in the getnotes.php page it won't get the information.

In the code below I'm simply trying to get the username to display. It will be used for more than retrieving a username but for the purposes of this thread, we're retrieving a username.

notifications.php

//allow sessions to be passed so we can see if the user is logged in
session_start();
ob_start();

//connect to the database so we can check, edit, or insert data to our users table
$con = mysql_connect('SERVER',  'NAME', 'PASSWORD') or die(mysql_error());
$db = mysql_select_db('NAME', $con) or die(mysql_error());

//include out functions file giving us access to the protect() function made earlier
include "./functions.php";

//In the header
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function toggleDiv(divId) {
   $("#"+divId).toggle();
$(document).ready(function(){
         $("#myContent").load("getnotes.php?name=<? echo $username; ?>");
    });
}
</script>

//In the body
<a href="javascript:toggleDiv('myContent');">Notes</a><br>
<div id="myContent" style="display:none; background-color: #aaa; padding: 5px 10px;"> 
</div>



getnotes.php

//allow sessions to be passed so we can see if the user is logged in
session_start();
ob_start();

//connect to the database so we can check, edit, or insert data to our users table
$con = mysql_connect('SERVER',  'NAME', 'PASSWORD') or die(mysql_error());
$db = mysql_select_db('NAME', $con) or die(mysql_error());

//include out functions file giving us access to the protect() function made earlier
include "./functions.php";

//Look up the username and see if they are logged in.
$userid = $_SESSION['uid'];
$lookupusername = mysql_query("SELECT * FROM users WHERE ID='$userid'");
$row = mysql_fetch_assoc($lookupusername);
$username = $row['username'];

echo "you are $username";



Why won't the SQL queries work in getnotes.php?

1
  • 2
    There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. Commented Apr 30, 2013 at 21:06

2 Answers 2

1

$lookupusername = mysql_query("SELECT * FROM users WHERE ID=" + $userid);

But please, use prepared statements.

Sign up to request clarification or add additional context in comments.

6 Comments

I plugged in the code but didn't fix the issue. It gave the error of mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
@derekshull Then maybe $userid is wrong? Try the query with a hard-coded value and output the value of $userid.
I'm not getting a $userid. After hardcoding it, it worked fine. Any ideas?
@derekshull There's eventually nothing in $_SESSION['uid']? From your snippets I can't see where/how/why it's set.
is there a way to pass along the userid from the notifications.php page? That would work but I've not got a clue how to do it.
|
0

Your code looks ok, you probably have a problem somewhere else. Try to print your session:

print_r($_SESSION);

and look if $_SESSION['uid'] has the right user id.

4 Comments

Tried it and it just says Array ( ). I really don't know what's wrong with it. so strange.
As i said, you have problem in another place. Search for a place where you write user id to session (logging area?).
is there a way to pass along the userid from the notifications.php page? That would work but I've not got a clue how to do it.
You already pass user name here: ?name=<? echo $username; ?> (assuming you define $username somewhere). You can easily change this to pass user id (remember to control what you pass to sql query...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.