php sql help

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
dave3460
Posts: 117
Joined: Wed Jul 01, 2009 2:13 pm

php sql help

Post by dave3460 »

i am getting a couple of errors and i am not 100% sure what i am changing could anyone please advise
these are the errors i am getting
Notice: Undefined index: page in C:\wamp\www\testing\index.php on line 14

Notice: Undefined offset: 1 in C:\wamp\www\testing\index.php on line 123

and the code is here i have taken out the url as it has a api key involved
<?php


$page = $_GET['page'];

if($page == 'db')
{

/*

@url index.php?page=db
@desc "This is giving you instructions as to how to create the table we will be using here."

*/


echo 'Use this SQL Statement to create the Table in the Database (Remembering to replace "datebase_name" with your actual database name):<br><br>';

echo "<pre>CREATE TABLE `test`.`_auction` (
`auctionID` INT( 12 ) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`auctionAmount` INT( 12 ) NOT NULL DEFAULT '0',
`auctionItemName` VARCHAR( 100 ) NOT NULL ,
`auctionUpgrade` VARCHAR( 100 ) NOT NULL ,
`auctionUpgradeAmount` INT( 10 ) NOT NULL DEFAULT '0',
`upgradeSellername` VARCHAR( 50 ) NOT NULL ,
`upgradeBidderName` VARCHAR( 50 ) NOT NULL ,
`auctionHighBid` INT( 12 ) NOT NULL ,
`auctionHoursLeft` TINYINT( 4 ) NOT NULL
) ENGINE = MYISAM</pre>";


exit();
}


/*

@url index.php
@desc "This is the full code, taking the output, breaking it down and inserting it to our DB."

*/


/*

This is your connection script, make sure to change the relevant details

*/

$host = 'localhost';
$db_user = ''; // Will need changing
$db_pass = ''; // Will need changing
$db_name = ''; // Will need changing

$dbc = mysql_connect("$host", "$db_user", "$db_pass") or die('Cannot Connect To Server');
mysql_select_db("$db_name") or die('Cannot Connect To Database');



/* VARIABLES */


$url = 'http://www.'; // Change this to the correct URL to access the data

$display_output = 0; // Change this to 1 if you want to run this in the browser and see the output. Or if you want the cron job to email you the output so that you know it is working.

$display_queries = 0; // Change this to 1 if you want to see the SQL equivalent statements in the browser, of what is being run.

$live = 0; // Change this to 1 once you have set up the table in the database and you want the script to run properly and update your DB, rather than just showing you the text output




// This is storing the output of that page into the variable "$data"

$data = file_get_contents($url);


// This line is getting rid of the first 2 characters in the data, ie: the "1 "

$data = substr($data, 2);

// This line is adding an extra "@" to the end of the data to fix a small problem

$data = $data . '@';


// This line is exploding the data into each element, separated by the "@"s

$elements = explode("@", $data);

// This is counting the number of elements (Not auctions - Each auction has 8 elements)

$count = count($elements);


// This is the regular expression we will be checking for in each element. Since the Admin who designed the output decided to make it difficult by not separating the auction slots properly, so it requires an extra loop later on.

$pat = '/[0-9]+\s[0-9]+/';



// This for loop is running through each element separated by the "@"s, and if it meets the pattern above (A number, followed by a space, followed by a number) then we put in a "~" character to separate the auction slots

for($i = 0; $i < $count; $i++)
{

// Does match pattern

if(preg_match($pat, $elements[$i]))
{
$exp = explode(" ", $elements[$i]); // Separate that element into 2, separated by the space
$elements[$i] = $exp[0] . '~' . $exp[1]; // Update the element to add in the "~" marker
$i = -1; // Restart the for loop
}

}



// Setting new variable for data

$data2 = '';

for($i = 0; $i < $count; $i++)
{

// Looping through all the elements again and reconstructing the data string from the array.

$data2 .= $elements[$i];

// Add in the "@" after each element to bring it back to how it should have looked originally if the Admin of said output had done it better.

if(($i + 1) < ($count - 1))
{
$data2 .= '@';
}


}






