PHP problems with function involving: explode(), MySQL, array()

olliepop

Member
Messages
138
Reaction score
0
Points
16
Hey there guys.
Just yesterday this function worked absolutely fine. That is until i duplicated it to explode a different row in the database.

The function explodes a row in the database into 11 different values. An example of the database (DB > Table > Column > Value)
Code:
server1 / bases1 / buildings / 1;1;1;1;1;1;1;1;1;1;1
Thanks for any help!!!!

____

The code for the function is
PHP:
function explodeBuildings($table, $uid) {
    $baseid = printMySQL("users1", "viewing_baseid", $uid);
    $result = mysql_query("SELECT * FROM $table WHERE fb_id='$uid' AND baseid='$baseid'") or die("Query failed with error: ".mysql_error());
    

    while($row = mysql_fetch_array($result))
    {
        $bdata = $row['buildings'];
    }
    
    list($b1, $b2, $b3, $b4, $b5, $b6, $b7, $b8, $b9, $b10, $b11) = explode(";", $bdata);
    $blevels = array($b1, $b2, $b3, $b4, $b5, $b6, $b7, $b8, $b9, $b10, $b11);
    return $blevels;
}
The error returned is
Notice: Undefined variable: bdata in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 10 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 9 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 8 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 7 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 6 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 5 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 4 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 3 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 2 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147

Notice: Undefined offset: 1 in C:\WEBSERVER\www\invasionpoint\client\action\config\calls\mysql.php on line 147
The code which is calling the function, is:
PHP:
<?php 

// prints city view

require("/../init.php");


$blevels = explodeBuildings("bases1", $uid);

echo "Operation Headquarters: " . $blevels[0] . "<br/>";
echo "Warehouse: " . $blevels[1] . "<br/>";
echo "Space Craft Factory: " . $blevels[2] . "<br/>";
echo "Research Laboratory: " . $blevels[3] . "<br/>";
echo "Barracks: " . $blevels[4] . "<br/>";
echo "Weapons Factory: " . $blevels[5] . "<br/>";
echo "Shield Generator: " . $blevels[6] . "<br/>";
echo "Galactic Exchange: " . $blevels[7] . "<br/>";
echo "Planetary Vehicle Factory: " . $blevels[8] . "<br/>";
echo "Steelworks: " . $blevels[9] . "<br/>";
echo "Mining Node: " . $blevels[10] . "<br/>";
?>
The entire file just incase, is:
PHP:
<?php

/* 
------------------------------------------------------------
File name: config/calls/mysql.php
Purpose: Calls to get and put to MySQL
------------------------------------------------------------
*/
require '/../settings.php';
require '/../mysql.php';
$uid = null;

global $con;
$con = mysql_connect($loc,$user,$pass);
global $con;
if (!$con){
    echo "<script type='text/javascript'>top.location.href = '../../';</script>";
  die("Can't connect to the database server! Redirecting you now.");
  }
mysql_select_db($db, $con);


