0

I'm trying to read a bunch of Rows and display them without the usual "ARRAY =>" crap but all I get is this error

Notice: Undefined variable: data in C:\xampp\htdocs\sql.php on line 0

This is me code:

<?php virtual('/Connections/TDBS_local.php');
$query = "SELECT sub_category FROM sub_category WHERE main_category_id = 2 ";
$result = mysqli_query($TDBS_local, $query);
if ($result = mysqli_query($TDBS_local, $query)) {

/* fetch associative array */
if($row = mysqli_fetch_assoc($result)) {
    $data = unserialize($row['sub_category']);
}

/* free result set */
mysqli_free_result($result);
}

 print "<pre>";
 print_r($data);
 print "</pre>";
?>

Where am I going wrong?

Gary
  • 13,303
  • 18
  • 49
  • 71
BiggJJ
  • 113
  • 4
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jolta Mar 01 '15 at 22:18
  • Updated my answer with mysqli, tested and works. – Adam Joseph Looze Mar 01 '15 at 22:31

2 Answers2

1

Here is how to do it with pdo, mysqli, and mysql

MYSQLI

    $con=mysqli_connect("localhost","username","password","dbname");

    $sql="SELECT sub_category FROM sub_category WHERE main_category_id = 2";

    $result=mysqli_query($con,$sql);

    $new_array = array();

    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
        $new_array[] = $row;
    }
    print_r($new_array);

    mysqli_free_result($result);

    mysqli_close($con);

MYSQL

mysql_connect("localhost","username","password");
mysql_select_db("dbname"); 

$result = mysql_query("SELECT sub_category FROM sub_category WHERE main_category_id = 2");
$new_array = array();
while ($row = mysql_fetch_array($result)) {
    $new_array[] = $row;
}
print_r($new_array);

PDO

$pdo = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');

$statement = $pdo->query("SELECT sub_category FROM sub_category WHERE main_category_id = 2");
$new_array = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
    $new_array[] = $row;
}
print_r($new_array);
  • this is the error its giving me this time: `Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\sql.php on line 0 Array ( ) ` Also if I use print_r will it not be formatted in the `Array => Row[0]` way in trying to avoid? – BiggJJ Mar 01 '15 at 22:14
  • see my updated answer. i tested with both pdo and mysql. mysqli should be the same. make sure your connection details are correct. and i removed the space at the end of your query – Adam Joseph Looze Mar 01 '15 at 22:21
  • UPDATED: how to do it in mysqli – Adam Joseph Looze Mar 01 '15 at 22:29
  • Thank you very much! I have been racking my brains all of yesterday to solve this. Simple problem as well. Much appreciated – BiggJJ Mar 02 '15 at 09:57
0

I think you have to use require or include instead of virtual:

<?php
require('Connections/TDBS_local.php');
$data = '';
$query = "SELECT sub_category FROM sub_category WHERE main_category_id = 2 ";
$result = mysqli_query($TDBS_local, $query);
if ($result = mysqli_query($TDBS_local, $query)) {

/* fetch associative array */
if($row = mysqli_fetch_assoc($result)) {
    $data = unserialize($row['sub_category']);
}

/* free result set */
mysqli_free_result($result);
}

 print "<pre>";
 print_r($data);
 print "</pre>";
?>
skroczek
  • 2,289
  • 2
  • 16
  • 23
  • Thanks for the suggestion. It's just giving me the same error but this time its telling me its on line 17, where $data is being called. – BiggJJ Mar 01 '15 at 22:10
  • Ok, that i haven't check. But I think this is a different problem? – skroczek Mar 01 '15 at 22:12
  • I updated my answer to avoid the error on line 17 if mysqli_fetch_assoc was not successful. – skroczek Mar 01 '15 at 22:23