What's the difference between using "mysql_query" and "$sql"?
| SitePoint Sponsor |
What's the difference between using "mysql_query" and "$sql"?
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com



<sarcasm>
one's a function and one's a variable
</sarcasm>
heh, uhhh do you mean what the difference between:
$result = mysql_query("SELECT * FROM users");
and
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
Is that what your asking?





Yeah.
There are a lot of scripts out there that define the query in a variable (often times called $sql); that's what Adam's asking (right?)
Essentially, Renegade is correct. One is simply a variable, while the other is the actual query command.
It makes it easier (generally) if you use something like:Because you can call what's returned easier by simply returning to the variable $sql (or whatever variable you decide to use).PHP Code:$sql = @mysql_query("select * from whatever");
I usually use variables to define the connection, the database selection, the query strings, and the returned results, just because it makes it so much easier to work with and call back the functions with the variables later in the script.
P.S. Adam, how's the PHP/MySQL coming along? If you need help, just ask me! Two months ago I was just starting with MySQL like you, and a few weeks ago it "just clicked!" I have almost an entire database driven site I'm working on (my district key club site), and I didn't even have to refer back to any tutorials! It's neat!
![]()
Last edited by Aes; Sep 23, 2001 at 01:45.
Colin Anderson
Ambition is a poor excuse for those without
sense enough to be lazy.
Exactly what I meant. O.K., so in: "$result = mysql_query("SELECT * FROM users");", what does the variable "result" hold?Originally posted by Renegade
heh, uhhh do you mean what the difference between:
$result = mysql_query("SELECT * FROM users");
and
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
Is that what your asking?
And in "$sql = "SELECT * FROM users";", what does the variable "sql" hold? I'm still confused.
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com
$sql = "SELECT * FROM users";
$sql is just a variable containing SELECT * FROM users, to run this query you have to use one of the MySQL functions
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
It's actually coming o.k. I'm just confused about some things, but I think I have an o.k. plan ready for databasing Help!4Newbies.Originally posted by Aes
P.S. Adam, how's the PHP/MySQL coming along? If you need help, just ask me! Two months ago I was just starting with MySQL like you, and a few weeks ago it "just clicked!" I have almost an entire database driven site I'm working on (my district key club site), and I didn't even have to refer back to any tutorials! It's neat!
Here it is, what do you think?
Then I will pull out the tuts by Category. The "SigID" column will be connected to a seperate table where I can enter peoples sig. choices.Code:|ID|Category|Date Submitted|SigID|Title|Tutorial| ------------------------------------------------- | | | | | | | | | | | | | | | | | | | | | etc. -------------------------------------------------
Tell me what you think.![]()
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com
ooooh, now I think I get it.Originally posted by seanf
$sql = "SELECT * FROM users";
$sql is just a variable containing SELECT * FROM users, to run this query you have to use one of the MySQL functions
Sean![]()
So in:
$result = mysql_query("SELECT * FROM users");
"result" holds everything from the users table, right?
and then in:
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
"sql" holds the "what to do" part and
"result" holds everything from the users table?
So, what is the advantage of using the second one? The first is shorter.![]()
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com





The second is is easier to change, say if you do multiple queries on one page you can call the $sql variable, if you ever need to change query, you can change the $sql variable. But with the first one you would need to change it all over the script.
That's why I would use it for![]()
I see, thanks!Originally posted by JMulder
The second is is easier to change, say if you do multiple queries on one page you can call the $sql variable, if you ever need to change query, you can change the $sql variable. But with the first one you would need to change it all over the script.
That's why I would use it for![]()
It's all getting to click, I'm excited!
![]()
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com





Heh cool..enjoy
Woah..I actually wrote is is ? Grr..





That looks like it will work well Adam. If you run into any problems, send me a PM or e-mail me. I'll be glad to help in any way!
Colin Anderson
Ambition is a poor excuse for those without
sense enough to be lazy.
Thanks guys!
Actually, one more question. I downloaded phpdev3. When I open mysql.exe, it automatically connects to my localhost. But I want to connect to my sites hosts server. Does anyone know what to do?
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com





You can't use your mysql.exe from your computer to connect, plus it wouldn't be secure.
I use Putty, it's a nifty little SSH1/SSH2 client -- you're server or hosting provider should support it -- give it a try:
http://people.nl.linux.org/~bjs/putty/
Colin Anderson
Ambition is a poor excuse for those without
sense enough to be lazy.




I use the OpenSSH port to Windows by Cygwin.
Here's a package that installs it automatically for you, includes the SSH server as well as client, although you'll probably be only interested in the client:
http://www.networksimplicity.com/openssh/
Well, my host has phpMyAdmin that I can use. Do you think that will be good enough for what I'm trying to do?
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com




phpMyAdmin will probably be OK for most things you wanna do, and may do it quicker as well.
I normally use the SSH shells when I need to use commands not supported by phpMyAdmin, or when I need to import a big database backup, I get timeout errors when I try to do it in phpMyAdmin, but in SSH, I can upload to my server and use the shell to import it.
Next question:
I want to database the site on my own computer first to make sure that I have all of the glitches and stuff worked out before I put it online. Is there a way for me to transfer a database from my computer to my hosts server?
SPF Mentor/Advisor 2001-2003
SPF Designer of the Year 2002
SPF Graphic Designer of the Year 2003
AdamPolselli.com




Yes, you go to command line and from your local server, you go:
mysqldump database_name > databasedump.sql
And in your remote host, you can either use phpMyAdmin to import that database dump, or go:
mysql < '/path/to/databasedump.sql'
or use mysqlimport, another way to do is after loggin into mysql, type:
source '/path/to/databasedump.sql'
Have to create the database first though I think.
You can use phpMyAdmin to do this too, but if your database is big (I think over 7mb) it can cause problemsOriginally posted by swimm5001
Is there a way for me to transfer a database from my computer to my hosts server?
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
Bookmarks