pixy Posted July 13, 2006 Share Posted July 13, 2006 Okay, so I'm using sessions to remember a user's username.At the top of every page I havesession_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? Quote Link to comment https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/ Share on other sites More sharing options...
brown2005 Posted July 13, 2006 Share Posted July 13, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57352 Share on other sites More sharing options...
pixy Posted July 13, 2006 Author Share Posted July 13, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57374 Share on other sites More sharing options...
Koobi Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57390 Share on other sites More sharing options...
wildteen88 Posted July 13, 2006 Share Posted July 13, 2006 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]; Quote Link to comment https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57393 Share on other sites More sharing options...
pixy Posted July 13, 2006 Author Share Posted July 13, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57399 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14489-a-few-quick-questions/#findComment-57454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.