Web Hosting Talk







View Full Version : PHP/MySQL Problem!


SmashedPumpkins
05-04-2009, 10:39 PM
I've very new to coding, but I've been involved with websites for years. I'm working on a few things, but I'm not sure if I'm heading in the right direction. My goal is to connect to the database and insert the text into a table. This is all the code I have relating to it. I'm using a book, but it's not helping me enough. First I have a few things commented out. I've tried two different methods, but I get errors. Here's the errors I'm getting. http://randallmarshall.com/contactme.php

Thanks for any help! Oh and the book said to require DB.php. It comes with PEAR DB? (PEAR DB is installed on my server) Any info on that would help too!


<?php
// include($_SERVER['DOCUMENT_ROOT'].'/db.inc.php');
// $hostname_db = "localhost";
// $username_db = "USERNAME";
// $password_db = "PASSSWORD";
// $db = "DATABASENAME";

// $link = @mysql_pconnect($hostname_db, $username_db, $password_db);
// if (!$link) {die('Could not connect: ' . mysql_error());}
// mysql_select_db($db,$link) or die(mysql_error());

require 'DB.php';
$db = DB::connect('mysql://USERNAME:PASSWORD)mp3s@db.randallmarshall.com/DATABASE');
if (DB::isError($db)) { die("connection error: " . $db->getMessage( )); }

$q = $db->query("INSERT INTO support (subject, question)
VALUES ('Exiting!', 'How do I exit?')");
?>

cselzer
05-04-2009, 11:33 PM
you need to install pear.

EDIT:
let me rephrase that

You need to configure pear for your server if its installed. I don't even suggest using pear tbh. I'd look for a tutorial, or a simple mysql wrapper class.

Jaseeey
05-05-2009, 08:14 AM
Hi mate,

If you would like some eBooks on PHP+MySQL, I can lead you in the right direction. Send me a private message if you're interested and I can provide you with a temporary download link for a number of eBooks. They will guide you in the right direction.

I am also available on MSN for which I can provide some basic help.

Regards,

cselzer
05-05-2009, 01:03 PM
Little tutorial.

Some code that we will be using.

<?PHP
/**
* Selzer -- A light weight CMS.
* @version 0.2
* @author Cody Selzer <cody.selzer@gmail.com>
* @link http://cselzer.com/
* @copyright Copyright 2009 Cody Selzer
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Selzer.php
*
* MODIFIED VERSION FOR TUTORIAL ON WEBHOSTINGTALK DO NOT REMOVE
*/
class Selzer
{

const hostname = "localhost";
const username = "root";
const password = "1234";
const database = "testdatabase";
const persistent = false; //only need to set true if mysql server is on a remote machine. pointless if its not.
const debug = false;
function Selzer()
{
MySQL::Connect();
if (self::debug)
Error::Raise();
}
}
class MySQL
{
static $databaseLink = false;
static $databaseConnected = false;
static $queries = 0;
function Connect()
{
if (Selzer::persistent) {
if (!self::$databaseLink = mysql_pconnect(Selzer::hostname, Selzer::username,
Selzer::password)) {
Error::Error("MySQL: Database connection: " . mysql_error(), true);
}
} else {
if (!self::$databaseLink = mysql_connect(Selzer::hostname, Selzer::username,
Selzer::password)) {
Error::Error("MySQL: Database connection: " . mysql_error(), true);
}
}
if (!mysql_select_db(Selzer::database, self::$databaseLink)) {
Error::Error("MySQL: Database selection: " . mysql_error(), true);
}
self::$databaseConnected = true;
}
function Connected()
{
if (self::$databaseConnected)
return true;
else
return false;
}
function Query($query)
{
$query = mysql_query($query, self::$databaseLink) or Error::Error("MySQL: Query error: " .
mysql_error());
if ($query) {
self::$queries++;
return $query;
} else {
return false;
}
}
function Row($result, $query = true)
{
if ($query == true) {
return mysql_fetch_assoc(self::Query($result));
} else {
return mysql_fetch_assoc($result);
}
}
function Rows($sql)
{
$query = self::Query($sql);
$data = array();

while ($row = $this->Row($query, false)) {
$data[] = $row;
}
return $data;
}
function NumRows($query)
{
return mysql_num_rows(self::Query($query));
}
function Sanitize($string)
{
if (get_magic_quotes_gpc() || ini_get('magic_quotes_sybase')) {
stripslashes($string);
}
return mysql_real_escape_string($string);
}
function ExecuteFile($file)
{
foreach (file($file) as $line) {
if (!is_null(trim($line)) || !strpos($line, "--")) {
echo $line . "<br />";
self::Query($line);
} else {
continue;
}
}
}
}
class Error
{
static $errors = array();
function Error($error, $flag = false)
{
self::$errors[] = array($error, $flag);
if ($flag === true)
self::Raise(true);
}
function Raise($hault = false)
{
foreach (self::$errors as $processError) {
echo $processError[0] . "<br />";
}
if ($hault === true) {
if (Selzer::debug === true) {
die("HAULTED");
} else {
die("");
}
}
}
}
new Selzer();
?>


Going to break this code down.


const hostname = "localhost";
const username = "root";
const password = "1234";
const database = "testdatabase";

What this code does, is hold constant values in an OOP place. You change them just like you would change a variable, but they cannot change, hence they are constants. You can only change them if you change them in the file itself, a php code cannot change them, nothing can. Constants. Good way to define database information, so that it cannot be changed.

Now that we have that settled. If you include this file, you will have a way to interact with a single database, and a way to sanitize a variable before you put it into the database which is very important to know because it can cause SQL Injection.

To use this code...

Select from a fake table and get the output of a single row..

$row = MySQL::Row("SELECT * FROM foo WHERE bar = 'fun'");
print_r($row);


Lets get a bunch of data...


$rows = MySQL::Rows("SELECT * FROM foo");
print_r($rows);


How many foo is there?


$foo = MySQL::NumRows("SELECT * FROM foo");
echo $foo;


Lets sanitize a variable, and then use it in a select statement.

$string = "I don't know what to write...";
$string = MySQL::Sanitize($string);

escapes the ' in $string, and makes it database ready, now lets ue it

$row = MySQL::Row("SELET * FROM sentences WHERE sentence = '$string'");


Obviously these examples wouldn't work, but if you compare the SQL from these to others you will understand.

Take a look at the mysql functions http://php.net/mysql

Feel free to use this code, but don't remove my copyright, soon going to be releasing the entire CMS open source under the MIT license. If you share the code with someone, don't remove the license.

If you need any further help, please write me an email at cody.selzer[at]gmail.com and I will be glad to help you out as much as possible to get you started in the right track. If you have AIM, my screen name is CBV, if i don't reply it's because im leaving in the next few minutes after i post this to go cut my grand parents grass.

Thanks,

Cody

HivelocityDD
05-09-2009, 07:11 PM
Great Job cselzer. I really like the class you have wrote.

cselzer
05-11-2009, 06:12 PM
Great Job cselzer. I really like the class you have wrote.

Thanks, glad it's useful for someone.

tim2718281
05-11-2009, 06:36 PM
In PHP, you can insert data into a table by first connecting to a database

global $dbhandle;

$dbhandle = mysql_connect("localhost:/var/lib/mysql/mysql.sock",
"username", "password");

mysql_select_db("databasename", $dbhandle);



and then

$sql="INSERT INTO tablename (
columnx,
columny,
columnz
)
VALUES (
'$data1',
'$data2',
'$data3'
) ";

if (!mysql_query($sql,$dbhandle)) {
die('Error: ' . mysql_error());
}