0

My code only shows the last entry of my database. I don't know why.

Please help me a little with this.

Here is my code:

$hostname = "localhost";
$username = "waru";
$password = "olairhead154";
$database = "inmueble";
$tabel    = "inmuebles";

// Create connection
$conn = mysqli_connect($hostname, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

//set array variable 
$results = array();

//talk to the db
$sql="SELECT count(*) as Total ,fecha  FROM inmuebles GROUP BY fecha
ORDER BY fecha DESC limit 100";
$result = mysqli_query($conn, $sql);

//count the rows and fields
$totalRows = mysqli_num_rows($result);
$totalFields = mysqli_num_fields($result);

//start the loop
for ( $i = 0; $i < $totalRows; ++$i ) {

//make it 2 dim in case you change your order
$results[$i] = mysqli_fetch_array($result); 
}

?>

The html part

<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi">  
</script>
<script type="text/javascript">
  google.load( 'visualization', '1', { 'packages': [ 'corechart' ] } );
  google.setOnLoadCallback( drawChart );

  function drawChart() {
    var data = new google.visualization.DataTable();

    data.addColumn( 'string', 'Fecha' );
    data.addColumn( 'number', 'Total' );

    data.addRows(100);
    <?php

    $i = 0;
   $numofloops = 100;

 while($i < $numofloops){

 echo "data.setValue($i, 0, '" . $results[$i]["fecha"] . "');";
 echo "data.setValue($i, 1,  " . $results[$i]["Total"] . ");";

 $i++; 
      }
    ?>

    var options = {title: 'Echangerate EUR - GBP',
                   vAxis: {title: "Total"},
                   hAxis: {title: "Fecha"},
      colors: ['red','#004411'] 
  };

    var chart = new 

 google.visualization.AreaChart(document.getElementById('chart_div'));
    chart.draw( data, options);
  }
</script>
</head>
<body>
 <div id="chart_div" style="width: 1500px; height: 800px;"></div>
</body>
</html>

thank you so much in advancethis is how it looks like

Stanislav Mekhonoshin
  • 4,276
  • 2
  • 20
  • 25
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…”)` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Oct 18 '17 at 19:07
  • @tadman `$db->prepare("…”)` you should change those to regular quotes `$db->prepare("…")` - some might take your advice then paste that into their application, fill in the `…`'s and then *BAM!!*, parse error. ;-) – Funk Forty Niner Oct 18 '17 at 19:08
  • so this might be because of the mysqli way of declaring? im a really noob programmer sorry xD – Ulrich Matthew Vidangos Philip Oct 18 '17 at 19:12
  • @Fred-ii- I'm blaming Evernote here, but I'll fix that. Thanks. – tadman Oct 18 '17 at 19:12
  • Heh, anytime ;-) @tadman – Funk Forty Niner Oct 18 '17 at 19:13
  • It's not because of how you're calling `mysqli`, but the way you're doing it is way more verbose and you might accidentally call the wrong function, increasing confusion. PDO is significantly better, so I'd encourage you to use that if you're just getting started, before becoming stubbornly hooked on this old interface. – tadman Oct 18 '17 at 19:13
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman Oct 18 '17 at 19:13
  • well the thing is i copied this from an example , and tried to adapt it to my needs so im not trying to change it to PDO but im getting kind of confused sorry for my lack of developing skills T_T – Ulrich Matthew Vidangos Philip Oct 18 '17 at 19:17
  • Rather than doing a `for` loop on `mysqli_fetch_array()`, try a `while` loop on `mysqli_fetch_assoc()`, see what that produces @UlrichMatthewVidangosPhilip – Funk Forty Niner Oct 18 '17 at 19:19
  • looks im useless lol i tried this while ($row = mysqli_fetch_assoc($Result)){ $results[$i] = mysqli_fetch_array($result); } but nothing happends – Ulrich Matthew Vidangos Philip Oct 18 '17 at 19:28
  • its now like this while ($row = mysqli_fetch_assoc($result)) { $results[] = $row; echo $results; } but still the same result – Ulrich Matthew Vidangos Philip Oct 18 '17 at 19:47
  • echoed the outoput of results and it fetches data like this Array ( [0] => Array ( [Total] => 1 [fecha] => 2017-10-02 ) ) Array ( [0] => Array ( [Total] => 1 [fecha] => 2017-10-02 ) [1] => Array ( [Total] => 1 [fecha] => 2017-10-01 ) ) Array ( [0] => Array ( [Total] => 1 [fecha] => 2017-10-02 ) [1] => Array ( [Total] => 1 [fecha] => 2017-10-01 ) [2] => Array ( [Total] => 1 [fecha] => 2017-09-27 ) ) – Ulrich Matthew Vidangos Philip Oct 18 '17 at 20:12
  • any other suggestions @Fred-ii-? – Ulrich Matthew Vidangos Philip Oct 18 '17 at 20:46
  • Let me see what I can do later on when I have a few moments @UlrichMatthewVidangosPhilip – Funk Forty Niner Oct 18 '17 at 21:12
  • @Fred-ii- i managed to do it in other way. thank you all for all the help – Ulrich Matthew Vidangos Philip Oct 19 '17 at 00:46
  • @UlrichMatthewVidangosPhilip That's good news. You know that Stack lets you post your own answer and you can accept it also when it lets you. – Funk Forty Niner Oct 19 '17 at 00:47

0 Answers0