Difference between revisions of "PHP:Read From a Database"

From Progzoo
Jump to: navigation, search
Line 6: Line 6:
 
<prog><![CDATA[
 
<prog><![CDATA[
 
<?php
 
<?php
$dbh = mysql_connect('sqlzoo.net','scott','tiger')
+
$dbh = new mysqli('sqlzoo.net','scott','tiger','gisq');
                            or die(mysql_error());
+
if ($dbh->connect_error){
mysql_select_db('gisq')    or die(mysql_error());
+
  die($dbh->connect_error);
$sql = "SELECT name, population
+
            FROM  world
+
            WHERE population>1000000000";
+
$result = mysql_query($sql) or die(mysql_error());
+
while ($line = mysql_fetch_array($result)){
+
  echo $line[0]."\t".$line[1]."\n";
+
 
}
 
}
mysql_close($dbh);
+
$bigPop = 1000000;
 +
$query = $dbh->prepare(
 +
"SELECT name, population
 +
            FROM world
 +
            WHERE population>?");
 +
$query->bind_param('d',$bigPop)
 +
  or die(mysql_error());
 +
$result = $query->execute() or die(mysql_error());
 +
$query->bind_result($name,$pop);
 +
while ($query->fetch()){
 +
  echo "$name\t$pop\n";
 +
}
 +
$query->close();
 +
$dbh->close()
 
?>
 
?>
 
]]></prog>
 
]]></prog>

Revision as of 11:29, 12 June 2017

Read the name and population for each country with a population of more than 1000000000.

You can access other columns from the bbc table. The columns are name region area population and gdp.

[Font] [Default] [Show] [Resize] [History] [Profile]

php:PHP and MySQL