mysql_query question


I’m building my first php application (nothing fancy or spectacular, just something to do to test myself). I have a question that doesn’t seem to be addressed anywhere I’ve looked.

Every example I’ve seen uses the mysql_query function as follows:

$stringname = mysql_query(‘query goes here’);

But my understanding of a string is that it’s a container. So if you’re using mysql_query to, for example, insert data to a table, where you’re not really concerned with storing any results, is $stringname necessary? I’ve never seen an example without some sort of string attached to it. Will mysql_query execute properly without such a string attached?

Thanks for your help.

I don’t know where u got these examples form.
But all examples i know use is as this:
$result = mysql_query(‘query goes here’);

This creates a result-id wich may be used to fetch the data on a SELECT or get the last-inset-id of an INSERT.

U are 99% right.
the 1% ur wrong about:
even on an INSERT MySQL returns data like the execution time of that query or the value that was created by an auto-increment field. Thats why u my use $result in mysql_info() as well.

but as u said: u may not need any return value. in that case u may just use:
[php]mysql_query(‘INSERT INTO table SET field=“value”’);[/php]
then the result id is lost, but the query will still work fine.

but, my best suggestion for beginner: just try. the worse that may happen is that it is not working.

Excellent point about the examples. Most examples do say $result. Which brings me to another question: is $result required, or is it recommended. I ask because there are a series of sql queries in my script, and I’m concerned about using the same variable for multiple queries in a single script.

And thank you for that answer. I may eventually ask fo help with the script itself, but for now I just want to make sure I understand what I’m doing.

Taking your other piece of advice, I’ll try testing it with something other than $result to see what happens.

I guess ur new to programming at all, not just PHP. If i’m wrong please forgive me to explain some programming basics here.

$result is just a variable, as known from mathematics. it’s a name used for a value it is representing. Actually u should never use the same variable to store different values (there are a lot of exceptions [e.g. $i is used for for-loops over and over again as long as they are not nested]). Of cause u may reassign the already used variable agein if u are 100% sure the old one is not needed anymore.

my suggestions on the $result variable (in example scripts it will always be called $result):

  1. leave it away if u’ll never use it
  2. give it a more accurate name. e.g. $members_insert_result (write more, think less :)
  3. as soon as u have some practice u may get to the point where u only call it $r, but if u used my suggestion 2 before u’ll know why u do it.
  4. most PHP-scripters use a function to get there select results (variables inside a function get lost after the function is done, in there $result the best name to use)

to get make this more practical:

[php]mysql_query(‘INSERT …’);
mysql_query(‘INSERT …’);
mysql_query(‘INSERT …’);
$select_members_result=mysql_query('SELECT * FROM members');
/* do something with $select_members_result e.g. $member=mysql_fech_assoc($select_members_result); /
mysql_query(‘INSERT …’);
$select_admins_result=mysql_query('SELECT id, name FROM admin');
do something with $select_admins_result e.g. $admin=mysql_fech_assoc($select_members_result); */
mysql_query(‘INSERT …’);
mysql_query(‘INSERT …’);
mysql_query(‘INSERT …’);
mysql_query(‘INSERT …’);
mysql_query(‘INSERT …’);
mysql_query(‘INSERT …’);[/php]

It could be said that I’m new to programming. I learned BASIC and COBOL in high school in the early 80s, then didn’t touch a computer until 1997, when I started playing with HTML. PHP is my first programming language since I forgot everything I learned in high school.

I very much appreciate your help. I think I knew at some point about variables, but it’s good to have it confirmed, and also to know that $result isn’t a special php-specific reserved thing.

One more.

There are rules for naming variables:

  • the names have to be unambiguous
  • if u use more then one word u should ether use a underscore or a capital letter: $twoWords or $two_words
  • the should be as short as possible (as long as they satisfy the previous rules)
  • the most impotent is that a variable name must never need documentation

P.S.: PHP has hardly any preserved things, i think $GLOBALS, $_…, and lots of constants (not stating with $) are nearly all.
on everythin else u’ll get a PHP error (e.g. if u try to redefine function strlen() {...})

the question wasn’t mend to put u down. it was just to be careful. (e.g. i’m programing since i was 10 years old; i anybody would tell me something about variables i would have problems to except anything else he’ll write [as if i would have told u the break is the middle petal]).

Of cause i have seen that u have a programming-kind-of-brain. that was the reason why i was so careful on posting programming basics.

i’m just glad ur question about whether $request is something special was answered as well.

the question wasn’t mend to put u down. it was just to be careful.

Not to worry — I didn’t feel put down at all. Having forgotten everything I once knew, I may as well be new to programming.

Again, thank you for your help. I like understanding what I’m doing, which is a bit different from knowing what I’m doing, and you’ve helped me to that end

Sponsor our Newsletter | Privacy Policy | Terms of Service