/    Sign up×
Community /Pin to ProfileBookmark

upgrade from flat file to MySQL

A while back I asked for some help and got some great advice on setting up a script. It no longer works with my new host as they don’t allow chmod 777

so I need to upgrade to a databse, I guess.

the code I was using looked like the following. could somebody point me in the correct direction to making this work with MySQL?

[code=php]<?php

$ip = getenv(REMOTE_ADDR);

if (isset($_POST[‘submit’])) {

$name = $_POST[‘name’];

$fp = fopen(“$name.txt”,”a”);

if(!$fp) {
echo ‘Error, the file could not be opened or there was an error creating it.’;
exit;
}
if ($rctt > .399 && $rctt < 1.5)
fwrite($fp, $rctt.”n”);
else
{
fwrite($fp, “3 -“);
fwrite($fp, RedLight.”n”);
}

fclose($fp);

}
?>[/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@aussie_girlDec 20.2006 — what about 770 (owner & group) on your public_html folder on your new host, that should work if you still want to use flat files
Copy linkTweet thisAlerts:
@72newbieauthorDec 20.2006 — I just tried it and I have to run a 755, anything else results in either a forbidden or 500 server error page. could I place the flat files off site maybe?
Copy linkTweet thisAlerts:
@sneakyimpDec 20.2006 — do they offer mysql? if not, setting up could be even more difficult than getting your script to work.

looks to me like your script is a bit of security hole because it doesn't screen $_POST['name'] for things like /root/important_file.txt

if the file permissions are correct, it's probably not a big deal but you should know it's a bit dodgy.

they may allow (like aussie girl said) for you to chmod 770. in that case, the file is only writeable by the owner (probably the same username as your ftp login) and its group. my suspicion is that your php server runs as something like 'www-data' or 'apache'. i don't know if you will be permitted but if you were to change the permissions on your files (and the folder that contains them) properly, you could keep using your current method.

a database opens up a lot of options if you are willing to learn. instead of opening a file and writing it you are connecting to a database:

http://php.net/mysql_connect

then you are selecting your database

http://php.net/mysql_select_db

then you are running a query:

http://php.net/mysql_query

your query might be:
[code=php]$sql = "INSERT INTO my_table SET rctt=" . $rctt;[/code]
Copy linkTweet thisAlerts:
@72newbieauthorDec 20.2006 — I am already running two MySQL databases with users cross referenced between the two .

On registration it writes the username and password to both databases but on a new password request I wasn't able to set both passwords the same. But that's a whole 'nother topic I'll let rest.

Could I add a table to the phpbb users table "rctt" and write to that using the phpbb username (I saw a mod to integrate the phpbb info to other pages)?

so I'd add at the top of the page:
[code=php]<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password', 'my_database');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

?> [/code]


and then lower I'd write something like:
[code=php]
$sql = "INSERT INTO phpbb_users WHERE username=".$username." SET rctt=" . $rctt;

[/code]


??? is that correct?

and if later I'd add [code=php]
$time= "SELECT rctt FROM phpbb_users WHERE username=".$username.";
[/code]


am I on the right track?
Copy linkTweet thisAlerts:
@72newbieauthorDec 21.2006 — I'm new to all this and I'm trying to learn. I don't know how to do this or I wouldn't be asking. I know I need to make a table but it asks for stuff like tinyt and terms I don't know. can somebody please help?
Copy linkTweet thisAlerts:
@72newbieauthorDec 21.2006 — Much thanks, will get to studying that real fast!!

Ok, I created a table, pro_tree with the fields(?) name and rctt

name is varchar and rctt is float. I choose float because it's a number from .400 to 1.5

am I on the correct path so far?

then I added to my page:
[code=php]<?php
if $name="";
{
$sql = "INSERT INTO pro_tree SET name=".$name.", rctt=".$rctt.";
}
else
{
$sql = "INSERT INTO pro_tree WHERE name=".$username." SET rctt=".$rctt.";
}
?>[/code]
Copy linkTweet thisAlerts:
@72newbieauthorDec 22.2006 — Ok, I've kinda got it working, one last question.

I am trying to get the averages of all the users as a total.

using:

SELECT AVG(rctt) FROM name

I am getting an error, " Resource id #5 "

and can't figure it out, any ideas?
Copy linkTweet thisAlerts:
@carlhDec 22.2006 — you usually get resource id numbers when you directly reference the variable assigned to a sql statement

like if you're doing

[code=php]
$sQuery = 'SELECT AVG(rctt) FROM name';
$sql = mysql_query($sQuery) or die ("Query failed: ".$sQuery."<hr>".$mysql_error())
echo $sql;
[/code]


you'll probably get a resource id, what you might want to try is

[code=php]
$sQuery = 'SELECT AVG(rctt) FROM name';
$sql = mysql_query($sQuery) or die ("Query failed: ".$sQuery."<hr>".$mysql_error())
$avg = mysql_fetch_row($sql);
echo $avg[0];
[/code]
Copy linkTweet thisAlerts:
@72newbieauthorDec 22.2006 — Thanks
×

Success!

Help @72newbie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...