Jump to content

A really simple php MySQL question


dannerz

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.