-2

Working on my first PHP blog system, I cannot display the result of my query from the page articles.php

include ('config/dbconnect.php');
function selectArticleById( $IdArticle, $c ){

$sqlArticlesId = 'SELECT 
            artID,
            artTitre,
            artAuteur,
            artContenu,
            artDate
            FROM articles
            WHERE artID =\''.$IdArticle.'\'
            ';
$result = mysqli_query($c, $sqlArticlesId); 
return $result;

}

Calling this function in my menu.inc.php

<?php 

$rArticlesId = selectArticleById( $row['artID'], $conn ); ?>
<?php while($row = mysqli_fetch_array($rArticlesId)){ ?> 

<ul class="menu">

<li><a href="index.php?page=articleform"><?php echo $row['artTitre']; ?></a></li>
<?php echo $row['artTitre']; ?>
</ul>
<?php } ?>

I get the error : undefined variable row in. SQL injections will be dealt accordingly. I just would like to understand how the function works with 2 variables.

swissed
  • 49
  • 1
  • 2
  • 8

3 Answers3

4

The $row in the below line is undefined.

$rArticlesId = selectArticleById( $row['artID'], $conn ); ?>

Maybe you mean $_GET['artID'] ?

$rArticlesId = selectArticleById( $_GET['artID'], $conn ); ?>
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • @swissed if you can see your code and let us know from where you are getting $_GET['artID']. it will be easy to debug – Maz I Jan 06 '14 at 11:18
0

you are using $row in this line $rArticlesId = selectArticleById( $row['artID'], $conn ); ?>

while $row is created in the line below

<?php while($row = mysqli_fetch_array($rArticlesId)){ ?> 

You need to use the correct variable here.

Maz I
  • 3,664
  • 2
  • 23
  • 38
0

Thank you for all who helped me out. Answering my own question: I declared row within menu.inc.php:

<?php 
$rows =  mysqli_fetch_assoc($rArticles); //THIS IS WHAT I ADDED
$rArticlesId = selectArticleById( $rows['artID'], $conn ); ?>
<?php while($row = mysqli_fetch_array($rArticlesId)){ ?> 

 <ul class="menu">

<li><a href="index.php?page=articleform"><?php echo $row['artTitre']; ?></a></li>
<?php echo $row['artTitre']; ?>
</ul>
<?php } ?>
swissed
  • 49
  • 1
  • 2
  • 8