Domanda

Im a noob and only 16, i have a table full of videos that can be added though the website.

How do i show the Number of videos in the my-sql database that i can just past in between some php tags?

the database has tables likes users etc but one is "post" and i need to show how many records there are in it.

there is a config.php file that connects to the database which is like this (with the real info)

È stato utile?

Soluzione 2

Hi james this is the example code from php.net. Which echo the number of rows in table 1. You could try playing with it.

   <?php

   $link = mysql_connect("localhost", "mysql_user", "mysql_password");
   mysql_select_db("database", $link);

   $result = mysql_query("SELECT * FROM table1", $link);
   $num_rows = mysql_num_rows($result);

   echo "$num_rows Rows\n";

   ?>

Hope you understand

Altri suggerimenti

<?php
   $dbh = new PDO('mysql:dbname=mydbname', 'username', 'password');
   echo $dbh->query('SELECT COUNT(*) FROM videos')->fetchColumn();
?>

If you know how to connect database to PHP, mysql_num_rows() might come in handy. Read the manual here.

Sample code:

$db_selected = mysql_select_db("test_db",$con);

$sql = "SELECT * FROM person";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);

mysql_close($con);
?>

Just use COUNT() in your SQL

$con=new mysqli('localhost?', 'username', 'password', 'tablename');
$stmt=$con->prepare('SELECT COUNT(*) FROM videos');
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
echo 'There are '.$result.' Videos in the DB.';

By learning on tutorials, doing some PHP/MySQL tests, reading some books and searching on Google keywords like "MySQL counting rows in table + PHP"...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top