PHP error
infokab
Posted messages
23
Status
Membre
-
infokab Posted messages 23 Status Membre -
infokab Posted messages 23 Status Membre -
Hello
I'm trying to create a small website, but I have this error:
Fatal error: Call to a member function query() on null in C:\xampp\htdocs\tp3\etudiant\SiteWebShop\index.php on line 26
I don't know if you can help me
Thank you
I'm trying to create a small website, but I have this error:
Fatal error: Call to a member function query() on null in C:\xampp\htdocs\tp3\etudiant\SiteWebShop\index.php on line 26
I don't know if you can help me
Thank you
2 réponses
Re,
Well... apparently you haven't read any of my links BEFORE posting your code!
So:
1 - Read (and apply...) the content of the links I provided.
2 - Show us your COMPLETE code!
In the small piece of code you shared that has issues... the call to the database connection function is missing.....
--
Sincerely,
Jordane
Well... apparently you haven't read any of my links BEFORE posting your code!
So:
1 - Read (and apply...) the content of the links I provided.
2 - Show us your COMPLETE code!
In the small piece of code you shared that has issues... the call to the database connection function is missing.....
--
Sincerely,
Jordane
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<title>WebShop</title>
</head>
<body>
<?php include 'header.php' ; ?>
<!-- BEGIN of the page -->
<?php
include 'utile.php';
include_once 'connexion.php';
$connexion =connexionBd();
$requete = "SELECT * FROM article ORDER BY RAND() LIMIT 3" ;
$sql=$connexion->query($requete); // send the query
$res=$sql->fetchALL(PDO::FETCH_ASSOC); // return all results
?>
<!--<header>
<div class="wrap">
<h1><a href="#" rel="nofollow noopener noreferrer" target="_blank"><img src="images/openshop_logo.png" alt="logo open shop"></a></h1>
<nav>
<ul>
<li><a href="index.php" rel="nofollow noopener noreferrer" target="_blank">home</a></li>
<li><a href="login.php" rel="nofollow noopener noreferrer" target="_blank">login</a></li>
<li><a href="creer_compte.php" rel="nofollow noopener noreferrer" target="_blank">create account</a></li>
<li><a href="panier.php" rel="nofollow noopener noreferrer" target="_blank">cart</a></li>
</ul>
</nav>
<form id="search" action="recherche.php" method="post" enctype="multipart/form-data">
<p>
<label for="searchText">Search :</label>
<input id="searchText" name="query" type="text" value="" />
<input id ="searchBtn" type="submit" class="button" value="OK" />
</p>
</form>
<nav id="menu-categorie">
<ul>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">all products</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">clothes</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">accessories</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">posters</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">dvd</a></li>
</ul>
</nav>
</div>
</header> -->
<section>
<header>Welcome <span class="ss-titre">Today is ?????? </span></header>
<p>The online store <strong>openSHOP</strong> is a project created by <em>Thomas Jouannic</em> & <em>Jérome Saunier</em>
then modified and adapted <strong>during Advanced Web Sites</strong>.</p>
<p>In the upper part, you will find a way to login or create an account if you do not have one. The search field
allows you to easily display products matching what you want. You can also navigate between the different
product categories by clicking on the one you wish to see.</p>
<p>Happy browsing !</p>
</section>
<section>
<header>
<h2>Random...</h2>
</header>
<!--Displaying 3 random articles -->
<ul id= "product-list" >
<?php
foreach ( $res as $key => $val2)
echo "<li class ='product'>";
echo "<h3>".$value["Designation"]."</h3>";
echo "<p><img alt='' src='".$value['img_article']."'></p></img>";
echo "<p id='price'>".$value['Prix']."€</p>";
echo "<p>".tronquer_texte($value['description'])."</p>";
echo '<p><a href="view_product.php?article='.$value[" rel="nofollow noopener noreferrer" target="_blank"id_article"].'">See details</a></p>';
echo '</li>';
?>
</ul>
</section>
<?php include('footer.php') ; ?>
<!--<footer>
<p> <a id="goto-admin" href="admin.php" rel="nofollow noopener noreferrer" target="_blank">Admin</a></p>
<p id="extra">
Advanced Web Sites - Year 2016 - HTML 5 version
</p>
</footer> -->
</body>
</html> ```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<title>WebShop</title>
</head>
<body>
<?php include 'header.php' ; ?>
<!-- BEGIN of the page -->
<?php
include 'utile.php';
include_once 'connexion.php';
$connexion =connexionBd();
$requete = "SELECT * FROM article ORDER BY RAND() LIMIT 3" ;
$sql=$connexion->query($requete); // send the query
$res=$sql->fetchALL(PDO::FETCH_ASSOC); // return all results
?>
<!--<header>
<div class="wrap">
<h1><a href="#" rel="nofollow noopener noreferrer" target="_blank"><img src="images/openshop_logo.png" alt="logo open shop"></a></h1>
<nav>
<ul>
<li><a href="index.php" rel="nofollow noopener noreferrer" target="_blank">home</a></li>
<li><a href="login.php" rel="nofollow noopener noreferrer" target="_blank">login</a></li>
<li><a href="creer_compte.php" rel="nofollow noopener noreferrer" target="_blank">create account</a></li>
<li><a href="panier.php" rel="nofollow noopener noreferrer" target="_blank">cart</a></li>
</ul>
</nav>
<form id="search" action="recherche.php" method="post" enctype="multipart/form-data">
<p>
<label for="searchText">Search :</label>
<input id="searchText" name="query" type="text" value="" />
<input id ="searchBtn" type="submit" class="button" value="OK" />
</p>
</form>
<nav id="menu-categorie">
<ul>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">all products</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">clothes</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">accessories</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">posters</a></li>
<li class="smenu"><a href="#" rel="nofollow noopener noreferrer" target="_blank">dvd</a></li>
</ul>
</nav>
</div>
</header> -->
<section>
<header>Welcome <span class="ss-titre">Today is ?????? </span></header>
<p>The online store <strong>openSHOP</strong> is a project created by <em>Thomas Jouannic</em> & <em>Jérome Saunier</em>
then modified and adapted <strong>during Advanced Web Sites</strong>.</p>
<p>In the upper part, you will find a way to login or create an account if you do not have one. The search field
allows you to easily display products matching what you want. You can also navigate between the different
product categories by clicking on the one you wish to see.</p>
<p>Happy browsing !</p>
</section>
<section>
<header>
<h2>Random...</h2>
</header>
<!--Displaying 3 random articles -->
<ul id= "product-list" >
<?php
foreach ( $res as $key => $val2)
echo "<li class ='product'>";
echo "<h3>".$value["Designation"]."</h3>";
echo "<p><img alt='' src='".$value['img_article']."'></p></img>";
echo "<p id='price'>".$value['Prix']."€</p>";
echo "<p>".tronquer_texte($value['description'])."</p>";
echo '<p><a href="view_product.php?article='.$value[" rel="nofollow noopener noreferrer" target="_blank"id_article"].'">See details</a></p>';
echo '</li>';
?>
</ul>
</section>
<?php include('footer.php') ; ?>
<!--<footer>
<p> <a id="goto-admin" href="admin.php" rel="nofollow noopener noreferrer" target="_blank">Admin</a></p>
<p id="extra">
Advanced Web Sites - Year 2016 - HTML 5 version
</p>
</footer> -->
</body>
</html> ```
Once and for all .... can you
Read the explanations here: https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
USE CODE TAGS
to post your code on the forum???!!!!Read the explanations here: https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
<?php
define("SERVER","localhost");
define("USER","root");
define("PASSWORD","");
define("DB","sitewebshop");
function connectDb($host=SERVER,$username=USER,$password=PASSWORD,$db=DB)
{
try
{
$connex= new PDO('mysql:host='.$host.';dbname='.$db, $username, $password);
$connex->exec("SET CHARACTER SET utf8"); //Character set management
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $connex;
}
catch(Exception $e)
{
echo 'Error: '.$e->getMessage().'<br />';
echo 'No: '.$e->getCode();
return null;
}
}
?>
this is my connection file
thank you
the error message means that $connection is "null".
Where did you initialize it??
Knowing that to help you... having the entire code is still ideal!!
NB: If you are using PDO: https://forums.commentcamarche.net/forum/affich-37584941-php-pdo-gerer-les-erreurs
NB²: To post your code on the forum: https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
NB3: Read this before .. so that you show us "clean" code!
https://forums.commentcamarche.net/forum/affich-37584947-php-gestion-des-erreurs-debogage-et-ecriture-du-code
Thank you for your response
Here is my connection file:
<?php define("SERVEUR","localhost"); define("USER","root"); define("MDP",""); define("BD","sitewebshop"); function connexionBd($hote=SERVEUR,$username=USER,$mdp=MDP,$bd=BD) { try { $connex= new PDO('mysql:host='.$hote.';dbname='.$bd, $username, $mdp); $connex->exec("SET CHARACTER SET utf8"); //Managing accents return $connex; } catch(Exception $e) { echo 'Error: '.$e->getMessage().'<br />'; echo 'No: '.$e->getCode(); return null; } } ?>EDIT: Added code tags!
$requete = "SELECT * FROM article ORDER BY RAND() LIMIT 3" ;
$sql=$connexion->query($requete); // sending the query
$res=$sql->fetchALL(PDO::FETCH_ASSOC); // returns all the results
thank you