Volgens mijn leerboek 'PHP6 en MySQL'(de echte Wiley Bible)zou ik met behulp van het onderstaand PHP-Script een tabel krijgen met continenten landen en steden.
Maar ik krijg de volgende foutmeldingen:
( ! ) Notice: Undefined variable: db in C:\wamp\www\VoorbeeldTabel.php on line 5
Call Stack
# Time Memory Function Location
1 0.0015 384912 {main}( ) ..\VoorbeeldTabel.php:0
( ! ) Notice: Undefined variable: global_dbh in C:\wamp\www\VoorbeeldTabel.php on line 5
Call Stack
# Time Memory Function Location
1 0.0015 384912 {main}( ) ..\VoorbeeldTabel.php:0
( ! ) Warning: mysql_select_db() expects parameter 2 to be resource, null given in C:\wamp\www\VoorbeeldTabel.php on line 5
Call Stack
# Time Memory Function Location
1 0.0015 384912 {main}( ) ..\VoorbeeldTabel.php:0
2 0.0060 391600 mysql_select_db ( ) ..\VoorbeeldTabel.php:5
Could not select databased
<?php
include("/home/phpbook/phpbook-vars.inc");
$globel_dbh = mysql_connect($hostname, $username, $password)
or die("Could not connect to database");
mysql_select_db($db, $global_dbh)
or die ("Could not select databased");
function add_new_country($dbh, $continent, $countryname, $city_array)
{
$country_query =
"INSERT INTO country (continent, countryname)
VALUES ('$continent', '$countryname')";
$result_id = mysql_query($country_query)
OR die($country_query . mysql_error());
if ($result_id)
{
$countryID = mysql_insert_id($dbh);
for ($city = current($city_array);
$city;
$city = next($city_array))
{
$city_query =
"INSERT INTO city (countryID, cityname)
VALUES ($countryID, '$city')";
mysql_query($city_query, $dbh)
OR die($city_query . mysql_error());
}
}
}
function populate_cities_db($dbh)
{
/* drop tables if they exist - permits function to be tried more than once*/
mysql_query("DROP TABLE city", $dbh);
mysql_query("DROP TABLE country", $dbh);
/*create the tables */
mysql_query("CREATE TABLE country
(ID int not null auto_increment primary key,
continent varcher(50),
countryname varchar(50))",
$dbh)
OR die(mysql_error());
mysql_query("create table city
(ID int not null auto_increment primary key,
countryID int not null,
cityname varchar(50))",
$dbh)
OR die (mysql_error());
/* store data in the tables */
add_new_country($dbh, 'Africa', 'Kenya',
array('Nairobi','Mombasa','Meru'));
add_new_country($dbh, 'South America', 'Brazil',
array('Rio de Janeiro', 'Sao Paulo', 'Salvador', 'Belo Horizonte'));
add_new_country($dbh, 'North America', 'USA',
array('Chicago', 'New York', 'Houston', 'Miami'));
add_new_country($dbh, 'North America', 'Canada',
array('Montreal', 'Windsor', 'Winnipeg'));
print("Sample database created<BR>");
}
?>
<HTML><HEAD><TITLE>Creating a sample database</TITLE></HEAD>
<body>
<?php populate_cities_db($global_dbh); ?>
</body>
</html>
1.538 views