• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

php constants...

harrylmh

New Member
Is there any way to use constants in mysql queries? I tried, but it couldn't work.

define("NAME","jane");
$data = mysql_fetch_object(mysql_query("select stuff from main where user=" . NAME));
echo $data->stuff;

should I forget about this and just use variables, or is there a way to use constants in queries correctly?


Thanks in advance.:)
 
I think that variables work best. In that mysql query you are telling it that the user is name. You would need to use something like $name because otherwise if there is a user that is called name then it will echo name. So if $name was something it would echo something. Here is what the correct mysql query should be.

PHP:
$data = mysql_query("SELECT stuff FROM main WHERE user=$name");
while($data1 = mysql_fetch_object($data))
echo $data1->stuff;

I also just noticed that you placed " before the end of the mysql query. It should be at the end. I also used the while command in mine as its the way I do it. You don't have to do it that way though (I think).
 
Thanks for the tips cheatpark. ;)

I didn't know I could use the while command in this way, though I thought that using it in this way would cause an infinite loop. :confused:
 
Maybe that way is better but the while command works fine for me. I am using it in a forum I am coding and haven't got any problems. I don't think there should be any problems with your way of doing it as long as you use a variable and put the " in the right places.
 
Originally posted by harrylmh
Is there any way to use constants in mysql queries? I tried, but it couldn't work.

define("NAME","jane");
$data = mysql_fetch_object(mysql_query("select stuff from main where user=" . NAME));
echo $data->stuff;

should I forget about this and just use variables, or is there a way to use constants in queries correctly?


Thanks in advance.:)

Try replacing the one line with ...

$data = mysql_fetch_object(mysql_query("select stuff from main where user=" . ${NAME}));
 
Cheatpark:
I think the while command method you're using is good in that it doesn't print/do anything if there is no record found. I tried taking away the while, and there were errors when I searched for something that doesn't exist.

For the method I'm using, to prevent such errors from happening, I do this:
if (!$data = mysql_fetch_object(mysql_query("SELECT stuff FROM main WHERE user=$name")))
//error message here
else {
//carry on
}


ashben:
It couldn't work. It gave an error saying undefined variable. However, I discovered that it works if I do this:
$data = mysql_fetch_object(mysql_query("select stuff from main where user='" . NAME . "'"));
//adding the single quotes
So I finally got the constants to work in queries. :)

PS: Does your code work in your script(s)?

Thanks!
 
if (!$data = mysql_fetch_object(mysql_query("SELECT stuff FROM main WHERE user=$name")))
//error message here
else {
//carry on
}

Thats wrong
because name is a string and not a integer you need quotes:
PHP:
$data = mysql_fetch_assoc(mysql_query("select <blah> from <blah> where user=\"$name\"") or die(mysql_error())

that should work
 
Last edited:
Back
Top