Jump to content


Photo

A really simple php MySQL question


  • Please log in to reply
11 replies to this topic

#1 dannerz

dannerz

    Member

  • Members
  • PipPip
  • 29 posts

Posted 11 February 2013 - 03:24 AM

I am using "myphpadmin". I think it came with XAMPP.
I do not yet know how to edit MySQL outside of this, except by executing .PHP

I wanted to know how can I select an array then modify it, preferably modify just one integer of the whole array.

I know how to make the arrays, in their most basic form, but once made, I don't know how to do things like make the number go up 10% or add 5 for example.

I have been reading some online tutorials but so far I have not found this one simple thing.

<?php

//And now an attempt at using the array

$con = mysql_connect("localhost",'root','');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM vals1");
echo $result;

//This selects the row
mysql_data_seek($result,0);

$row = mysql_fetch_array($result);
$pt = $row['Vals'];
echo $pt;

mysql_close($con);
?>


That is my code.

mysql_data_seek can go to a specific row in the array. But once there, I don't know how to make it change in a way such as adding or subtracting, or dividing.

Any help will be much appreciated.

#2 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 892 posts
  • LocationLondon, UK

Posted 11 February 2013 - 03:28 AM

Instead of listing things you'd like to do why not describe the specific problem so we can assist in solving it. At the moment you've merely asked how to manipulate arrays but to what end?

Moreover, get away from the MySQL library and start using MySQLi, if you want to know why just read the php docs.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#3 dannerz

dannerz

    Member

  • Members
  • PipPip
  • 29 posts

Posted 11 February 2013 - 03:38 AM

Instead of listing things you'd like to do why not describe the specific problem so we can assist in solving it. At the moment you've merely asked how to manipulate arrays but to what end?

Moreover, get away from the MySQL library and start using MySQLi, if you want to know why just read the php docs.


I will look for MySQLi, (edit : where did you download yours at? Was it hard to install?)

I simply want to know how to manipulate one array so that I can either add to the amount the integer is, or subtract, multiply or divide.

Edited by dannerz, 11 February 2013 - 03:46 AM.


#4 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 11 February 2013 - 06:29 AM

The MySQLI library (collection of functions) is an integrated part of PHP, just like the MySQL library. The PHP manual (php.net/mysqli) contains just about all of the information you should need.

As for manipulating arrays, I (yet again) refer you to the PHP manual: php.net/arrays. ;) Once you know how to reference the correct index/key for the value you want, altering said value is the exact same as if altering a value in a non-array variable.
Keeping it simple.

#5 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 892 posts
  • LocationLondon, UK

Posted 11 February 2013 - 06:44 AM

An array holds many values not 1. Manipulating an array usually means altering its structure not merely taking its values and processing them. So again I ask, what do you want to actually do?

MySQLi is usually downloaded with PHP as one of its extensions but if not you can probably download it from PECL.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#6 dannerz

dannerz

    Member

  • Members
  • PipPip
  • 29 posts

Posted 11 February 2013 - 07:25 AM

Ok I will try to explain differently what I want to actually do.

Let's say I have a table, in MySQL, and it has one array. The field name is Vals. And within Vals, as far as I can tell, there is this one array.
Vals[0]=100
Vals[1]=150

now, I'd like to add 5 to Vals[0]. I can make the 100 appear using a code, but I don't know how to take it and increase it by 5.
I know how to do this:

mysql_query("UPDATE `test`.`vals1` SET `Vals` = '100' WHERE `vals1`.`Vals` =120 LIMIT 1 ;");

What this does, for me, is it checks if Vals[0]=120, and if it is 120, it changes it to 100.

I want to change Vals[0], but I want to increase it, or multiply it, instead of just setting it to a new number.

#7 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,268 posts
  • LocationCheshire, UK

Posted 11 February 2013 - 07:44 AM


mysql_query("UPDATE `test`.`vals1` SET `Vals` = Vals + 5 WHERE `vals1`.`Vals` = 120 LIMIT 1 ");

mysql_query("UPDATE `test`.`vals1` SET `Vals` = Vals * 1.1 WHERE `vals1`.`Vals` =120 LIMIT 1 ");

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#8 dannerz

dannerz

    Member

  • Members
  • PipPip
  • 29 posts

Posted 11 February 2013 - 08:05 AM

mysql_query("UPDATE `test`.`vals1` SET `Vals` = Vals + 5 WHERE `vals1`.`Vals` = 120 LIMIT 1 ");
mysql_query("UPDATE `test`.`vals1` SET `Vals` = Vals * 1.1 WHERE `vals1`.`Vals` =120 LIMIT 1 ");


Thank you very much I will try this out I hope it works it looks like what I wanted.

#9 dannerz

dannerz

    Member

  • Members
  • PipPip
  • 29 posts

Posted 11 February 2013 - 08:21 AM

Ok. Barand's code was exactly what I wanted.

The only problem which remains is this strange one:
I only know how to make ' , not `
But I need to make allot of `

Holding shift key doesn't work. Anyone know how to solve this mystery?

#10 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,268 posts
  • LocationCheshire, UK

Posted 11 February 2013 - 08:27 AM

You rarely need the `.

It's only necessary if the identifier contains space or other special characters or if it a MySql reserved word.

It's usually on the left hand key above the TAB key

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#11 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 892 posts
  • LocationLondon, UK

Posted 11 February 2013 - 08:40 AM

And you really need to work on your terminology.


...
Let's say I have a table, in MySQL, and it has one array.
...


A MySQL database does not contain arrays. PHP generates arrays based on the result set provided by MySQL.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#12 dannerz

dannerz

    Member

  • Members
  • PipPip
  • 29 posts

Posted 11 February 2013 - 08:56 AM

sorry what I meant was recordsets. or rows. I seem to see them as named both of those. But it's like an array to me.

``` <-- and now I know how to make these too.

Ok that is it for me. I will continue to experiment and read more tutorial stuffs.

Thanks allot everyone.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com