Jump to content

executing a query stored in a variable..


php_begins

Recommended Posts

Hi, In some cases I need to execute  a default query , so I am storing the SELECT statement in a variable like this and executing it:

 

$default_query="SELECT * from user where userid='$userid'";
$user_query=mysql_query($default_query);

The above code returns an empty results.

But if I execute it without the variable it works fine.

$user_query=mysql_query("SELECT * from user where userid='$userid'");

 

Am I syntatically wrong somewhere?

Link to comment
Share on other sites

You should be coding with error_reporting and display errors set to full, and all possible de-bugging in place.

 

For queries, you should have.

<?php
error_reporting(-1);
ini_set('display_errors',1);

$default_query="SELECT * from user where userid='$userid'";
$user_query=mysql_query($default_query) or trigger_error($default_query . ' has an error<br />' . mysql_error());

Link to comment
Share on other sites

You would need to post your complete and actual code that does not work.

 

I can tell, based on experience, from the symptom, that you either have some lines of code between where you are setting the variable and where you are using it that is clearing it or your mysql_query statement is in a different program scope from where you set the variable.

 

You also don't have php's error reporting set to E_ALL and display_errors set to ON to get php to report and display all the errors it detects and you don't have any error checking logic on the mysql_query statement to get it to tell you why it is failing.

 

Edit: Which is pretty much exactly what jcbones posted above.

Link to comment
Share on other sites

ok never mind..it was not getting the $userid variable since I was using that variable before I retrived the $userid from the database!

 

Setting the error_reporting and display_errors settings as suggested would have directly pointed out a variable that was not set at the time you referenced it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.