error help
ik heb jullie hulp nodig met deze error het is een code voor search engine met pagination. ik heb deze error nog nooit gezien er staat interger inplaats van boolean.
alvast bedankt voor jullie hulp.
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, integer given in C:\xampp\htdocs\search.php on line 274
Quote:
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
mysql_connect ("localhost", "root", "");
mysql_select_db("results");
$k = ($_GET['k']);
$tableName="search";
$targetpage = "search.php";
$limit = 1;
$terms = explode (" ", $k);
$query = "SELECT COUNT(*) as num FROM `search` WHERE ";
foreach ($terms as $each){
@$i++;
if ($i == 1)
$query .= " `keywords` LIKE '%$each%' ";
else
$query .= "OR `keywords` LIKE '%$each%' ";
}
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM search LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
echo "<p><font color='#62472C'>$total_pages Result(s)</font> </p>";
// pagination
echo $paginate;
?>
mysql_connect ("localhost", "root", "");
mysql_select_db("results");
$k = ($_GET['k']);
$tableName="search";
$targetpage = "search.php";
$limit = 1;
$terms = explode (" ", $k);
$query = "SELECT COUNT(*) as num FROM `search` WHERE ";
foreach ($terms as $each){
@$i++;
if ($i == 1)
$query .= " `keywords` LIKE '%$each%' ";
else
$query .= "OR `keywords` LIKE '%$each%' ";
}
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM search LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
echo "<p><font color='#62472C'>$total_pages Result(s)</font> </p>";
// pagination
echo $paginate;
?>
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
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
<?
$conclose= mysql_connect ("localhost", "root", "");
mysql_select_db("results");
$query = mysql_query ($query);
$numrows = mysql_num_rows ($query);
if ($query > 0) {
while ($row = mysql_fetch_assoc($numrows)){
$id = $row ['id'];
$title = $row ['title'];
$image = $row ['image'];
$keywords = $row ['keywords'];
$link = $row ['link'];
echo "<div class='floater'> <h3><a href ='$link'><img src='$image' width='180px' height='140px'><br /><font color='#62472C'>$title</font></a></h3></div>";
}
}
else
echo "<p><font color='#62472C'>No games found for: $k </font></p>";
var_dump($row);
?>
$conclose= mysql_connect ("localhost", "root", "");
mysql_select_db("results");
$query = mysql_query ($query);
$numrows = mysql_num_rows ($query);
if ($query > 0) {
while ($row = mysql_fetch_assoc($numrows)){
$id = $row ['id'];
$title = $row ['title'];
$image = $row ['image'];
$keywords = $row ['keywords'];
$link = $row ['link'];
echo "<div class='floater'> <h3><a href ='$link'><img src='$image' width='180px' height='140px'><br /><font color='#62472C'>$title</font></a></h3></div>";
}
}
else
echo "<p><font color='#62472C'>No games found for: $k </font></p>";
var_dump($row);
?>
Gewijzigd op 19/07/2012 18:07:42 door - Ariën -
De melding die je krijgt, komt omdat je niet controleert of de query gelukt is. Je moet dit altijd controleren en hier heb je foutafhandeling voor nodig.
mysql_fetch_assoc gaat fout.
$row = mysql_fetch_assoc($numrows))
mysql_num_rows geeft het aantal rijen terug in de resultset. Dat is dus een getal.
mysql_fetch_assoc verwacht juist de resource naar de resultset om er een rij uit te halen. Die resource is $query.
In de tweede gequote regel moet je dus $query meegeven, niet $numrows.
En dan nog eens goede foutafhandeling inbouwen....
Stap 1) Vertaal de error naar het Nederlands:
Quote:
mysql_fetch_assoc verwacht een resource (iets wat mysql_query teruggeeft) als parameter, maar er wordt een getal gegeven.
Stap 2) Ga naar de regel, zoek op welke variabele je meegeeft aan de functie
Stap 3) Var_dump die variabele, welke waarde heeft die?
Stap 4) Ga na hoe het die waarde kan hebben door te kijken hoe je hem gedefinieerd hebt en wat de eventuele return values van deze functie zijn
Stap 5) Lees de documentatie van mysql_fetch_assoc om erachter te komen welke waarde er precies verwacht wordt
Stap 6) Kom je er nog steeds niet uit? Haal dan de persoonlijke gegevens (bestandsnaam, regelnummer) uit de error melding en ga zoeken op google.
Stap 7) Vraag het op een forum, plaats alleen relevante code
het moest zo
<qoute>if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)){
</qoute>
dank je wel ik zoe volgende relevante code schrijven
Gewijzigd op 19/07/2012 18:38:23 door daniyal hussain