Kyle
Kyle

Reputation: 2379

PHP -> MySQL not returning data

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

Upvotes: 0

Views: 451

Answers (2)

pankaj
pankaj

Reputation: 69

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();

Upvotes: 1

Mahendra Jella
Mahendra Jella

Reputation: 5596

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();

Upvotes: 3

Related Questions