1 Replies - 636 Views - Last Post: 02 November 2014 - 12:09 PM Rate Topic: -----

#1 alexgoaga   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-September 14

Error returning value from array text mysql

Posted 02 November 2014 - 11:33 AM

I have problems in returning an unique number from an array from mysql database table with type 'text'

  $adversar = mysql_fetch_array(mysql_query("select * from creaturi where id = ".$_GET['lupta']));    

   $sansa_sa_pice_drop = rand(1,100);
        if ($sansa_sa_pice_drop < 21 )
        { $drop_creatura = array($adversar['drops']);

            $sansa_cate_sa_pice_drop = rand(1,10);
            if ($sansa_cate_sa_pice_drop < 8)
                $rand_keys = array_rand($drop_creatura, 2);
                $obiect_1 = $drop_creatura[$rand_keys[0]];
                $verifica_obiect_1 = mysql_query("SELECT * from iteme_inventar WHERE id_obiect = ".$obiect_1."");
                $item_obiect_1 = mysql_fetch_array(mysql_query("select * from iteme where obiect = ".$obiect_1." "));
                if (mysql_num_rows($verifica_obiect_1) > 0) 
                mysql_query("update iteme_inventar set cantitate = cantitate+1 where id_obiect = ".$obiect_1." AND id_jucator = ".$citeste['id']);
                else mysql_query("insert into iteme_inventar (id_jucator, id_obiect) value (".$citeste['id'].",".$obiect_1.")");

                echo "<hr/><font color=green>Omorand creatura ".$adversar['nume']." ai descoperit un obiect : <i>".$item_obiect_1['nume_ro']."</i> </font><hr/>";


I dont know where the code is wrong , if ask for echo $adversar['drops'] ; it returns the correct values.


Is This A Good Question/Topic? 0
  • +

Replies To: Error returning value from array text mysql

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Error returning value from array text mysql

Posted 02 November 2014 - 12:09 PM


First of all, that code is messed up in a few different ways.

  • The formatting is horrible. You're just making it harder on yourself (and everybody else, too) by not writing this in a way that is actually legible. I'm not trying to be a pain here. It may seem a minor nuisance, but for anything except the smallest of projects, not adhering to a decent coding style will kill the project - or get you fired, if you're just one dev among many - faster than anything else.

  • You're using the deprecated MySQL API functions. They are ancient, and their use is highly discouraged. Instead, look into PDO or MySQLi.

  • You pass mysql_query results directly into mysql_fetch_array calls. - What if the query fails? What if it doesn't return the expected result set? You have to assume there can be problems with external resource calls like that, or you'll end up regretting it.

I could probably find a couple of more things to nag you about (like using wild-cards in your SELECT queries), but those are the three big issues I see there.

With that said, lets move on to your actual question...

alexgoaga said:

I have problems in returning an unique number from an array from mysql database table with type 'text'

If you are talking about an array, serialized and stored as text, then the reason your code fails is on line #5. What you do there doesn't actually unserialize what's stored; it just puts the whole thing into a new array as a text string, at element 0.

More importantly, you should never, ever, store an array like that. It violates the first "law" of relational databases. When you store complex data structures - like arrays - as text strings, the database is completely unaware of what it's storing, and thus can not manage it. The integrity of the data will be compromised, which WILL lead to problems down the line, assuming your project gets used a fair amount. - Trust me, I've seen it many times. In fact, I've made a good living in the past by replacing developers who have done this, to fix the problems they caused.

I suggest you look up proper database design. I wrote a pretty good tutorial on it over in our Database Tutorials section: Relational Database Design - Normalization.
The rule you are breaking here is what's known as the first normal form (1NF)
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1