// SELECTS from MySQL. Returns data one cell at a time.
//         $table = table to update
//         $what = what column to pull data from
//         $uid = what row to pull data from
function printMySQL($table, $what, $uid) {

    $result = mysql_query("SELECT * FROM $table
    WHERE fb_id='$uid'");

    while($row = mysql_fetch_array($result))
      {
      return $row[$what];
      }
  
}

// UPDATES in MySQL. Can not insert a whole new row. Updates a row one cell at a time.
//         $table = table to update
//         $column = what column to update
//        $what = what data to put into the column
//         $uid = what row to update
//        $where = second WHERE - eg townid
//        $where2 = what the second WHERE should equal
function updateMySQL($table, $column, $what, $uid, $where, $where2) {

    $result = mysql_query("UPDATE $table SET $where='$what' WHERE fb_id='$uid' AND $where='$where2'");
    
    if(!$result) { 
    die();
    }
  
}

// INSERTS NEW TIMER in MySQL. Can only insert a new timer. Can not update an already existing timer.
//         $timeend = time in seconds to add to current time, for when script should end.
//         $send = What script to execute when ended
//         $uid = users id should remain $uid in function, specific to users FB id

/* TIME CHART
1 Minute: 60 seconds
1 Hour: 3,600 seconds
1 Day: 86,400 seconds
1 Week: 604,800 seconds
4 Weeks: 2,419,200 seconds
1 Year: 31,536,000 seconds
1 Decade: 315,360,000 seconds
*/

function newTimer($timeend, $send, $uid) {
    
    $date = date('H:i:s');
    
    // add seconds to time
     $t1 = $timeend + time();
     $t2 = date("H:i:s", $t1); 
     
     // time order. make H, M, S in seconds totalled up.
     $timeorder = $timeend + time();

    global $con;
    
    $result="INSERT INTO timers1 (fb_id, time_start, time_end, script_end, timeorder)
                VALUES ('$uid','$date','$t2','$send', '$timeorder')";
    mysql_query($result,$con);
}


// Explodes the buildings row from MySQL into an array. Each value is the LEVEL OF THE BUILDING.
// $table = table to explode from
// $uid = what uid to get from
// $baseid = CURRENTLY REMOVED, WILL GET FROM MYSQL THE CURRENT BASE
// function explodeBuildings($table, $uid, $baseid) {

function explodeBuildings($table, $uid) {
    $baseid = printMySQL("users1", "viewing_baseid", $uid);
    $result = mysql_query("SELECT * FROM $table WHERE fb_id='$uid' AND baseid='$baseid'") or die("Query failed with error: ".mysql_error());
    

    while($row = mysql_fetch_array($result))
    {
        $bdata = $row['buildings'];
    }
    
    list($b1, $b2, $b3, $b4, $b5, $b6, $b7, $b8, $b9, $b10, $b11) = explode(";", $bdata);
    $blevels = array($b1, $b2, $b3, $b4, $b5, $b6, $b7, $b8, $b9, $b10, $b11);
    return $blevels;
}


// Prints an entire MySQL table. Should be for admin and debug purposes only. If exploitable, REMOVE.
//         $table = table to print
// function renderTable($table) {

function renderTable($table) {
    // sending query
    $result = mysql_query("SELECT * FROM {$table}");
    if (!$result) {
        die("Query to show fields from table failed");
    }

    $fields_num = mysql_num_fields($result);

    echo "<h1>Table: <a href='#' onclick='renderPage(\"rendertable.php?table=".$table."\")'>{$table}</a></h1>Invasion Point MySQL admin by Ollie<br/><br/><br/>";
    echo "<table border='1'><tr>";
    // printing table headers
    for($i=0; $i<$fields_num; $i++)
    {
        $field = mysql_fetch_field($result);
        echo "<td>{$field->name}</td>";
    }
    echo "</tr>\n";
    // printing table rows
    while($row = mysql_fetch_row($result))
    {
        echo "<tr>";

        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        foreach($row as $cell)
            echo "<td>$cell</td>";
    
        
    }
    echo "</tr>\n";
      echo "</table>";
    mysql_free_result($result);

}

// Orders timers
function printTimers($uid, $pag = 0) {
$result = mysql_query("SELECT * FROM timers1 WHERE fb_id='$uid' ORDER BY timeorder, timer_id");
$i = 1;
echo "<table id='timerstacktable' border='1' cellpadding='0' cellspacing='0' width='163'>";
while($row = mysql_fetch_array($result)) {        

        $ttd1 = ($row['timeorder'] - time());
        $td1 = sec2hms($ttd1);
         echo "<tr><td>".$row['timer_id'] . " : ".$i."</td><td>";
        echo "<span id='ff".$i."'>";
          echo $td1 . "</span><br/>";
        echo "<script id='sc".$i."' type='text/javascript'>timer('ff".$i."');</script>";
        echo "</tr></td>";
        /*echo "<script language=javascript> eval(t".$i."()); </script>";*/
        $i++;
        if($i== 6) {
            break;
        }
  }
  $i--;
 echo "</table>";
 //if($pag != "ya") {
 echo "|".$i;
 //}
}

// how many timers are running (out of 5 cause of max 5 in stack)
function countTimers($uid) {
$result = mysql_query("SELECT * FROM timers1 WHERE fb_id='$uid' ORDER BY timeorder, timer_id");
$i = 1;
while($row = mysql_fetch_array($result)) {        
        $i++;
        if($i== 6) {
            break;
        }
  }
  $i--;
 return $i;
}

// Destroy expired timers
function destroyExpired() {
    $result = mysql_query("SELECT * FROM timers1 WHERE timeorder < '" . (time() + 1) . "'");

while($row = mysql_fetch_array($result)) {
    mysql_query("DELETE FROM timers1 WHERE timer_id='".$row['timer_id']."'");
    }
}

?>
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Read the error report. $bdata is being accessed but its value is undefined.

Hence, it was never assigned a value.

Hence, your query did not return a row. You don't test to see if your query returned a row.

Construct your query in a separate string so you can print it out, and then print it out. You might find your answer there.
 

olliepop

Member
Messages
138
Reaction score
0
Points
16
/facepalm

After 6 and a half hours of banging my head on my desk and sipping caffine i noticed that the row viewing_baseid != baseid like it was supposed too. Thank you so much for opening my eyes! Now to find out why this is so....
 
Top