hi friends
i want to draw graphs by feching data from database through php code . plz help me

Dani AI

Generated

Brief expert summary and a practical, minimal workflow.

and were correct that rendering graphs in the browser is the modern, most-flexible approach; server-side image generation (GD) is possible but less interactive and harder to maintain. A reliable pattern that works with PHP + MySQL is: the database -> a small PHP endpoint that returns JSON -> a client-side charting library that consumes that JSON and draws the chart.

Minimal PHP endpoint (use PDO and prepared statements; adapt connection/SQL to the schema):

<?php
header('Content-Type: application/json; charset=utf-8');

$pdo = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8mb4', 'DBUSER', 'DBPASS', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare('SELECT date, value FROM metrics WHERE user_id = :uid ORDER BY date');
$stmt->execute([':uid' => 1]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($rows);

Client-side (common pattern — fetch JSON and feed a chart library such as Chart.js, D3, Flot, or Highcharts):

<canvas id="chart"></canvas>
<script>
fetch('/data.php')
  .then(r => r.json())
  .then(data => {
    const labels = data.map(r => r.date);               // ISO dates recommended
    const values = data.map(r => Number(r.value));     // ensure numeric types
    const ctx = document.getElementById('chart').getContext('2d');
    new Chart(ctx, { type: 'line', data: { labels, datasets:[{ label:'Value', data:values }] } });
  })
  .catch(console.error);
</script>

Practical notes and gotchas (tie-back to ): always set the JSON header, validate and cast types (dates and numbers), use prepared statements to avoid SQL injection, and watch CORS if the JS is hosted on a different origin. For large datasets, perform server-side aggregation or downsampling rather than plotting thousands of raw points. Be mindful of library licenses (some are free, some require commercial licenses). Debug with the browser Network and Console tabs to verify the JSON shape and MIME type.

Recommended Answers

All 4 Replies

Hi you can use google charts its easy to integrate to your php and mysql script as mmc told you need javascript to accomplish it although you can also use plane php to achieved it but i will not really recommend it..

thanks friens for the help
but sorry to say i m still failed to do so

Well, in the last 2+ weeks, what have you tried?

Have you read this:

You can google your question, there are a lot of solutions... trust me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.