Item Database [need help]

Everything PHP-related! Discuss scripts, host challenges, request a script, or just ask for plain old help.

Moderators: IRC Operators, Support Team

DirtyD
Newbie
Newbie
Posts: 1
Joined: Sat Aug 29, 2009 8:15 pm

Item Database [need help]

Postby DirtyD » Sat Aug 29, 2009 9:08 pm

I'm fairly new to PHP and I was editing a code found on this forum (viewtopic.php?f=44&t=20661)
So far, the only issue I'm having is I cannot get the code to add anything to the items table in my database.

Can anyone tell me what i did wrong? Either changed something in the code i shouldn't have, "/runescape/index.php?rs2item=&orderby=0&Players=all&category=0&search=1" isnt correct, my variables arent right, etc... any kind of hints or tips would be appreciated.

I left /** * @author Srankie * @copyright 2008 */ in the code because i did not make the code itself, I'm just editing it to work for items and not monsters

Code: Select all

<?php

/**
* @author Srankie
* @copyright 2008
*/

/*
Set some data to connect
to MySQL, edit it to your
own setting.
*/

$server = "";
$username = "";
$password = "";
$database = "";

$mysql = mysql_connect($server, $username, $password) or die("Can't connect to MySQL (" .
mysql_error() . ")"); //Connect to MySQL
mysql_select_db($database, $mysql) or die("Can't select database '$database' (" .
mysql_error() . ")"); //Select the table
$check = mysql_query("SELECT * FROM items LIMIT 0,1");
if (!$check) {
$mysql_query = "CREATE TABLE IF NOT EXISTS items (
id INT,
name VARCHAR(50),
category VARCHAR(50),
members VARCHAR(3),
quest VARCHAR(3)
)";
mysql_query($mysql_query, $mysql); // Create the table
$first_time = true;
}

// Check if I have to update the database, or only return information about a item
if ($first_time == true) {
echo "First time this script executes, updating the database.";
$update = true;
} else {
if (isset($_GET['update'])) {
echo "You wanted to update the database, please wait.";
$update = true;
}
}

// if $update is true update the database
if ($update == true) {
$results = 0; // The amount of results in the database
$host = "www.tip.it"; // Set the host of the item database.
$get = "/runescape/index.php?rs2item=&orderby=0&Players=all&category=0&search=1"; // Set the GET part of the headers.
$sock = fsockopen($host, 80, $errno, $errstr);
if (!$sock) {
die("Couldn't connect to '$host' ($errstr ($errno))");
}
$headers = "GET $get HTTP/1.0\n\r\n\r"; // Set GET header
$headers .= "HOST: $host\n\r"; // Set HOST header
fwrite($sock, $headers); // Send the headers.
while (!feof($sock)) { // Read every single line of the socket
$read = fgets($sock); // Read the line
$next_line = fgets($sock); // Read the next line of the source
if (preg_match("/<td><a href=\"\?rs2item_id=(\d+)\">(.+?)<\/a><\/td>/", $next_line,
$match)) { // Another if to make sure that the level is actually a level
$id = $match[1]; // Set the id of the item
$name = $match[2]; // Set the name of the item
//Set some data (category, Members, and the Quest)
$next_line = fgets($sock);
preg_match("/<td align=\"center\">(.+?)<\/td>/", $next_line, $match);
$category = $match[1];
$next_line = fgets($sock);
preg_match("/<td align=\"center\">(.+?)<\/td>/", $next_line, $match);
$members = $match[1];
$next_line = fgets($sock);
preg_match("/<td align=\"center\">(.+?)<\/td>/", $next_line, $match);
$quest = $match[1];
$next_line = fgets($sock);
preg_match("/<td align=\"center\">(.+?)<\/td>/", $next_line, $match);
$query = "SELECT * FROM items WHERE id='$id'";
$check = mysql_query($query); // Check if the item exist, if yes
// Then I should use UPDATE, else INSERT
$row = mysql_fetch_array($check);
// Make everything useable for MySQL
$id = mysql_escape_string($id);
$name = mysql_escape_string($name);
$members = mysql_escape_string($members);
$quest = mysql_escape_string($quest);
$category = mysql_escape_string($category);
if (!$row['name']) {
$query = "INSERT INTO items (category, id, name, members, quest)
VALUES ('$category', '$id', '$name', '$members', '$quest')";
} else {
$query = "UPDATE items SET category='$category', name='$name', members='$members', quest='$quest' WHERE id='$id'";
}
$results++;
mysql_query($query) or die("Could not add data (" . mysql_error() . ")");
}
}

die("<p>Updating done, $results items added/updated");
}

if (isset($_GET['viewall'])) {
echo '<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">item name</div></td>
<td><div align="center">category</div></td>
<td><div align="center">Members</div></td>
<td><div align="center">Quest</div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>'; // Echo the table
$result = mysql_query("SELECT * FROM items ORDER BY name"); // Select the items
while ($row = mysql_fetch_array($result)) {
echo "<tr>
<td><div align=\"center\">{$row['name']}</div></td>
<td><div align=\"center\">{$row['category']}</div></td>
<td><div align=\"center\">{$row['members']}</div></td>
<td><div align=\"center\">{$row['quest']}</div></td>
</tr>"; // Echo data
}
echo "</table>"; // And close the table
}

if (isset($_GET['viewone'])) {
// Validate some $_GET variables
if (!isset($_GET['searchby'])) {
die("Please specifie 'searchby'");
}
if (!in_array($_GET['searchby'], array("Name", "category", "Members",
"Quest"))) {
die("Please choose out of: Name, category, Members, or Quest for 'searchby'");
}
if (!$_GET[$_GET['searchby']]) {
die("Please specifie '{$_GET['searchby']}'");
}
$where = array(mysql_escape_string($_GET['searchby']), mysql_escape_string($_GET[$_GET['searchby']]));
$result = mysql_query("SELECT * FROM items WHERE {$where[0]}='{$where[1]}' ORDER BY name");
if (!isset($_GET['parse'])) {
echo '<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">item name</div></td>
<td><div align="center">category</div></td>
<td><div align="center">Members</div></td>
<td><div align="center">Quest</div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>';
}
$noresult = true;
while ($row = mysql_fetch_array($result)) {
if (!isset($_GET['parse'])) {
echo "<tr>
<td><div align=\"center\">{$row['name']}</div></td>
<td><div align=\"center\">{$row['category']}</div></td>
<td><div align=\"center\">{$row['members']}</div></td>
<td><div align=\"center\">{$row['quest']}</div></td>
</tr>";
} else {
$noresult = false;
echo "RESULT:{$row['name']}:{$row['category']}:{$row['members']}:{$row['quest']}\n";
}
}
if (isset($_GET['parse'])) {
if ($noresult == true) {
echo "RESULT:No results found";
}
echo "\n\n";
}
}

?>

Here is what i get on first time running:
First time this script executes, updating the database.

Updating done, 0 items added/updated


and when updating
You wanted to update the database, please wait.

Updating done, 0 items added/updated

the table "items" and the fields "id, name, category, members, and quest" are all created without a problem, but as i said before nothing else is added to the database.


I would have posted on their topic for some advice, but in 99% sure i would get flamed for "bumping" an old thread.

As is, I'm sure some one will say something about me editing their code...

thanks for any help that i receive

Return to “PHP”

Who is online

Users browsing this forum: No registered users and 1 guest