0

I have confirmed that all of my code is working except for fetching of the array. It returns that there is only 1 row, and somehow 32 columns. However, my database has only 16 columns. Also when trying to print anything from the array out, there is nothing. Any help is much appreciated!

My query:

SELECT * FROM information WHERE username='kmccmk9'

 //Get Acquired Data
$useradmin = $_GET['useradmin'];
$userpass = $_GET['userpass'];
$db = $_GET['db'];
$username = $_GET['username'];
$password = $_GET['password'];
$sql = $_GET['sql'];

$link = mysql_connect("bondsolutionsnjcom.fatcowmysql.com", "aiforfrg", "********") or die('Could not connect: ' . mysql_error()); 
mysql_select_db($db) or die(mysql_error());
$temp = "$"."username";
$sql = str_replace($temp,"'".$username."'",$sql);
$temp = "$"."password";
$sql = str_replace($temp,"'".$password."'",$sql);
echo $sql;

$result_id = mysql_query($sql) or die("DATABASE ERROR!".mysql_error());

echo count($result_id);

$total = mysql_num_rows($result_id);

if($total == 1) {
    $info = mysql_fetch_array($result_id);
    for ($i=0;$i<count($info);$i++) {
        echo $info[i]." ";
    }
} else {
    echo "something messed up";
}

mysql_close();

Using mysqli instead, my code looks like the following:

$mysqli = new mysqli("bondsolutionsnjcom.fatcowmysql.com", $useradmin, $userpass, $db);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$res = $mysqli->query($sql);
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
    echo " id = " . $row['id'] . "\n";
}

But I am getting the following error and I don't know why:

Fatal error: Call to a member function data_seek() on a non-object in /hermes/waloraweb097/b516/moo.bondsolutionsnjcom/frg/scripts/executequery.php on line 15

5
  • Try using mysqli_* functions or PDO, avoid using mysql_* functions(deprecated) Commented Nov 28, 2013 at 5:35
  • Hi thanks for the response, I'm now getting a strange error. Please see the edited post. Commented Nov 28, 2013 at 5:46
  • Can you post your sql query here? Commented Nov 28, 2013 at 5:53
  • 1
    echo $info[$i] /// you missed to declare $ for the variable i.. Commented Nov 28, 2013 at 5:54
  • Thank you so much @Mahendra that fixed my original code. Commented Nov 28, 2013 at 6:02

2 Answers 2

3

Try this:

$useradmin = $_GET['useradmin'];
$userpass = $_GET['userpass'];
$db = $_GET['db'];
$username = $_GET['username'];
$password = $_GET['password'];
$sql = $_GET['sql'];

$link = mysql_connect("bondsolutionsnjcom.fatcowmysql.com", "aiforfrg", "********") or die('Could not connect: ' . mysql_error()); 
mysql_select_db($db) or die(mysql_error());
$temp = "$"."username";
$sql = str_replace($temp,"'".$username."'",$sql);
$temp = "$"."password";
$sql = str_replace($temp,"'".$password."'",$sql);
echo $sql;

$result_id = mysql_query($sql) or die("DATABASE ERROR!".mysql_error());

echo count($result_id);

$total = mysql_num_rows($result_id);

if ($total > 0) {
    $i=0;
    while($info = mysql_fetch_array($result_id))
    {
        echo $info[$i]." ";
        $i++;
    }
} else {
    echo "something messed up";
}

mysql_close();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response, unfortunately this did not change anything.
put echo $info[$i]; check now
Yes the $i was part of the problem. Thank you
1

Try below code. I have reviewed and changed your code:

//Get Acquired Data
$useradmin = $_GET['useradmin'];
$userpass = $_GET['userpass'];
$db = $_GET['db'];
$username = $_GET['username'];
$password = $_GET['password'];
$sql = $_GET['sql'];

$link = mysql_connect("bondsolutionsnjcom.fatcowmysql.com", "aiforfrg", "********") or die('Could not connect: ' . mysql_error()); 
mysql_select_db($db) or die(mysql_error());
$temp = "$"."username";
$sql = str_replace($temp,"'".$username."'",$sql);
$temp = "$"."password";
$sql = str_replace($temp,"'".$password."'",$sql);

$result_id = mysql_query($sql) or die("DATABASE ERROR!".mysql_error());

$total = mysql_num_rows($result_id);

if($total > 0) {
    while ($info = mysql_fetch_row($result_id)) {
        for ($i=0;$i<count($info);$i++) {
            echo $info[$i]." ";
        }
        echo "<br/>";
    }
}
else {
    echo "something messed up";
}

 mysql_close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.