0

I am trying to query a table on my database and assign arrays to individual sets of results,

then i will use these results in a FLOT graph to plot them....

i am connected to the database and attempting to pull data,

i then use the following query:

    <?php
// Main query to pull data from 'tests' table
    $sql = "SELECT * FROM `tests` WHERE member_id = '1'";
    $result = mysql_query($sql) or die ("no query");

//  Dataset1
  if ($result)
  {
    while ($row=$result1->fetch_assoc())
    {
      $dataset1[] = array($row['test1'],$row['date']);
    }
  }
?>

i then try to plot the first graph using the following javascript

<script type="text/javascript">
//put array into javascript variable
var dataset1 = <?php echo json_encode($dataset1); ?>;

//plot
$(function () {
     $.plot($("#placeholder"), [ dataset1 ]);
});

but i'm getting the error

atal error: Call to a member function fetch_assoc() on a non-object in /homepages/test.php on line 25

once this works i will then continue the charts from 'test1' all the way to 'test9'

all with individual charts..

where am i going wrong guys???

Thanks

Stephen Jackson
  • 260
  • 2
  • 6
  • 20
  • it will eventually replace long winded google charts for example this is "test1" http://chart.apis.google.com/chart?&cht=lc&chtt=Salinity+(sg)&chls=2&chg=14.89,25,0.5,4,2.13&chs=460x180&chd=t:1.024,1.028,1.024,1.024,1.024,0.000,0.000,0.000,0.000&chds=1.018,1.030&chxt=x,y&chxl=0:|06.05|07.05|09.05|21.09|21.09|05.05|05.05|05.05|15.09|1:|1.018|1.020|1.022|1.024|1.026|1.028|1.030&chxtc=1,7,13&chf=bg,s,FFFFFF00&chco=3d98df&chm=B,3d98df44,0,1,0|o,348dd2,0,-1,6.0|N*3*,a8a8a8,0,-1,10 Date across the bottom and the left axis will vary depending on results i guess?? – Stephen Jackson Jan 03 '14 at 23:39
  • The old mysql extension is deprecated and will be phased out. Instead, use mysqli (which supports the OO interface you're trying to use) or PDO. They both have API support for the operation you're looking for: [`mysqli_stmt::fetch_all()`](http://php.net/mysqli_result.fetch_all) (when using [mysqlnd](http://php.net/mysqlnd), the MySQL native driver) and [`PDOStatement::fetchAll()`](http://php.net/PDOStatement.fetchAll). Both also support prepared statements, unlike mysql. – outis Jan 03 '14 at 23:51
  • Don't use [`SELECT *`](http://stackoverflow.com/q/321299/) unless you're writing a DB administration program; select only the columns you need. – outis Jan 03 '14 at 23:54

2 Answers2

2

You are not standard MySQL and not MySQLI so you need to use:

while ($row=mysql_fetch_assoc($result))

MySQLI is the OOP version of the non OOP MySQL version. Please be aware the version you are currently using is deprecated and you should use MySQLI or PDO_MYSQL.

Niels
  • 48,601
  • 4
  • 62
  • 81
  • 1
    `$result` not `$result1` – Paul Jan 03 '14 at 23:40
  • that got rid of my error, but im getting no data, how can i echo the array to see what im getting?? – Stephen Jackson Jan 03 '14 at 23:45
  • 1
    Just use `echo $row['test1'];` to see the output. or use `var_dump($row);` to see the array version in the HTML. Example of using mysql_fetch_assoc: http://php.net/mysql_fetch_assoc – Niels Jan 03 '14 at 23:47
1

I just think you have the variable name wrong, $result1 should be $result. Also, you are not using mysqli, your:

while ($row=$result1->fetch_assoc())

should be

while ($row=mysql_fetch_assoc($result))
Jeremy Zerr
  • 874
  • 7
  • 12