Fout in functie
Ik wil graag een functie maken die de volgende waarden uit een database haalt: id, titel, datum post en auteur. Wanneer ik deze functie invoeg in een pagina, krijg ik de volgende error:
Code (php)
1
Fatal error: Call to undefined function getListContentMain() in /Applications/MAMP/htdocs/beheer/index.php on line 443
Dit is het script waar de functie in staat:
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
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
<?php
//Database connections are made in this file
$conHost = 'localhost';
$conUser = 'blablabla';
$conPass = 'blablabla';
$con = mysqli_connect($conHost, $conUser, $conPass);
if(mysqli_connect_errno()){
echo "Er kon geen verbinding worden gemaakt met de database: " . mysqli_connect_error();
function getListContentMain() {
$dbname = 'content';
mysqli_select_db($dbname,$con);
$q = "SELECT * FROM main";
$result = mysql_query($q,$con);
$ListContentMainData = array();
while($row = mysql_fetch_array($result));
$ListContentMainData[''] = $row;
return $ListContentData;
}
}
?>
//Database connections are made in this file
$conHost = 'localhost';
$conUser = 'blablabla';
$conPass = 'blablabla';
$con = mysqli_connect($conHost, $conUser, $conPass);
if(mysqli_connect_errno()){
echo "Er kon geen verbinding worden gemaakt met de database: " . mysqli_connect_error();
function getListContentMain() {
$dbname = 'content';
mysqli_select_db($dbname,$con);
$q = "SELECT * FROM main";
$result = mysql_query($q,$con);
$ListContentMainData = array();
while($row = mysql_fetch_array($result));
$ListContentMainData[''] = $row;
return $ListContentData;
}
}
?>
Hoe kan ik deze functie verbeteren zodat ik mijn error niet meer krijg?
Jules
Heb je die wel ge-include()?
En $con bestaat niet in de function.
Zie global.
Is het slim om de verbinding met de database in een apart bestand te zetten, bv connect.php en dan deze te include na de regel ?
Gewijzigd op 08/04/2014 14:02:54 door Jules Kreutzer
Code (php)
Je kan de variabele mee geven met de functie
Code (php)
Of aangeven dat de variabele een GLOBAL is.
De functie ziet er nu zo uit:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
//Require bestand waar database connecties worden gemaakt
include ('connect/connect.php');
function getListContentMain() {
global $con;
$dbname = 'content';
mysqli_select_db($dbname, $con);
$q = "SELECT * FROM main";
$result = mysql_query($q,$con);
$ListContentMainData = array();
while($row = mysql_fetch_array($result));
$ListContentMainData[''] = $row;
return $ListContentData;
}
?>
//Require bestand waar database connecties worden gemaakt
include ('connect/connect.php');
function getListContentMain() {
global $con;
$dbname = 'content';
mysqli_select_db($dbname, $con);
$q = "SELECT * FROM main";
$result = mysql_query($q,$con);
$ListContentMainData = array();
while($row = mysql_fetch_array($result));
$ListContentMainData[''] = $row;
return $ListContentData;
}
?>
$ListContentMainData[''] = $row;
moet zijn:
$ListContentMainData[] = $row;
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 8
Warning: mysql_query() expects parameter 2 to be resource, object given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 10
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 13
Notice: Array to string conversion in /Applications/MAMP/htdocs/beheer/index.php on line 443
Array
Warning: mysql_query() expects parameter 2 to be resource, object given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 10
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 13
Notice: Array to string conversion in /Applications/MAMP/htdocs/beheer/index.php on line 443
Array
Parameters omdraaien:
mysqli_select_db($con, $dbname);
Jules Kreutzer op 08/04/2014 14:18:39:
Nee helaas niet, ik krijg verschillende errors:
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 8
Warning: mysql_query() expects parameter 2 to be resource, object given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 10
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 13
Notice: Array to string conversion in /Applications/MAMP/htdocs/beheer/index.php on line 443
Array
Warning: mysql_query() expects parameter 2 to be resource, object given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 10
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /Applications/MAMP/htdocs/beheer/functions/functions.php on line 13
Notice: Array to string conversion in /Applications/MAMP/htdocs/beheer/index.php on line 443
Array
De eerste error is opgelost, $dbname en $con moesten worden omgewisseld.
Parameters moeten ook omgedraaid worden.
Je haalt mysqli_* en mysql_* door elkaar.
nu moet ik alleen de array to string conversion wegwerken. Is hier een simpele functie voor?
Jules Kreutzer op 08/04/2014 14:31:02:
nu moet ik alleen de array to string conversion wegwerken. Is hier een simpele functie voor?
Geen idee wat je hiermee bedoelt.
Notice: Array to string conversion in /Applications/MAMP/htdocs/beheer/index.php on line 443
Array
Of terwijl er wordt geprobeerd een array als string weer te geven.
De oplossing is dus de array als array behandelen en een string als een string.
en hoe kan ik hier precies voor zorgen?
En hoe ziet die regel er uit?
Wat ik uiteindelijk wil is een kolom van 4 rijen waarbij de laatste 5 records worden weergegeven. Ik heb het voor elkaar gekregen om de gegevens te tonen, maar ze worden nu per cel getoond.
De functie ziet er nu als volgt uit:
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
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
<?php
include ('connect/connect.php');
function getListContentMain() {
global $con;
$dbname = 'content';
mysqli_select_db($con,$dbname);
$q = "SELECT * FROM main LIMIT 5";
$result = mysqli_query($con,$q);
$ListContentMainData = array();
while($row = mysqli_fetch_array($result)) {
echo $row['id'];
echo $row['title'];
echo $row['date'];
echo $row['auteur'];
}
$ListContentMainData[] = $row;
return $ListContentMainData;
}
?>
include ('connect/connect.php');
function getListContentMain() {
global $con;
$dbname = 'content';
mysqli_select_db($con,$dbname);
$q = "SELECT * FROM main LIMIT 5";
$result = mysqli_query($con,$q);
$ListContentMainData = array();
while($row = mysqli_fetch_array($result)) {
echo $row['id'];
echo $row['title'];
echo $row['date'];
echo $row['auteur'];
}
$ListContentMainData[] = $row;
return $ListContentMainData;
}
?>
De html van de pagina index.php ziet er als volgt uit:
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
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
<html>
<table>
<thead>
<tr>
<th>Post ID</th>
<th>Titel</th>
<th>Datum Post</th>
<th>Auteur</th>
</tr>
</thead>
<tbody>
<tr>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>444</b><br />
Array</td>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>445</b><br />
Array</td>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>446</b><br />
Array</td>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>447</b><br />
Array</td>
</tr>
</tbody>
</table>
</html>
<table>
<thead>
<tr>
<th>Post ID</th>
<th>Titel</th>
<th>Datum Post</th>
<th>Auteur</th>
</tr>
</thead>
<tbody>
<tr>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>444</b><br />
Array</td>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>445</b><br />
Array</td>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>446</b><br />
Array</td>
<td>1test 12014-04-04 21:50:03Jules Kreutzer2test 22014-04-04 21:50:03test account<br />
<b>Notice</b>: Array to string conversion in <b>/Applications/MAMP/htdocs/beheer/index.php</b> on line <b>447</b><br />
Array</td>
</tr>
</tbody>
</table>
</html>
foreach ( jouwfunctie() AS $result){
echo $veld['naam'];
}
?>
Toevoeging op 08/04/2014 15:05:06:
Dit is denk wat je wilt:
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
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
<?php
include 'hier/je/database/map/en/bestandsnaam.php';
$dbname = 'content';
//Connectie staat in de $con
$host = mysqli_select_db($dbname, $connectie);
function getListContentMain($host) {
$query = "SELECT * FROM main";
$result = mysql_query($host, $query);
if($result != null){
$returnValue = '';
while($row = mysql_fetch_array($result));
$returnValue .= $row['veldNaam1'];
$returnValue .= $row['veldNaam2'];
$returnValue .= $row['veldNaam3'];
$returnValue .= $row['etc'];
return $returnValue;
}
} else {
return 'Er kon niets opgehaald worden!';
}
}
include 'hier/je/database/map/en/bestandsnaam.php';
$dbname = 'content';
//Connectie staat in de $con
$host = mysqli_select_db($dbname, $connectie);
function getListContentMain($host) {
$query = "SELECT * FROM main";
$result = mysql_query($host, $query);
if($result != null){
$returnValue = '';
while($row = mysql_fetch_array($result));
$returnValue .= $row['veldNaam1'];
$returnValue .= $row['veldNaam2'];
$returnValue .= $row['veldNaam3'];
$returnValue .= $row['etc'];
return $returnValue;
}
} else {
return 'Er kon niets opgehaald worden!';
}
}
Gewijzigd op 08/04/2014 15:05:40 door Donny Wie weet
@Donny: Achter de while() geen puntkomma en de return uit de loop halen.
Hoe moet ik dat met mijn functie doen aangezien ik niet echt wijs word van de handleidingen op internet...