Tabel in MySQL aanmaken met PHP
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
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?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>");
}
?>
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>
</body>
</html>
Gewijzigd op 16/08/2014 16:41:26 door Henk Woeltjes
Anyway, de foutmeldingen zeggen volgens mij precies wat er mis is. De variabelen $db, $global_dbh zijn niet gedefinieerd, dus die kan je nog helemaal niet gebruiken. De derde foutmelding is daar uiteraard een gevolg van, want omdat de variabelen niet bestaan krijgt de betreffende functie niet het object dat het verwacht.
En een toekomst waarin de mysql extensie nog niet deprecated is!!!!!!!!
Ow ja, en dit:
Geeft een SQL fout als de tabel niet bestaat.
Dit niet:
Of opnemen in de CREATE ddl
Gewijzigd op 17/08/2014 08:15:37 door Ger van Steenderen