Skip navigation

OK,

So I’m starting to get into using PHP to return MySQL database data to my SWF apps and there for need some super simple code to generate the returns (XML). So with that in mind here is my example of how to connect to a MySQL server using PHP, I’ve tried to make it self explanatory. Hopefully my copy and paste from TextMate will not give me any character encoding errors.

—– Code
// Put in header of page

<?php
$connection = mysql.connect(“server_name”, “user_name”, “password”);
if (!$connection)
{
die(“Database connection failed”);
}
$db_select = mysql_select_db(“database_name”, $connection);
if (!$db_select)
{
die(“Database connection failed”);
}
?>

// Put in body of page

<?php
$result = mysql_query(“SELECT * FROM table_name”, $connection);
if (!$result)
{
die(“Database connection failed”);
}

// Now lets display the results
while ($row = mysql_fetch_array($result))
{
// display columns 2 & 3 from the table, assuming that column 1 would be
// the primary unique id key that you do not need to display
// for example column [1] might be a name and column [2] might be a telephone number
echo $row[1].” “.$row[2]. “<br />”;
}
?>

// Finally outside of the closing HTML tag close the database connection
<?php
my_sql_close($connection);
?>

— End code.

Advertisements

3 Comments

  1. Hello,
    I am in dire need of assistance. I had to upgrade my box i.e. do a fresh install and then my php3 files no longer worked. I am sadly not good with php3, I’m hoping that you who seem to be very good with it might help.

    I will paste the php3 code here, Note also we have tried it on other domains and it works fine but for some reason it brain farts on the box so we have eliminated the mysql db.

    Here’s the code;

    Syree Character DataBase

    <?php
    $PATH_INFO=”/Syree/CharDB/”;
    $db = mysql_connect(“ourIP”, “name”, “pw”) or die(mysql_error());
    mysql_select_db(“DBNAME”, $db);
    include (‘includes/display_functions.php3’);
    if ($secure) {
    display_search_box($user,$password);
    $authenticated = new_authentication($user,$password);
    if ($show) {
    if ($id) {
    if ($show == 1) {
    display_char_info($id, $user, $password);
    }
    else if ($show == 2) {
    modify_char($id, $user, $password);
    }
    else if ($show == 3) {
    $authenticated = authenticate_user($user, $password, “null”, “null”);
    if ($authenticated == “Master”) {
    $result = mysql_query(“SELECT * FROM characters WHERE id=$id”);
    $row = mysql_fetch_assoc($result);
    while (list($name,$value) = each($row)) {
    $message .= “$name=$value\n”;
    }
    mail(“codewolf@centurytel.net”, “Char deleted”, $message);
    $result = mysql_query(“DELETE FROM characters WHERE id=$id”);
    echo “$result\n”;
    echo “Character Deleted!”;
    }
    else {
    echo (“You do not have access to delete records from the DB”);
    }
    }
    else {
    echo “Invalid function”;
    }
    }
    else {
    $extra_where = “”;
    $extra_order = ” ORDER BY charname “;
    if ($son) {
    if ($srch == 1) {
    $extra_where .= ” WHERE $son like ‘$mtch%’ “;
    }
    else if ($srch == 2) {
    $extra_where .= ” WHERE $son like ‘%$mtch%’ “;
    }
    else {
    $extra_where .= ” WHERE $son = ‘$mtch’ “;
    }
    $extra_order = ” ORDER BY $son “;
    }
    if ($authenticated != “Master” && $authenticated != “GM”) {
    if ($son != “charclass” && $son != “charname” && $son != “race”) {
    $extra_where = “”;
    $extra_order = ” ORDER BY charname “;
    }
    }
    $result = mysql_query(“SELECT * FROM characters $extra_where $extra_order”, $db);
    $myrow = mysql_fetch_array($result);
    echo “\n”;
    echo “Character Name”;
    if (($authenticated == “GM”) || ($authenticated == “Master”) || ($authenticated == “Host”)) {
    echo “ClassLevelRace”;
    }
    echo “\n”;
    do {
    if (!$myrow[“charname”]) {
    $myrow[“charname”] = “Fix me GM! I have no name!”;
    }
    printf(“%s“, $PATH_INFO, $myrow[“id”], $myrow[“charname”]);
    if ($authenticated == “GM” || $authenticated == “Master” || $authenticated == “Host”) {
    printf(“%s”,$myrow[“charclass”]);
    printf(“%s”,$myrow[“level”]);
    printf(“%s”,$myrow[“race”]);
    }
    echo “\n”;
    } while ($myrow = mysql_fetch_array($result));
    echo “\n”;
    }
    }
    else if ($add) {
    add_char($user, $password);
    }
    else if ($submit) {
    if ($id) {
    $db = mysql_connect(“THEIP”, “nameofDB”, “pw”);
    $result = mysql_query(“SELECT * FROM characters WHERE id=$id”, $db);
    $myrow = mysql_fetch_array($result);

    $authenticated = authenticate_user($user, $password, $myrow[charpassword], $myrow[charname]);
    if ($authenticated == “Master”) {
    // echo “background=$background”;
    $sql = “UPDATE characters SET
    charname=’$charname’,
    playername=’$playername’,
    emailaddress=’$emailaddress’,
    charpassword=’$charpassword’,
    strength=’$strength’,
    intelligence=’$intelligence’,
    wisdom=’$wisdom’,
    constitution=’$constitution’,
    dexterity=’$dexterity’,
    charisma=’$charisma’,
    charclass=’$charclass’,
    race=’$race’,
    level=’$level’,
    experience=’$experience’,
    deity=’$deity’,
    age=’$age’,
    height=’$height’,
    weight=’$weight’,
    hair=’$hair’,
    eyes=’$eyes’,
    alignment=’$alignment’,
    sex=’$sex’,
    description=’$description’,
    background=’$background’,
    languages=’$languages’,
    skills=’$skills’,
    carried_equipment=’$carried_equipment’,
    equipment=’$equipment’,
    gold=’$gold’,
    treasures=’$treasures’,
    armor=’$armor’,
    potions=’$potions’,
    weapons=’$weapons’,
    companions=’$companions’,
    racial_abs=’$racial_abs’,
    gm_notes=’$gm_notes’
    WHERE id=’$id'”;
    // echo “$sql”;
    $result = mysql_query($sql);
    // echo “Result is: $result”;
    // echo “Error is: “;
    // printf (“%s”,mysql_error());
    echo “Thank you! Information Updated.\n”;
    }
    /* elseif ($authenticated == “GM”) {
    $sql = “UPDATE characters SET
    experience=’$experience’,
    level=’$level’,
    carried_equipment=’$carried_equipment’,
    equipment=’$equipment’,
    gold=’$gold’,
    treasures=’$treasures’,
    armor=’$armor’,
    potions=’$potions’,
    weapons=’$weapons’,
    companions=’$companions’,
    gm_notes=’$gm_notes’
    WHERE id=’$id’
    “;
    $result = mysql_query($sql);
    echo “Thank you! Information Updated.\n”;
    } */
    elseif ($authenticated == “User”) {
    $sql = “UPDATE characters SET background=’$background’,description=’$description’,charpassword=’$charpassword’,carried_equipment=’$carried_equipment’ WHERE id=’$id'”;
    $result = mysql_query($sql);
    echo “Thank you! Information Updated.\n”;
    }
    else {
    echo (“You are not authenticated to modify this character”);
    }
    }
    else {
    $authenticated = authenticate_user($user, $password, “null”, “null”);
    if ($authenticated == “Master”) {
    $sql = “INSERT INTO characters (
    charpassword,
    charname,
    playername,
    emailaddress,
    strength,
    intelligence,
    wisdom,
    constitution,
    dexterity,
    charisma,
    charclass,
    race,
    level,
    experience,
    deity,
    age,
    height,
    weight,
    hair,
    eyes,
    alignment,
    sex,
    carried_equipment,
    equipment,
    description,
    background,
    skills,
    languages,
    gold,
    treasures,
    armor,
    weapons,
    potions,
    companions,
    gm_notes,
    racial_abs
    ) VALUES (
    ‘$charpassword’,
    ‘$charname’,
    ‘$playername’,
    ‘$emailaddress’,
    ‘$strength’,
    ‘$intelligence’,
    ‘$wisdom’,
    ‘$constitution’,
    ‘$dexterity’,
    ‘$charisma’,
    ‘$charclass’,
    ‘$race’,
    ‘$level’,
    ‘$experience’,
    ‘$deity’,
    ‘$age’,
    ‘$height’,
    ‘$weight’,
    ‘$hair’,
    ‘$eyes’,
    ‘$alignment’,
    ‘$sex’,
    ‘$carried_equipment’,
    ‘$equipment’,
    ‘$description’,
    ‘$background’,
    ‘$skills’,
    ‘$languages’,
    ‘$gold’,
    ‘$treasures’,
    ‘$armor’,
    ‘$weapons’,
    ‘$potions’,
    ‘$companions’,
    ‘$gm_notes’,
    ‘$racial_abs’)”;
    $result = mysql_query($sql);
    if (!$result) {
    echo “Error: “.mysql_error();
    }
    else {
    echo “Thank you! Information Submitted.\n”;
    }
    }
    else {
    echo (“You are not authenticated to add characters to the DB”);
    }
    }
    }
    else {
    printf(“Show Records\n”, $PATH_INFO);

    if (($authenticated == “GM”) || ($authenticated == “Master”)) {
    printf(“Add new Record\n”, $PATH_INFO);
    printf(“Modify Records\n”, $PATH_INFO);
    printf(“Delete Records\n”, $PATH_INFO);
    }
    if ($authenticated == “User”) {
    $sql = “SELECT id FROM characters WHERE charname=’$user'”;
    $result = mysql_query($sql);
    if (!$result) {
    echo “Error: “.mysql_error();
    }
    else {
    $myrow = mysql_fetch_array($result);
    display_char_info($myrow[id],$user,$password);
    }
    }
    }
    }
    else {
    echo (“Please Log Into the DataBase”);
    echo (“You may log in as Guest/Guest if you do not have a special user/pass for the DB”);
    printf (“”, $PATH_INFO);
    echo (“Username: \n”);
    echo (“Password: \n”);
    echo (“\n”);
    echo (“\n”);
    }
    ?>

    Note, I did remove all of the passwords and IP’s etc..

  2. Since you mention it works fine on other boxes I am wondering if maybe it is a problem with the php configuration on the box? I know I had a setup once that was giving me problems due to php in apache being configured for a previous version to the code I had in place.

    Perhaps compare the configurations with a box that the code above works in and see how they match up.

  3. Yea thats what I was thinking


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: