Query result opslaan in variabele
Als je een select query doet en er komen meerdere rijen uit als resultaat, is het resultaat dan op te slaan in een variabele?
ja
en hoe doe ik dat?
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
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
<?php
function sql($query, $echo = false)
{
global $mysqli;
$query = trim(str_replace("\n", '', $query));
$query = trim(str_replace("\t", '', $query));
$result = mysqli_query($mysqli, $query);
$return = array();
if($echo !== false)
{
echo '<pre>'.$query.'</pre>';
}
if($result === false)
{
// mysqli_error($mysqli);
return false;
}
if(substr($query, 0, 6) == 'SELECT')
{
while ($row = mysqli_fetch_assoc($result))
{
$return[] = $row;
}
return $return;
}
else
{
return $result;
}
}
?>
function sql($query, $echo = false)
{
global $mysqli;
$query = trim(str_replace("\n", '', $query));
$query = trim(str_replace("\t", '', $query));
$result = mysqli_query($mysqli, $query);
$return = array();
if($echo !== false)
{
echo '<pre>'.$query.'</pre>';
}
if($result === false)
{
// mysqli_error($mysqli);
return false;
}
if(substr($query, 0, 6) == 'SELECT')
{
while ($row = mysqli_fetch_assoc($result))
{
$return[] = $row;
}
return $return;
}
else
{
return $result;
}
}
?>
Ik heb nu de query er boven gezet, maar dan zie ik nog niets:
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
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
<?php
include('dbconnect.php');
$query = "SELECT kop FROM nieuwsbrieven1";
function sql($query, $echo = false)
{
global $mysqli;
$query = trim(str_replace("\n", '', $query));
$query = trim(str_replace("\t", '', $query));
$result = mysqli_query($mysqli, $query);
$return = array();
if($echo !== false)
{
echo '<pre>'.$query.'</pre>';
}
if($result === false)
{
// mysqli_error($mysqli);
return false;
}
if(substr($query, 0, 6) == 'SELECT')
{
while ($row = mysqli_fetch_assoc($result))
{
$return[] = $row;
}
return $return;
}
else
{
return $result;
}
}
?>
include('dbconnect.php');
$query = "SELECT kop FROM nieuwsbrieven1";
function sql($query, $echo = false)
{
global $mysqli;
$query = trim(str_replace("\n", '', $query));
$query = trim(str_replace("\t", '', $query));
$result = mysqli_query($mysqli, $query);
$return = array();
if($echo !== false)
{
echo '<pre>'.$query.'</pre>';
}
if($result === false)
{
// mysqli_error($mysqli);
return false;
}
if(substr($query, 0, 6) == 'SELECT')
{
while ($row = mysqli_fetch_assoc($result))
{
$return[] = $row;
}
return $return;
}
else
{
return $result;
}
}
?>