Best way to save/read/mod variables?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

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

Post by 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
;)

Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Post by 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. =(

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

Post by 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
Contact:

Post by 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 ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest