4

I am a beginner learning PHP and MySQL and have gotten to chapter 5 of Head First PHP & MySQL and am attempting a self made project in which I created a database and a index.php page where I can see the results printed out. When I go to my index.php page I see the HTML title but the PHP code is not printing out my submissions. I have to assume my code syntax is correct or I would end up with a blank page. Can someone please tell me what I have coded wrong to wind up with no output?

<?php

$dbc = mysqli_connect(localhost, root, root, itmyfamily);
$query = "SELECT * FROM itsmyfamily ORDER BY last_name ASC, first_name DESC, date ASC";
$data = mysqli_query($dbc, $query);
$i = 0;

while ($row = mysqli_fetch_array($data))
{
    if ($i == 0)
    {
        echo '<strong>First Name:</strong> ' . $row['first_name'] . ' <br />';
        echo '<strong>Last Name:</strong> ' . $row['last_name'] . ' <br />';
        echo '<strong>Spouse Name:</strong> ' . $row['spouse_name'] . ' <br />';
        echo '<strong>Email:</strong> ' . $row['email'] . ' <br />';
    }
    else
    {
        echo 'There is no info in the database';
    }
    $i++;
}

mysqli_close($dbc);
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Formatting 101: paste your code, select it, press Ctrl + K. – Amal Murali Apr 28 '14 at 04:05
  • 2
    Be sure to use `htmlspecialchars()` around any arbitrary data used in the context of HTML. Otherwise, you open yourself up to potential XSS attacks. At a minimum, you may be creating invalid HTML since you aren't escaping anything in your HTML. – Brad Apr 28 '14 at 04:06
  • Head First is a good source. Enjoy :) – James P. Apr 28 '14 at 04:06
  • What happens when you run `var_dump($data)`? What's in your error log? – Brad Apr 28 '14 at 04:07
  • 1
    Step 1: [Enable error reporting](http://stackoverflow.com/a/6575502/1438393). – Amal Murali Apr 28 '14 at 04:07
  • Tip: If you can run your query in phpMyAdmin to see if executes ok. – James P. Apr 28 '14 at 04:08
  • Also, you can use `echo "
    "; print_r($data);echo "
    ";` or vardump to print out a memory structure on your page for troubleshooting purposes.
    – James P. Apr 28 '14 at 04:10
  • Brad, I have no idea how to run the var_dump($data). I haven't got that far in the book. – user3495621 Apr 28 '14 at 04:25
  • @Armal, not sure how to enable error reporting. Are we talking about editing the .ini file? – user3495621 Apr 28 '14 at 04:34
  • Did you intend for both your mysqli_connect and SELECT statements to be itmyfamily or are both supposed to be itsmyfamily? Or are they both intended to have different names (one is itmyfamily and the other is itsmyfamily)? – The One and Only ChemistryBlob Apr 28 '14 at 04:36
  • @Chemistry Bob, yeah I caught that after I had entered data in to the table so I said what the heck. – user3495621 Apr 28 '14 at 04:50
  • I do not understand what happened, because I did not change one line of code but only the first row is actually echoing now. I have 3 records in the table, the last two echo the else statement instead of echoing their actual data. – user3495621 Apr 28 '14 at 04:53
  • 1
    @user3495621 if I follow your logic, you're initializing `$i` with 0. Then the `while` enters the loop, the `if` checks the value of `$i` before it is incremented further on. That would mean `$i` is only at value 0 once and it would explain why the last two echos or going through the `else`. What should it do instead ? – James P. Apr 28 '14 at 05:22
  • @user3495621 Btw, `var_dump` can be entered anywhere as an instruction in your code. Find a memory structure you want to print like `$data` , make sure it has a value and then place `var_dump($data);` just after it. Same applies to `print_r($data);`. – James P. Apr 28 '14 at 05:27
  • @james, so should I set i not equal to zero and then initialize the if with i=0? I just want the loop to start with first record and stop when there is no more. I realize I sound elementary but that all I am trying to do – user3495621 Apr 28 '14 at 12:10
  • The while takes care of records here. See `while ($row = mysqli_fetch_array($data))` ? That's basically looping each time `$row` gets something through `mysqli_fetch_array($data)`. If you want to display a convenient message you could have a variable set to false before the while and check if it's still false after the loop to print 'There is no info in the database'. – James P. Apr 29 '14 at 02:16

1 Answers1

4

Set this on top of the source code to display errors.

<?php error_reporting( E_ALL ); ?>

Or set display_erros in php.ini as follows:

display_errors = On

error_reporting = E_ALL | E_STRICT

Also try to replace your source code with following,

<?php

  //Establish connection with database

  $dbc = mysqli_connect(localhost,root,root,itmyfamily);

  //Order the data to be retrieved

  $query = "SELECT * FROM itsmyfamily ORDER BY last_name ASC, first_name DESC, date ASC";

  //Execute the connect command and the query 

$data = mysqli_query($dbc,$query);


  while ($row = mysqli_fetch_array($data)) {

//Loop through the array of family submissions, formatting it as html

  $i = 0;

  //Display family submissions

    if ($i == 0) {

    echo '<strong>First Name:</strong> ' .$row['first_name']. ' <br />';
    echo '<strong>Last Name:</strong> ' .$row['last_name']. ' <br />';
    echo '<strong>Spouse Name:</strong> ' .$row['spouse_name']. ' <br />';
    echo '<strong>Email:</strong> ' .$row['email']. ' <br />';

}
    else{
        echo 'There is no info in the database';       
  }
  $i++;


  }


    mysqli_close($dbc);

  ?>

If you can write it this way, I think you don't even need the $i.

$result = mysql_query("SELECT id, first_name FROM mytable");

if($result){

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["first_name"]);
}

}
else
 echo 'There is no info in the database'; 

Read here for more information about PHP mysql_fetch_array http://www.php.net/mysql_fetch_array

Read here for more information about iterations. http://webcheatsheet.com/php/loops.php

Please note that the method you have used is deprecated from PHP 5.5.0. So I suggest you consider mysqli or PDO. Examples can be found in below PHP manual links

http://www.php.net/manual/en/mysqli.query.php

http://www.php.net/manual/en/pdo.query.php

Dharman
  • 30,962
  • 25
  • 85
  • 135
dev1234
  • 5,376
  • 15
  • 56
  • 115
  • `$i` seems to be a check for the presence of records. The OP should clarify. – James P. Apr 28 '14 at 05:28
  • 2
    @James you are correct. $i is my counting variable to be incremented after each pass. Sorry for not disclosing this. I assume that you all can read my code and therefore automatically see what I am trying to do. I apologize for this and will try to post less ambiguous questions in the future. – user3495621 Apr 28 '14 at 20:31
  • Ok, I did finally get the script to work. I moved the $i = 0 from outside the While statement to inside of the While statement and now all three records are echoed properly. Can someone please tell me why the $i=0 has to be inside of the while statments. – user3495621 Apr 28 '14 at 20:36
  • That's because `$i` is reset each time the loop is run. I'm reading the code and my guess is that the original intent was to have an true-false equivalent to show a message if no records are found. If so, some kind of check could be done on `$data` to see if it contains anything. Is that how it was written initially ? – James P. Apr 29 '14 at 02:13