2e database connecten
Ik heb al een tijdje een site. Maar laatst heb ik daar een radio bij toegevoegd. Daar heb ik ook een DJ Panel bij gedownload. Alleen wil ik de dingen die in de DJ panel staan in mijn site programmeren, wat betekent dat ik naar 2 databasen moet verbinden met bepaalde queries.
Als voorbeeld heb ik deze site genomen:
http://php.net/manual/en/function.mysql-select-db.php
En dan heb ik dit script bovenaan staan:
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
65
66
67
68
69
70
71
72
73
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
65
66
67
68
69
70
71
72
73
<?php
$handle_db1 = mysql_connect("localhost","databasenaam","databasewachtwoord");
mysql_select_db("typoradio",$handle_db1);
// Grab today's date using PHP date()
$today_date = date( 'N' );
// Grab the current hour
$now_hour = date( 'H' );
// Now we have the current hour, we add one to get the next hour
$next_hour = $now_hour + 1;
if ( $next_hour == 24 ) {
// It's midnight on the next day
$next_date = $today_date + 1;
$next_hour = "0";
}
else {
// It's the same day
$next_date = $today_date;
}
// Now we find out who's currently on ;)
$now_query = "SELECT * FROM typoradio.timetable WHERE day = '{$today_date}' AND time = '{$now_hour}'";
$which = $handle_db1;
mysql_query($now_query,$which);
// And who's next!
$next_query = "SELECT * FROM typoradio.timetable WHERE day = '{$next_date}' AND time = '{$next_hour}'";
$which = $handle_db1;
mysql_query($next_query,$which);
// Now create an array of the data, both now and next
$now_query_array = mysql_fetch_assoc( $now_query );
$next_query_array = mysql_fetch_assoc( $next_query );
$which = $handle_db1;
mysql_query($now_query_array,$which);
$which = $handle_db1;
mysql_query($next_query_array,$which);
// Now to make the hours friendly
if( $now_hour == 0 ) {
$now_hour = "00:00";
}
elseif( $now_hour == 24 ) {
$now_hour = "00:00";
}
else {
$now_hour = "{$now_hour}:00";
}
if( $next_hour == 0 ) {
$next_hour = "00:00";
}
elseif( $next_hour == 24 ) {
$next_hour = "00:00";
}
else {
$next_hour = "{$next_hour}:00";
}
?>
$handle_db1 = mysql_connect("localhost","databasenaam","databasewachtwoord");
mysql_select_db("typoradio",$handle_db1);
// Grab today's date using PHP date()
$today_date = date( 'N' );
// Grab the current hour
$now_hour = date( 'H' );
// Now we have the current hour, we add one to get the next hour
$next_hour = $now_hour + 1;
if ( $next_hour == 24 ) {
// It's midnight on the next day
$next_date = $today_date + 1;
$next_hour = "0";
}
else {
// It's the same day
$next_date = $today_date;
}
// Now we find out who's currently on ;)
$now_query = "SELECT * FROM typoradio.timetable WHERE day = '{$today_date}' AND time = '{$now_hour}'";
$which = $handle_db1;
mysql_query($now_query,$which);
// And who's next!
$next_query = "SELECT * FROM typoradio.timetable WHERE day = '{$next_date}' AND time = '{$next_hour}'";
$which = $handle_db1;
mysql_query($next_query,$which);
// Now create an array of the data, both now and next
$now_query_array = mysql_fetch_assoc( $now_query );
$next_query_array = mysql_fetch_assoc( $next_query );
$which = $handle_db1;
mysql_query($now_query_array,$which);
$which = $handle_db1;
mysql_query($next_query_array,$which);
// Now to make the hours friendly
if( $now_hour == 0 ) {
$now_hour = "00:00";
}
elseif( $now_hour == 24 ) {
$now_hour = "00:00";
}
else {
$now_hour = "{$now_hour}:00";
}
if( $next_hour == 0 ) {
$next_hour = "00:00";
}
elseif( $next_hour == 24 ) {
$next_hour = "00:00";
}
else {
$next_hour = "{$next_hour}:00";
}
?>
En dan heb ik dit script staan op de plek waar hij moet zeggen welke DJ er zometeen is:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// Now we find out if someone is currently on
if ( $now_query_array['dj'] == "" ) {
// There wasn't, so we tell the user
echo "No DJ has been scheduled for " . $now_hour . "";
}
else {
// Now we give them the bad news, someone is online :(
// So now to find out who they are
$who_are_they_now = "SELECT username FROM users WHERE id='{$now_query_array['dj']}'";
$which = $handle_db1;
mysql_query($who_are_they_now,$which);
$who_are_they_now = mysql_result( $who_are_they_now, 0, "username");
echo "DJ " . $who_are_they_now;
echo " (" . $now_hour . ")";
}
?>
// Now we find out if someone is currently on
if ( $now_query_array['dj'] == "" ) {
// There wasn't, so we tell the user
echo "No DJ has been scheduled for " . $now_hour . "";
}
else {
// Now we give them the bad news, someone is online :(
// So now to find out who they are
$who_are_they_now = "SELECT username FROM users WHERE id='{$now_query_array['dj']}'";
$which = $handle_db1;
mysql_query($who_are_they_now,$which);
$who_are_they_now = mysql_result( $who_are_they_now, 0, "username");
echo "DJ " . $who_are_they_now;
echo " (" . $now_hour . ")";
}
?>
Maar hij zegt dus telkens:
"No DJ has been scheduled for 18:00"
Wat gaat er dan verkeerd in mijn queries?
Ik hoop dat jullie het zien.
Alvast bedankt!
Groetjes, Bart
Toevoeging op 07/04/2011 19:41:42:
Weet niemand wat er fout is?
Gewijzigd op 07/04/2011 19:42:53 door Bart Knippers
Waarvoor denk jij dat mysql_connect een link identifier terug geeft!!
http://www.php.net/mysql_connect
Les 1: maak geen overbodige variabelen aan!!
Les 2: leer quoten -> PHP variabelen buiten quotes
Les 3: pas fatsoenlijke - mysql - foutafhandeling toe
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
65
66
67
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
65
66
67
<?php
// Include the required glob.php file
require_once( "panel/_inc/glob.php" );
// Grab today's date using PHP date()
$today_date = date( 'N' );
// Grab the current hour
$now_hour = date( 'H' );
// Now we have the current hour, we add one to get the next hour
$next_hour = $now_hour + 1;
if ( $next_hour == 24 ) {
// It's midnight on the next day
$next_date = $today_date + 1;
$next_hour = "0";
}
else {
// It's the same day
$next_date = $today_date;
}
// Now we find out who's currently on ;)
$now_query = $db->query( "SELECT * FROM timetable WHERE day = '{$today_date}' AND time = '{$now_hour}'" );
// And who's next!
$next_query = $db->query( "SELECT * FROM timetable WHERE day = '{$next_date}' AND time = '{$next_hour}'" );
// Now create an array of the data, both now and next
$now_query_array = $db->assoc( $now_query );
$next_query_array = $db->assoc( $next_query );
// Now to make the hours friendly
if( $now_hour == 0 ) {
$now_hour = "00:00";
}
elseif( $now_hour == 24 ) {
$now_hour = "00:00";
}
else {
$now_hour = "{$now_hour}:00";
}
if( $next_hour == 0 ) {
$next_hour = "00:00";
}
elseif( $next_hour == 24 ) {
$next_hour = "00:00";
}
else {
$next_hour = "{$next_hour}:00";
}
?>
// Include the required glob.php file
require_once( "panel/_inc/glob.php" );
// Grab today's date using PHP date()
$today_date = date( 'N' );
// Grab the current hour
$now_hour = date( 'H' );
// Now we have the current hour, we add one to get the next hour
$next_hour = $now_hour + 1;
if ( $next_hour == 24 ) {
// It's midnight on the next day
$next_date = $today_date + 1;
$next_hour = "0";
}
else {
// It's the same day
$next_date = $today_date;
}
// Now we find out who's currently on ;)
$now_query = $db->query( "SELECT * FROM timetable WHERE day = '{$today_date}' AND time = '{$now_hour}'" );
// And who's next!
$next_query = $db->query( "SELECT * FROM timetable WHERE day = '{$next_date}' AND time = '{$next_hour}'" );
// Now create an array of the data, both now and next
$now_query_array = $db->assoc( $now_query );
$next_query_array = $db->assoc( $next_query );
// Now to make the hours friendly
if( $now_hour == 0 ) {
$now_hour = "00:00";
}
elseif( $now_hour == 24 ) {
$now_hour = "00:00";
}
else {
$now_hour = "{$now_hour}:00";
}
if( $next_hour == 0 ) {
$next_hour = "00:00";
}
elseif( $next_hour == 24 ) {
$next_hour = "00:00";
}
else {
$next_hour = "{$next_hour}:00";
}
?>
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
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
<?php
// Now we find out if someone is currently on
if ( $now_query_array['dj'] == "" ) {
// There wasn't, so we tell the user
echo "No DJ has been scheduled for " . $now_hour . "";
}
else {
// Now we give them the bad news, someone is online :(
// So now to find out who they are
$who_are_they_now = $db->query( "SELECT username FROM users WHERE id='{$now_query_array['dj']}'" );
$who_are_they_now = mysql_result( $who_are_they_now, 0, "username");
echo "DJ " . $who_are_they_now;
echo " (" . $now_hour . ")";
}
?>
<?php
if ( $next_query_array['dj'] == "" ) {
// There wasn't, so we tell the user
echo "No DJ has been scheduled for " . $next_hour . "";
}
else {
// Now we give them the bad news, someone is online :(
// So now to find out who they are
$who_are_they_next = $db->query( "SELECT username FROM users WHERE id='{$next_query_array['dj']}'" );
$who_are_they_next = mysql_result( $who_are_they_next, 0, "username");
echo "DJ " . $who_are_they_next;
echo " (" . $next_hour . ")";
}
?>
// Now we find out if someone is currently on
if ( $now_query_array['dj'] == "" ) {
// There wasn't, so we tell the user
echo "No DJ has been scheduled for " . $now_hour . "";
}
else {
// Now we give them the bad news, someone is online :(
// So now to find out who they are
$who_are_they_now = $db->query( "SELECT username FROM users WHERE id='{$now_query_array['dj']}'" );
$who_are_they_now = mysql_result( $who_are_they_now, 0, "username");
echo "DJ " . $who_are_they_now;
echo " (" . $now_hour . ")";
}
?>
<?php
if ( $next_query_array['dj'] == "" ) {
// There wasn't, so we tell the user
echo "No DJ has been scheduled for " . $next_hour . "";
}
else {
// Now we give them the bad news, someone is online :(
// So now to find out who they are
$who_are_they_next = $db->query( "SELECT username FROM users WHERE id='{$next_query_array['dj']}'" );
$who_are_they_next = mysql_result( $who_are_they_next, 0, "username");
echo "DJ " . $who_are_they_next;
echo " (" . $next_hour . ")";
}
?>
Leg jij mij eens uit zonder het gebruik van het regeltje: require_once( "panel/_inc/glob.php" ); hoe je een tweede database connectie maakt. Want geloof me, heel php.net heb ik al afgezocht!
Dan geef je namenlijk zelf antwoord op je eigen vraag!!
Het antwoord is overigens al - in verdekte vorm - gegeven.
Het heeft dus totaal geen nut om met iets te komen wat niet mijn vraag beantwoord.
http://www.php.net/mysql_connect
Gewijzigd op 07/04/2011 21:54:05 door Noppes Homeland