Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Best way to save/read/mod variables?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Best way to save/read/mod variables?

Postby FahDooS » Thu Dec 26, 2002 1:08 pm

I'm EXTEREMLY new to PHP , at this point about 4hours or so, so bare with me...

Whats the easiest way to save/read/mod variables and data on the server side? It would also be great if the variables could include things like arrays, and not just simple strings.

any input would be greatly appreaciated!

...and again, please bare with me, I know this all seems so simple to you pros out there. :?
FahDooS
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Dec 26, 2002 12:58 pm

Re: Best way to save/read/mod variables?

Postby Joan Garnet » Thu Dec 26, 2002 5:53 pm

FahDooS wrote:I'm EXTEREMLY new to PHP , at this point about 4hours or so, so bare with me...

Whats the easiest way to save/read/mod variables and data on the server side? It would also be great if the variables could include things like arrays, and not just simple strings.

any input would be greatly appreaciated!

...and again, please bare with me, I know this all seems so simple to you pros out there. :?

Hi FahDooS,
wellcome to the forums :)

Ok, I guess you are running a Mysql server.
For each option you have to first connect to the database:
Code: Select all
$db=mysql_connect($server , $login , $pass);    
mysql_select_db($database , $db);

- save data -->
Code: Select all
$sql= "INSERT INTO ".$table." VALUES ('$value1','$value2,'$value3)";
mysql_query($sql);

- read data -->
Code: Select all
$sql =  "SELECT * from ".$table." ORDER BY id desc";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
   echo $row["name"]." - ".$row["email"]."<br>";
}

- update data -->
Code: Select all
mysql_query("UPDATE ".$table." SET row_name1='$my_var1' , row_name2='$my_var2' , row_name3='$my_var3' WHERE id='$my_selected_id'");

- erase data -->
Code: Select all
mysql_query("DELETE FROM ".$table." WHERE id='$my_selected_id'");


These are the basics, now you'll have to implement it as you need.

Bye
;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby FahDooS » Fri Dec 27, 2002 5:40 am

Well, it just so happens that my web hosting provider has been down for the last 2 days, and I'm pretty sure I don't have MySQL installed. What are my other options...?

I noticed PHP has some INI methods. If i just made my own few 'get' and 'set' functions to set varibales inside the INI file, it seems like it would be an option.

The only problem I see with that is the storing of arrays. If its not possible to do so, I figured i could make my own 'virtual' array simple by making a INI file with 0=, 1= , 2=, 3=, etc and those could be like the index values of a real array, and could work in a similar fassion.

Any thoughts?
FahDooS
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Dec 26, 2002 12:58 pm

Postby Oleg Butuzov » Fri Dec 27, 2002 6:14 am

Yes, You can store your vars in the text files.

at the your qustions i did nt understend it. =(
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Joan Garnet » Fri Dec 27, 2002 8:59 pm

ok,
if you don't have Mysql you can use .txt files and save, update and erase data as you want.
is not as easy to work as Mysql... but if you have little amount of data, it'll be ok.

Have a look at these functions: fopen fclose. They work with files.
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fclose.php

and string functions to move through text strings:
fpassthru($file) -- output the content of the $file
fgets($file,101) -- reads the first 100 chars of the $file
fgetss($file,101) -- the same as before but reveals the HTML or PHP code as well
fputs($file, $string) -- adds $string at the end of $file
substr( "Joan Garnet" , 0 , 3) -- outputs Joa
substr( "Joan Garnet" , -4 , 3) -- outputs net
strlen("Joan Garnet") -- the length of the string -- this case outputs 11
trim("Joan Garnet") -- ignore whitespaces -- this case outputs JoanGarnet


There are many more,
just go to php.net/manual and have a look.

Bye
;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby FahDooS » Sat Dec 28, 2002 11:43 pm

Ok I decided I want to use MySQL, there is only a $10 setup fee with my hosting service, so not a big deal.

I was wondering if there are any functions already written in PHP which simplify the getting/setting of data. for example:

data_get( 'user', 'varname' ); //will get the value of varname from the user database

data_set( 'user', 'varname', 'value' ); //likewise, this would set that same variable to value

Note that the 'user' name im talking about is acctually sepecific to each person that will be using my pages becuase they will ALL have completely different data. Also, arrays are VERY important to what I wanna do, so hopefully those get and set functions can extract arrays from the mySQL table, or if it had to have functions like data_array_get and data_array_set, that would be fine also.

If anyone knows of a PHP script that already does something like this PLEASE let me know, I'm not highly interested in learning all about mySQL, and a pre-written interface like this would be a great help. If there is nothing out there, would anyone be willing to help me write one :? ? I do know how to program, this is just my first time working with php and mysql, so its still very fuzzy.

THANKS!
FahDooS
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Dec 26, 2002 12:58 pm

Postby Joan Garnet » Sun Dec 29, 2002 2:26 pm

The key is learning at least a little bit what you are doing.
You can't pretend using a language if you don't spend a little time grinding your fingers :D

The code I posted above is the minimum knowledge you have to achieve to interact with Mysql data. 8O

Anyway, if you want you can start asking what you want to do, so we can guide you through these first steps with php-Mysql.

bye ;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.