Jump to content

A few quick questions...


pixy

Recommended Posts

Okay, so I'm using sessions to remember a user's username.

At the top of every page I have
session_start();
$user = $_SESSION['user'];

Since using $_SESSION['user'] messes mysql queries.

My first question is:
Why do I get errors if I do it this way:
$_SESSION['user'] = $user;

Or when I have something like this...
while ($row = mysql_fetch_array($result, MYSQL_NUM) {
  $row[0] = $name;
}
It gives me an error, but...
$name = $row[0];
is okay!

It's not a big deal, but I'm wondering. I read in a book that people can do variables backwards and it works, so I dont know.

----------------------------------------------------------------------------

My other question:

I tried to query the database with a fields called "to" and "from" and "read" and it gave me errors when I used those field names. I changed them to user_to, user_from, and is_read and it's fine. Why did it mess up when I used those names? They were inside parenthasis, so I wouldn't think it would assume that those are some properties of the table...right?
Link to comment
Share on other sites

Yea, that's what I figured since when I ran the query it used syntax coloring for them differently than the rest of my tables. My question was WHY did it think they were something else when they were inside of parenthasis?
Link to comment
Share on other sites

to answer your first question:
i don't think what you saw was errors. i believe they were more like warnings. you should post your "errors"
warnings are expected your case where you don't seem to have initialized your variables which is considered good practice.

i don't see how this makes sense:
[code=php:0]
while ($row = mysql_fetch_array($result, MYSQL_NUM) {
  $row[0] = $name;
}
[/code]

wouldn't you want to do it the other way around? assigning values works from left to right.




as for the second question, reserved words can't be used in MySQL, as far as i know. so, regardless if you used it with a parenthesis to enclose them or not, it would still cough up since those are reserved words.
Link to comment
Share on other sites

To use "to" and "from" and "read" within an SQL query you should encase them within backticks - `

like so: [b]`some_name`[/b]

Ideally it is good practise to use backticks arround your field/table names within a query, however many people don't do this.

Also this:
[code=php:0]while ($row = mysql_fetch_array($result, MYSQL_NUM) {
  $row[0] = $name;
}[/code]
Will not work as this will assign $row[0] to the value of of $name, which $name probably doesnt exist and so you get a notice error. You should do it the otherway round sit its $name = $row[0];
Link to comment
Share on other sites

Yea, I figured out that I have to switch them, but it never occured to me that it would be a problem.

Thanks for the clarification! :D It was getting frustrating trying to make my messages database and having it give me stupid errors. XD
Link to comment
Share on other sites

for the record, even though using backticks works, i would suggest staying away from reserved keywords regardless just for good practice.

also, the reason $_SESSION['user'] messes up your queries is likely that you're not including it properly.  when you need to echo an array value within a string, use braces to enclose it:

[code]<?php
$_SESSION['name'] = 'Someone You Wish You Knew';
$hello = "Hi, I'm {$_SESSION['name']}.";
?>[/code]
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.