Tabel in MySQL aanmaken met PHP

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Henk Woeltjes

Henk Woeltjes

16/08/2014 16:33:24
Quote Anchor link
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



Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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>
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
<?php populate_cities_db($global_dbh); ?>

</body>
</html>
Gewijzigd op 16/08/2014 16:41:26 door Henk Woeltjes
 
PHP hulp

PHP hulp

28/03/2024 20:25:27
 
Erwin H

Erwin H

16/08/2014 16:51:27
Quote Anchor link
PHP6? Een boek uit de toekomst!

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.
 
Ger van Steenderen
Tutorial mod

Ger van Steenderen

16/08/2014 20:31:28
Quote Anchor link
>> PHP6? Een boek uit de toekomst!

En een toekomst waarin de mysql extensie nog niet deprecated is!!!!!!!!

Ow ja, en dit:
Code (sql)
PHP script in nieuw venster Selecteer het PHP script
1
DROP TABLE atable

Geeft een SQL fout als de tabel niet bestaat.
Dit niet:
Code (sql)
PHP script in nieuw venster Selecteer het PHP script
1
DROP TABLE IF EXISTS atable

Of opnemen in de CREATE ddl
Code (sql)
PHP script in nieuw venster Selecteer het PHP script
1
CREATE TABLE IF NOT EXISTS atable
Gewijzigd op 17/08/2014 08:15:37 door Ger van Steenderen
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.