MySql table/variable questions

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
GandolfMatt
Forum Newbie
Posts: 5
Joined: Fri Aug 08, 2008 1:45 pm

MySql table/variable questions

Post by GandolfMatt »

Hey, I'm new to this, but I'm wondering about a few things.

(1) In a table, 1 field is equal to 1 variable?
(2) Would I be able to change the field name and it's data in the php script?
(3) Would it be faster to save my variables in a text file on my domain or in a field on MySql?
(4) How do I receive a data field from MySql using php?
(5) How do I write to a data field from php?
(6) If someone accessed my php page with " -d usr=Gandolf -d pass=cow " after the .php, how would I use that to affect my php script?
(7) How do I write data to a text file and read data?
(8) How do I create a new text file?
(9) How do I create a new field in MySql from a php script?


Thanks,
-Gandolf
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: MySql table/variable questions

Post by nowaydown1 »

I would encourage you to google around a bit for some PHP tutorials. Specifically, try looking for a tutorial on how to create a simple blog using PHP. That will give you a good starting point if you're new to working with databases.

-----------------------------------------------------------------------------------------------

(1) In a table, 1 field is equal to 1 variable?
* Yes, essentially.

(2) Would I be able to change the field name and it's data in the php script?
* Yes, consult the MySQL manual for UPDATE and ALTER TABLE syntax.

(3) Would it be faster to save my variables in a text file on my domain or in a field on MySql?
* Database will almost always be faster.

(4) How do I receive a data field from MySql using php?
* Assuming you're connected to a database...

Code: Select all

 
$result = mysql_query('SELECT someField FROM table');
if($result) {
    while($row = mysql_fetch_array($result)) {
         echo "This is my field value:" . $row["someField"]; 
    }
} else {
    die('An error occurred: ' . mysql_error());
}
 
Take a look at the mysql_query function in the PHP manual.

(5) How do I write to a data field from php?

Code: Select all

 
mysql_query('UPDATE table SET someField='myrandomvalue' WHERE some_record_id = '1') or die(mysql_error());
 
 
 
(6) If someone accessed my php page with " -d usr=Gandolf -d pass=cow " after the .php, how would I use that to affect my php script?

* Read up on superglobals (http://us2.php.net/manual/en/reserved.variables.php), specifically $_GET / $_POST.

If you had a url like:

Code: Select all

 
http://www.example.com/somescript.php?usr=Gandolf&pw=cow
 
I could access it with something like:

Code: Select all

 
<?php
echo 'Username was: ' . $_GET['usr'];
echo 'Password was: ' . $_GET['pw'];
?>
 
(7) How do I write data to a text file and read data?
* Check out the manual for fwrite (http://us2.php.net/manual/en/function.fwrite.php)

(8) How do I create a new text file?
* With fopen. (fwrite will sort of shed some light on this.)

(9) How do I create a new field in MySql from a php script?
* Check the MySQL manual for ALTER TABLE syntax. (http://dev.mysql.com/doc/refman/5.0/en/alter-table.html)
* You also might be interested in the CREATE TABLE syntax.
Post Reply