Uutiset:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu
Advertisement:

Query only calls one column

Aloittaja Jamora1124, elokuu 08, 2013, 08:34:46 IP

« edellinen - seuraava »

Jamora1124

$result = $smcFunc['db_query']('', '
SELECT * FROM {db_prefix}rpg_characters
WHERE character_id = {int:c_id}
LIMIT 1',
array(
'c_id' => $character_id,
)
);
while($row = $smcFunc['db_fetch_assoc']($result)
echo $row['l_name'];


For some reason, this query only allows me to call one column of data from the table. I want to be able to pull all of the data out of the row that has said id, such as $row['avatar'], $row['f_name'] and so on. However, it seems that the only way to do so is to query again and again for each column that I want to use out of the database. I have no problem doing so however, there MUST be some easier way to do this. Any takers?

margarett

You should print_r($row) since it is an array (if it successfully fetches all the columns, as it should...

Maybe you can try like this:

$myarray = array();
// fill the array
while ($row = $smcFunc['db_fetch_assoc']($result))
  $myarray[] = $row;

$smcFunc['db_free_result']($result);
print_r($myarray);

Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

MrPhil

SELECT * should be returning all the columns defined for the table, as row elements. LIMIT 1 limits you to 1 row, so your while loop will only go once. You're not confusing rows and columns, are you? I also see a missing ) in the while statement, so you're lucky to get even one element (l_name) printed out.

Jamora1124

I found the error. Apparently, I forgot to add some {} brackets before and after while. I forgot that while() was a function. Made a noob mistake. This is how my code looks like now.

$result = $smcFunc['db_query']('', '
SELECT * FROM {db_prefix}rpg_characters
WHERE character_id = {int:c_id}
',
array(
'c_id' => $character_id,
)
);
while($row = $smcFunc['db_fetch_assoc']($result))
{
$hppercent=$row['hp']/$row['mhp']*100;
$mppercent=$row['mp']/$row['mmp']*100;
echo "<p align='right'><b>" .$row['l_name']. " " .$row['m_name']. " " .$row['f_name']. "</b>";
echo "<br> Level " .$row['level']. " " .$row['race']. " " .$row['class']. "</p>";
echo "<p align='left'><img src='".$row['avatar']. "' width='75' height='75' style='border: 1px solid #000'>";
echo "<div id='healthbar'><div style='width:" .$hppercent. "%'></div></div><br><div id='magicbar'><div style='width:" .$mppercent. "%'></div></div></p>";
}

MrPhil

"while" is not a function. It is a control statement:
while (boolean-test) single-or-compound-statement

You might be confused by the ($result) part, which is actually part of the function $smcFunc['db_fetch_assoc']($result). It returns the next row of data (if there is more) and assigns it to $row, or NULL if there is no more data in $result. If there is data, while() interprets it as TRUE and makes a pass through the loop, and if no data (NULL), while() interprets it as FALSE and quits the while loop.

Advertisement: