Jump to content


Photo

A few quick questions...


  • Please log in to reply
6 replies to this topic

#1 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 03:04 PM

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?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#2 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 13 July 2006 - 03:06 PM

im not too sure but i would guess that they are already built in as function in your database program as i had a similar problem...

#3 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 03:33 PM

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?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#4 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 13 July 2006 - 03:50 PM

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:
while ($row = mysql_fetch_array($result, MYSQL_NUM) {
   $row[0] = $name;
}

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.

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 July 2006 - 03:57 PM

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

like so: `some_name`

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:
while ($row = mysql_fetch_array($result, MYSQL_NUM) {
   $row[0] = $name;
}
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];

#6 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 04:03 PM

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

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#7 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 05:19 PM

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:

<?php
$_SESSION['name'] = 'Someone You Wish You Knew';
$hello = "Hi, I'm {$_SESSION['name']}.";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users