Log in

View Full Version : $sql not returning results, this can't be this tough..



cspgsl
05-05-2006, 05:25 PM
I am new to php and have run into a brick wall. Any help would be appreciated

I need an update form on a page that will look to see if anything is entered in the users "welcome" field in their record and, if there is content, display it for editing / updating.

If there is nothing in the user's field enter "My Default Text" and use that to update the user's welcome field in their record.

Thus far I have the following.

In the login form


<input type="submit" name="kt_login1" id="kt_login1" value="Login" />

In the page that opens as a result of logging in:


<? session_start();
if (isset($_POST['kt_login1'])):
$_SESSION['kt_login_user'] = $_POST['kt_login_user'];
$_SESSION['kt_name'] = $_POST['kt_name'];
endif;
?>
<p>Hello <? echo $_SESSION['kt_name'] ?> Choose a link to update a part of your records...</p>

In the page that is to contain the update record form:

<? session_start();
if (!isset($_SESSION['kt_login_user'])):
$_SESSION['kt_login_user'] = "Anonymous";
$_SESSION['kt_name'] = $_POST['kt_name'];
$_SESSION['kt_welcome'] = $_POST['kt_welcome'];
endif;
?>
<p>Hello <? echo $_SESSION['kt_name'] ?> Yada yadda...</p>
<?php $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
$qry = mysql_query($sql);
if(@mysql_num_rows($qry) > 0)
{
while($r = mysql_fetch_array($qry))
{
$kt_welcome = $r[1];
}
}
else
{
$kt_welcome = "My default text";
}
?>
<textarea name="welcome" cols="80" rows="20"><?php echo $_SESSION['kt_welcome']; ?></textarea>
<?php mysql_close(); ?>

I have created 2 users, one with content in the welcome field of their record and the other with a blank welcome field in the welcome field of their record.

What is happening at the moment is
1 - The "echo $_SESSION['kt_name" is returning the name of the logged in user (as expected)
2 - the only thing that is being returned in the textarea is My default text. The contents of the user with pre-existing data in his welcome field is not being returned in spite of being logged in.

I am coming to honestly believe that PHP was developed to drive me nuts.

I would appreciate some direction here. Many thanks in advance.

Twey
05-05-2006, 05:43 PM
Firstly, it will save you a lot of headaches to
error_reporting(E_ALL);at the top of your page.
Secondly, do you get results when executing this query from a simple MySQL prompt?

djr33
05-05-2006, 05:51 PM
Here's what I'd suggest. There are some loose rules on some of this, so what I'm suggesting will work, but I can't say what you have right now won't.
I'm just gonna try to clean up the syntax like I'm used to it.. then we'll see if that fixes the problem.

<?php $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
--Use backticks (``) (above tab) instead of single quotes for 'welcome'.
>>`welcome`
You also might want to put some single quotes around $kt_login_user, as that will make it a quoted string when sent to mysql to be parsed.
I don't know if/think either of this is absolutely required, but it might be giving you a bug.

while($r = mysql_fetch_array($qry))
--Personally, I like mysql_fetch_assoc(), as it gives you the same thing, but $r will work like $r['username'], not $r[1]. Just makes a bit more sense.
Might help you track stuff a bit better. Then again, with only getting one thing, not hard anyway. (The default for some of this stuff is 0, though. Like the first character in a string, $string, can be found using $string[0], not $string[1]. Might be related, but I think mysql_fetch_array does start with 1, to make it more user friendly.)



And, ok. here's your error.
$kt_welcome = $r[1];
...
<?php echo $_SESSION['kt_welcome']; ?>
>>>you've called it like you stored it as a session variable.
Just use:
<?php echo $kt_welcome; ?>
instead of the other echo tag.

That should work.
That WILL work, but I can't say it's the only error... but... make sure you do change that.

If not, add echo statements after every if and query, etc. (echo "it got to if ....") just so you can track exactly where the error happens.

cspgsl
05-05-2006, 07:10 PM
error_reporting(E_ALL);
first returned the error that kt_login_user was not defined. I placed it between ' ' and the error dissapeared. Thanks for that tip, I'll make it standard.

As for the query (done in PHP MyAdmin)

SQL query:
SELECT `welcome`
FROM `users`
WHERE 1
LIMIT 0 , 30

returns (edited for brevity)


id welcome
34 text in Bill
37 NULL

cspgsl
05-05-2006, 07:17 PM
echoing $kt_login_user returns the username

Twey
05-05-2006, 07:22 PM
first returned the error that kt_login_user was not defined. I placed it between ' ' and the error dissapeared.Of course it will. You're now using the literal string '$kt_login_user'.

Also, I see no actual connection attempt. I suggest:
<?php $sql = "SELECT `welcome` FROM `users` WHERE `id` = ". $_SESSION['kt_login_user'] . ";";
$conn = mysql_connect("localhost", "user", "password"); // change if necessary.
$qry = mysql_query($sql);

djr33: I'm sure I've told you before that mysql_fetch_array() returns an array with both associative and numerical indices. I think you're confusing it with mysql_fetch_row(). Both mysql_fetch_row() and mysql_fetch_assoc() are pointless aliases, since mysql_fetch_array() can be used to return either a purely numerical or a purely associative array by specifying MYSQL_NUM or MYSQL_ASSOC as the second argument, respectively.

djr33
05-05-2006, 10:34 PM
It's less to type to use the right function the first place, not add a condition to another one. Never needed numbers myself... so... yeah. Whatever, though... that's fine too.

The connection attempt... good point. Maybe he took it out though.

And, here's the answer, as said above:

And, ok. here's your error.
$kt_welcome = $r[1];
...
<?php echo $_SESSION['kt_welcome']; ?>
>>>you've called it like you stored it as a session variable.
Just use:
<?php echo $kt_welcome; ?>
instead of the other echo tag.

Twey
05-05-2006, 10:49 PM
Nuh-uh, you seem to have missed:
$_SESSION['kt_welcome'] = $_POST['kt_welcome'];

djr33
05-05-2006, 10:51 PM
Oh, ok. Right.

Perhaps it's still relevant, though?
Maybe they should both get set to the same variable so that's what is displayed? Are they intended to be the same thing?