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
https://forums.phpfreaks.com/topic/32495-solved-quick-database-question/
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 :(
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.
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
[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.
if ($userrow["currentarea"] == "Outside Kings Fortress") {
$monsterdb = "townmonster1";
}


        $monsterquery = doquery("SELECT * FROM {{table}} ORDER BY RAND() LIMIT 1", $monsterdb);
        $monsterrow = mysql_fetch_array($monsterquery);
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.