Jump to content

[SOLVED] Quick database question


proxximo

Recommended Posts

Hello,

I am having a problem with one of my scripts and cant figure out exactly what is goin wrong. I am trying to call a certain database table using a variable value. Here is the code I am having problems with.

if ($userrow["currentarea"] == "Outside Kings Fortress") {
$monsterdb = "townmonster1";
}


        $monsterquery = doquery("SELECT * FROM {{table}} ORDER BY RAND() LIMIT 1", "$monsterdb");

I know that this is the problem in the script as I have added the townmonster1 to the query itself and the script works,

        $monsterquery = doquery("SELECT * FROM {{table}} ORDER BY RAND() LIMIT 1", "townmonster1");

so i am guessing that calling the table using a variable is the problem. Does anyone know how to fix this? or know a snippet of code that could help?
Link to comment
Share on other sites

I did hard code it and it worked fine, thats why I know its this part of the script that is the problem. But I do need to use a variable at this stage because I have about 250 different tables that use this script and didnt want to rewrite the 500 lines of code for each of the 250 tables :(
Link to comment
Share on other sites

Personally I always suggest storing any dynamic-ish queries in a string before processing.

For example:

$query = "SELECT * FROM sometable";
$result = mysql_query($query);  //or in this case doquery($query);

The reason being when you are trying to debug the script you can simply do an echo $query to make sure the query you are attempting to send is in fact what is generated.
Link to comment
Share on other sites

Here is my doquery function

function doquery($query, $table) { // Something of a tiny little database abstraction layer.
   
    include('config.php');
    global $numqueries;
    $sqlquery = mysql_query(str_replace("{{table}}", $dbsettings["prefix"] . "_" . $table, $query)) or die(mysql_error());
    $numqueries++;
    return $sqlquery;

}


so my setup looks like this

$monsterquery = doquery("SELECT * FROM {{table}} ORDER BY RAND() LIMIT 1", "$monsterdb");

I already have the variable filled by the time it gets to this point
Link to comment
Share on other sites

Store that query in a variable before calling doquery, echo it and make sure it's what you think it is. If it is, take the query and run it in phpMyAdmin or some similar tool to find out if the query works as expected.
Link to comment
Share on other sites

[quote author=dbo link=topic=120609.msg495055#msg495055 date=1167709968]
Personally I always suggest storing any dynamic-ish queries in a string before processing.

For example:

$query = "SELECT * FROM sometable";
$result = mysql_query($query);  //or in this case doquery($query);

The reason being when you are trying to debug the script you can simply do an echo $query to make sure the query you are attempting to send is in fact what is generated.
[/quote]

I store in the next line, the error that I am getting is that the table doesnt  "pod_ " (In this case it should be "pod_townmonster1")  doesnt exist, which means the function isnt in fact taking the info contained in the variable.
Link to comment
Share on other sites

if ($userrow["currentarea"] == "Outside Kings Fortress") {
$monsterdb = "townmonster1";
}


        $monsterquery = doquery("SELECT * FROM {{table}} ORDER BY RAND() LIMIT 1", $monsterdb);
        $monsterrow = mysql_fetch_array($monsterquery);
Link to comment
Share on other sites

Debug how you wish. But I echo line by line the variables so I can found out exactly where it goes haywire. I would do it as I suggested before the doquery call, once it gets into the doquery function, and then right before it attempts to actually execute the query, but the mysql error is helpful as well.
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.