$output = '';

$output .= '<h1 style="text-align:center;">Cron Output</h1><br><hr><br>';

echo '<p style="text-align:center;font-size:10pt;color:red;">If you are viewing this page in the browser, you will need to check the source code on the server to see what is happening and what you need to do.</p>';


// Start over again with the new String of data, this time explode with "~" so we can get each auction slot

$slots = explode("~", $data2);

$output .= 'Auction Slots: ' . count($slots) . '<br><br>';

// Explode to get number of elements as well

$el = explode("@", $data2);

$output .= '<br><br>';

if($live = 1)
{
// Truncate the table to remove all current rows
mysql_query("TRUNCATE TABLE `_auction`") or die('Error: ' . mysql_error());
}


$query = '';



// Loop through each auction slot

for($i = 0; $i < count($slots); $i++)
{


$liveQuery = '';

// Update query String if output is desired

$query .= "<br><br>INSERT INTO `_auction` (`auctionAmount`, `auctionItemName`, `auctionUpgrade`, `auctionUpgradeAmount`, `upgradeSellername`, `upgradeBidderName`, `auctionHighBid`, `auctionHoursLeft`) VALUES (";

// Live Query String

$liveQuery .= "INSERT INTO `_auction` (`auctionAmount`, `auctionItemName`, `auctionUpgrade`, `auctionUpgradeAmount`, `upgradeSellername`, `upgradeBidderName`, `auctionHighBid`, `auctionHoursLeft`) VALUES (";





$output .= '<u>Slot '.$i.'</u><br>';

// Explode this auction slot with "@" to get each element

$elements = explode("@", $slots[$i]);

// Loop through them

for($j = 0; $j < count($elements); $j++)
{

$query .= "'".$elements[$j]."'";
$liveQuery .= "'".$elements[$j]."'";

if($j < (count($elements) - 1))
{
$query .= ',';
$liveQuery .= ',';
}

$output .= '['.$j.']: '.$elements[$j].'<br>';
}

$output .= '<br><br>';

$query .= ');';
$liveQuery .= ')';


// If script is live, insert into the DB
if($live == 1)
{
mysql_query($liveQuery) or die('Error (Cannot Insert AuctionID ['.$i.']) Query =><br><br>'.$liveQuery.' : ' . '<br><br>' . mysql_error());
}


}

if($display_output == 1)
{
echo '<br><hr><br>';
echo $output;
}
if($display_queries == 1)
{
echo '<br><hr><br>';
echo $query;
}
if($live == 1)
{
echo '<p style="text-align:center;font-size:10pt;color:blue;">Database Updated Successfully</p>';
}
?>
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: php sql help

Post by hallsofvallhalla »

change

Code: Select all

echo 'Use this SQL Statement to create the Table in the Database (Remembering to replace "datebase_name" with your actual database name):<br><br>';
to this

Code: Select all

echo "Use this SQL Statement to create the Table in the Database (Remembering to replace \"datebase_name\" with your actual database name):<br><br>";
also give us what line 123 is, too much for me to count :)
Genaga
Posts: 86
Joined: Fri Dec 11, 2009 7:05 pm

Re: php sql help

Post by Genaga »

According to context, line 123 is

Code: Select all

$data2 = '';
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: php sql help

Post by Chris »

These errors are caused when trying to call a variable that doesn't exist.
If the page key isn't defined in your url, you'll get an error on line 14, first check wether $_GET['page'] isn't empty
index.php?page=

Code: Select all

<?php 
if( empty($_GET['page']) === false )
{
    $page = $_GET['page'];
}
else
{
    $page = false;
}
 
As for line 123.. I don't think it is. I think it's line 130. It should be.

Code: Select all

<?php 
if( empty($element[$i]) === false )
{
    $data2 .= $element[$i];
}
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Advanced Help and Support”