-2

I have this code which retrieves data from a MySql database:

<?php
$con= mysqli_connect("localhost","root","","darrenvellaedp2");

$query = mysqli_query($con,"SELECT postTitle, pContent FROM tbl_posts WHERE userID = '2'  ");

while($rows = mysqli_fetch_array($query))
{

    $title = $rows['postTitle'];

    $content = $rows['pContent'];

}

?>

I'm getting these errors:

Notice: Undefined variable: title in C:\xampp\htdocs\WSSA1S1\index.php on line 48

Notice: Undefined variable: content in C:\xampp\htdocs\WSSA1S1\index.php on line 50

Before you tell me that this problem has already been answered in this forum and you can use isset() to solve the problem. I know, I tried but eighter I'm not implementing it right or it just can't work with my code.

I've saw this post:

PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

It's not working for me.. I did has he told in the answer but I keep getting the errors.

I'm using the variables here:

<article class="contentbox1">
            <div class="articleHEADER">
                    <!--<h2>Welcome!</h2>-->

                    <h2><?php echo "$title"?></h2>
            </div>
                <p><?php echo "$content"?></p>
                <footer>
                    <p class="post-info">This post is written by admin</p>
                </footer>

Please someone help!

Community
  • 1
  • 1
user3703944
  • 71
  • 3
  • 9
  • Look at the edited post @Jonathan Kuhn – user3703944 Jun 04 '14 at 18:42
  • BDW this was working like an hour ago but now for some damn reason it's not.. I touched nothing – user3703944 Jun 04 '14 at 18:42
  • Is the loop ever executed? Are the variables ever defined? Are they in the same scope as where you're trying to use them? – deceze Jun 04 '14 at 18:42
  • The code you show would not throw this notice as you are not trying to access the variable values. I would guess later in the code (lines 48 and 50) you are trying to access thiese variables, but the variables were never populated because you did not enter the while loop for some reason. Add some error handling/debugging to find out why. – Mike Brant Jun 04 '14 at 18:43
  • is the `article` code within a function or something? in php variables scope inside of a function means that within the function you don't have access to variables outside of the function. – Jonathan Kuhn Jun 04 '14 at 18:43

2 Answers2

3

Can you verify that the loop with $title = and $content = is actually executing?

Most likely your query is failing or you're getting zero rows back

Ford Filer
  • 325
  • 1
  • 11
  • 1
    On that note, I would dump $rows to the screen and verify that it is not returning uppercase keys in the array, as some database connection code is apt to do. – Mark Jun 04 '14 at 18:47
  • @Ford Filer Thanks, there was a problem with the query. When I got those type of errors I never looked for problems in the query. – user3703944 Jun 04 '14 at 18:54
  • Glad to hear it's fixed – Ford Filer Jun 04 '14 at 18:54
0

I have not used MySQLi but your syntax appears odd to me, maybe try this instead, from the PHP Manual (Here: http://www.php.net/manual/en/mysqli-result.fetch-array.php):

<?php
    // Establish New Connection
    $mysqli = new mysqli("localhost","root","","darrenvellaedp2");

    // Save the Query for debugging and modification later
    $query = "SELECT postTitle, pContent FROM tbl_posts WHERE userID = '2'  ";

    // Get the result by executing the query
    $result = $mysqli->query($query);

    // Format the result and assign it to a local var
    $row = $result->fetch_array(MYSQLI_NUM);

    // Assign local values
    $title = $rows['postTitle'];
    // Assign local values
    $content = $rows['pContent'];

Your looping in this case seems superfluous, as returning multiple rows will simply overwrite your values and use whichever came out of the database last.

Mark
  • 861
  • 9
  • 17