Jump to content

Recommended Posts

foreach($players as $player)
            {
                ?>

                <a href="user.php?user=<?php echo htmlentities($player['playerName'], ENT_NOQUOTES); ?>" target="_self" class="tableRow">
                    <span class="rankColumn">
                    <span class="Olde"><?php echo number_format($x); ?></span>
                    </span>
                    <span class="nameColumn">
                    <span class="Olde"><?php echo htmlentities($player['playerName'], ENT_NOQUOTES); ?></span>
                    </span>
                    <span class="levelColumn">
                        <span class="Olde"><?php echo number_format($player[$this->whatSkill($skill).'lvl']); ?></span>
                    </span>
                    <span class="xpColumn">
                    <span class="Olde"><?php echo $player[$this->whatSkill($skill).'xp']; ?></span>
                    </span>
                </a>

                <?php
                $x++;
            }

 

GPPI

 

I'm trying to rewrite a website script for someone, without modifying the database. Sadly they did this kind of badly.

 

Here you can see:

 

<?php echo number_format($player[$this->whatSkill($skill).'lvl']); ?>

 

Works perfectly. The level is outputted. But for some reason, this:

 

<?php echo $player[$this->whatSkill($skill).'xp']; ?>

 

Only outputs text, and not the XP (See example picture). Any idea as to why this line of code doesn't work but the other does?

Link to comment
https://forums.phpfreaks.com/topic/265386-extracting-data-from-database/
Share on other sites

Seems the database is returning the text:

 

[Fishingxp] => Fishingxp
[2] => Fishingxp

 

I'm not exactly sure why it's returning that. Looking at my query, everything seems to be fine:

 

case 11:
                return $this->database->processQuery("SELECT `playerName`,`Fishinglvl`,'Fishingxp' FROM `skills` ORDER BY `Fishinglvl` DESC,`Fishingxp` DESC LIMIT $start,$per", array(), true);
                break;

 

GQU9

 

GQUs

You code is apparently storing the key as the value, rather than the value as the value.

 

Do you have any idea on how it's happening? I see nowhere in my code where a mixup like that is possible.

 

Edit: I found the problem, but not the solution.

 

return $this->database->processQuery("SELECT `playerName`,`Cookinglvl`,'Cookingxp' FROM `skills` ORDER BY `Cookinglvl` DESC,`Cookingxp` DESC LIMIT $start,$per", array(), true);

 

Is my code. Whenever I switch the places of Cookinglvl and Cookinglvl (in this instance, the switch make the query: `playerName`,`Cookingxp`,`Cookinglvl`), the Cookinglvl stops working and Cookingxp starts to work. Any idea of why this is?

The problem is probably in the code in the processQuery method.

 

I've used my method on other projects bigger than this one and have never encountered such a problem:

 

public function processQuery($query, array $binds, $fetch)
    {
        $query_handle = $this->dbc->prepare($query);
        if(!$query_handle->execute($binds))
        {
            $error = $query_handle->errorInfo();
            echo $error[2];
}
        
        //update insertId var
        $this->insertId = $this->dbc->lastInsertId();
        
        //incase we ever want to get the number of rows affected
        //we set our row_count variable to the number of rows
        //affected
        $this->row_count = $query_handle->rowCount();
        
        if($fetch)
        {
            return $query_handle->fetchAll();
        }
    }

in this queries:

                return $this->database->processQuery("SELECT `playerName`,`Fishinglvl`,'Fishingxp' FROM `skills` ORDER BY `Fishinglvl` DESC,`Fishingxp` DESC LIMIT $start,$per", array(), true);

 

    and

return $this->database->processQuery("SELECT `playerName`,`Cookinglvl`,'Cookingxp' FROM `skills` ORDER BY `Cookinglvl` DESC,`Cookingxp` DESC LIMIT $start,$per", array(), true);

 

do you realize that you are enclosing the fields Fishingxp and Cookingxp in single quotes instead of backticks?.... is that what you want to do?

in this queries:

                return $this->database->processQuery("SELECT `playerName`,`Fishinglvl`,'Fishingxp' FROM `skills` ORDER BY `Fishinglvl` DESC,`Fishingxp` DESC LIMIT $start,$per", array(), true);

 

    and

return $this->database->processQuery("SELECT `playerName`,`Cookinglvl`,'Cookingxp' FROM `skills` ORDER BY `Cookinglvl` DESC,`Cookingxp` DESC LIMIT $start,$per", array(), true);

 

do you realize that you are enclosing the fields Fishingxp and Cookingxp in single quotes instead of backticks?.... is that what you want to do?

 

...........

 

facepalm.jpg

 

Thank you. How you noticed that, I'm not too sure. Once again, thank you.

When the database/table/field name is one of the MySQL reserved words, or contains special characters.

 

Well I use to forget a lot of the reserved words and usually generated some errors that took me a while to locate. So, I moved on to just using `.

 

I'll try and cut back on my usage of them.

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.