[MySQLI] Verschillende paginas

Gestart doorFreestyle5 reacties25 weergaven

6 berichten · pagina 1 van 1

Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Hallo,

Ik ben nu bezig met MYSQLI en ik loop nu al tegen een paar dingen aan. Wat ik niet heel erg duidelijk vind. (het gaat om MYSQLI via OOP).

Nu heb ik dus in mijn config.php de connectie via MYSQLI gemaakt naar mijn database. wat allemaal goed werkt.

En als ik nu in een andere pagina een Query wil uit voeren krijg ik een error, deze error word verholpen als je global $mysqli; doet. Maar dat lijkt mij niet handig om dat boven elke pagina te doen. Dus MOET dit persee? Of underdruk ik nu iets. Via google word het mij niet echt duidelijker.

Het heeft ook geen zin om onder aan de config.php global $mysqli; te zetten, helaas.

Om een voorbeeld te geven:
Config.php
Spoiler tonen
/**
* Get the database Connection
 */[COLOR= #0000BB]<?php
$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]localhost[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #DD0000]localhost[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]usr[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #DD0000]root[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]usr_pass[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #DD0000]pass[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]table[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #DD0000]table[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #0000BB]$mysqli[/COLOR] [COLOR= #007700]= new[/COLOR] mysqli[COLOR= #007700]([/COLOR][COLOR= #0000BB]$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]localhost[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #0000BB]$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]usr[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #0000BB]$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]usr_pass[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #0000BB]$con[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]table[/COLOR][COLOR= #007700]]);[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR]mysqli_connect_errno[COLOR= #007700]()) {[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Couldnt connect to Database"[/COLOR][COLOR= #007700];
}[/COLOR]?>
index.php
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR] [URL="http://www.php.net/require"]require[/URL] [COLOR= #DD0000]config.php[/COLOR][COLOR= #007700];[/COLOR] [URL="http://www.php.net/include"]include[/URL] [COLOR= #DD0000]home.php[/COLOR][COLOR= #007700];[/COLOR] ?>
home.php FOUT
Spoiler tonen
[COLOR= #0000BB]<?php
$resultaat[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$mysqli[/COLOR][COLOR= #007700]->[/COLOR]query[COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM pages"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][COLOR= #0000BB]$resultaat[/COLOR][COLOR= #007700]) {[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Query Error:"[/COLOR][COLOR= #007700];
    }[/COLOR] [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
        [URL="http://www.php.net/while"]while[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$record[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$resultaat[/COLOR][COLOR= #007700]->[/COLOR]fetch_assoc[COLOR= #007700]())
        {[/COLOR]
            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<tr> 
 "[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pId[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]" - "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pTitle[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pTags[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pDate[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pAuthor[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pStatus[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"</tr> 
"[/COLOR][COLOR= #007700];
        }
    }[/COLOR]
?>
home.php GOEd
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/global"]global[/URL] [COLOR= #0000BB]$mysqli[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #0000BB]$resultaat[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$mysqli[/COLOR][COLOR= #007700]->[/COLOR]query[COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM pages"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][COLOR= #0000BB]$resultaat[/COLOR][COLOR= #007700]) {[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Query Error:"[/COLOR][COLOR= #007700];
    }[/COLOR] [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
        [URL="http://www.php.net/while"]while[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$record[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$resultaat[/COLOR][COLOR= #007700]->[/COLOR]fetch_assoc[COLOR= #007700]())
        {[/COLOR]
            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<tr> 
 "[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pId[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]" - "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pTitle[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pTags[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pDate[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pAuthor[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
                [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"	<td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]pStatus[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
"[/COLOR][COLOR= #007700];[/COLOR]
            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"</tr> 
"[/COLOR][COLOR= #007700];
        }
    }[/COLOR]
?>
In het kort, Kan ik die global op een of andere manier weg laten?? of hoort dit zo?

Alvast bedankt,

Marijn
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Je include je config niet
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Aangepast, maar dat is het niet.
0
Thom

Lid

Berichten
10.654
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
global moet toch alleen in functies?
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dat dacht ik dus ook.
Maar $mysqli is wel een object (met functies dus)

En daarom vind ik het ook zo raar, en vraag ik het hier.

toevoeging op 2 maart 2013, 151352:
Ik denk dat ik het al weet. Want in mijn index include ik de paginas door middel van een class.

nu heb ik de global op de plek gezet waar hij ook de pagina include. En nu doet hij het ook.
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
nee $mysqli is gewoon een variable, dan hoef je geen global te doen
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren