PDA

View Full Version : How to code this in mysql and php?


butsags
10-16-2009, 05:44 PM
Okay so i has a start already.


$query = mysql_query("SELECT * FROM inventoryitems WHERE characterid = '$id' AND inventorytype = -1 AND position = -101");
while($row = mysql_fetch_array($query)){
$equipt = $row[itemid];
echo "
<img src=\"http://www.mapletip.com/images/maplestory-monsters/0$equipt.png\">";
}



I would like to somehow say, if there is no item that qualifies the specifications of $query , then to show <img src="images/none.png">


sorry i barely know any mysql implementing

i guess it would have to be something like

IF $query amount > 0 {
echo "
<img src=\"http://www.mapletip.com/images/maplestory-monsters/0$equipt.png\">
";
} else {
echo "
<img src=\"images/none.png\">";
}


but i just don't know how to correctly say that.

I would be extremely grateful for any help.

thanks

Fumigator
10-16-2009, 06:09 PM
The function you're looking for is mysql_num_rows(), it returns the number of rows in a query's resultset.

http://us3.php.net/manual/en/function.mysql-num-rows.php

onmoon
10-20-2009, 11:18 AM
this code may be faster than using mysql_num_rows():

$query = mysql_query("SELECT * FROM inventoryitems WHERE characterid = '$id' AND inventorytype = -1 AND position = -101");
if($row = mysql_fetch_array($query))
{
do
{
$equipt = $row[itemid];
echo "<img src=\"http://www.mapletip.com/images/maplestory-monsters/0$equipt.png\">";
}while($row = mysql_fetch_array($query));
}else
{
echo "
<img src=\"images/none.png\">";
}

Fumigator
10-20-2009, 05:19 PM
this code may be faster than using mysql_num_rows():

$query = mysql_query("SELECT * FROM inventoryitems WHERE characterid = '$id' AND inventorytype = -1 AND position = -101");
if($row = mysql_fetch_array($query))
{
do
{
$equipt = $row[itemid];
echo "<img src=\"http://www.mapletip.com/images/maplestory-monsters/0$equipt.png\">";
}while($row = mysql_fetch_array($query));
}else
{
echo "
<img src=\"images/none.png\">";
}

Meh, I prefer clarity over an insignificant savings in processor time. Plus I doubt a call to mysql_num_rows() would be measurably slower than a call to mysql_fetch_array() that doesn't return a row.

onmoon
10-21-2009, 12:42 AM
Meh, I prefer clarity over an insignificant savings in processor time. Plus I doubt a call to mysql_num_rows() would be measurably slower than a call to mysql_fetch_array() that doesn't return a row.

We don't care about the exact number, what we care is yes or no. If there's not many records in this database, it should be no big difference between them. However, in case there could be a lot of records, then, we will have to fetch every array anyway, and we don't need to call mysql_num_rows(), which could exhaust lots of resources if there is a huge table.

Thus, actually, unless it is an empty table, I still prefer to "do.. while".