dannerz Posted February 11, 2013 Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/ Share on other sites More sharing options...
cpd Posted February 11, 2013 Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411690 Share on other sites More sharing options...
dannerz Posted February 11, 2013 Author Share Posted February 11, 2013 (edited) 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 February 11, 2013 by dannerz Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411694 Share on other sites More sharing options...
Christian F. Posted February 11, 2013 Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411704 Share on other sites More sharing options...
cpd Posted February 11, 2013 Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411705 Share on other sites More sharing options...
dannerz Posted February 11, 2013 Author Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411712 Share on other sites More sharing options...
Barand Posted February 11, 2013 Share Posted February 11, 2013 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 "); Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411713 Share on other sites More sharing options...
dannerz Posted February 11, 2013 Author Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411715 Share on other sites More sharing options...
dannerz Posted February 11, 2013 Author Share Posted February 11, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411719 Share on other sites More sharing options...
Barand Posted February 11, 2013 Share Posted February 11, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411722 Share on other sites More sharing options...
cpd Posted February 11, 2013 Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411723 Share on other sites More sharing options...
dannerz Posted February 11, 2013 Author Share Posted February 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274328-a-really-simple-php-mysql-question/#findComment-1411725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.