-2

This is my first post in this community. I am a new to PHP and MySQL. Here is the code that I am trying to run.

I'm using MAMP. I have tested that I am able to establish connection to the "pet" database. Checked my user name and password, that's correct too. I have data in the database with "horse" in the field "petType".

There is got to be something wrong in the code below. I have scratched my head and looked in books and online, but couldn't figure out what I am doing wrong? With the code below, my webpage comes blank.

I'm trying to display records from the database as the first part and worry about formatting later.


<?php

$host = 'localhost';
$username = ' ';
$password = ' ';
$database = 'pet';

$conn = mysqli_connect($host, $username, $password, $database);

$query= "SELECT * FROM pet WHERE petType = 'horse'";

while($row = mysqli_fetch_array($query)):
{   
$petDescription = $query [$petDescription];
$petName = $query [$petName];
$petType = $query [$petType];

echo "$petType <br> <br> $petName <br> $petDescription";

}

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

3

Ok, here's the rundown.

  • You're not querying. This means that you need to use the mysqli_query() function, which requires a successful DB connection established and passed as the first parameter.

  • Then you're assigning $row in the while loop to fetch the array, yet you're using $query as its replacement for the intended rows to be echo'd. It should be $row and not $query.

(Edit) You also have a colon in while($row = mysqli_fetch_array($query)): <= delete it.

Now, it's hard to know for certain as to what your actual column names are in your table, so I'll just replace them with generic column_x, column_y and column_z for now.

You can replace those with the ones that are in your table. More on this a bit further below.

Replace your current block of code with the following:

$query = mysqli_query($conn, "SELECT * FROM pet WHERE petType = 'horse'");

    while($row = mysqli_fetch_array($query))
    {   
        $petType = $row['column_x'];
        $petName = $row['column_y'];
        $petDescription = $row['column_z'];

        echo "$petType <br> <br> $petName <br> $petDescription";

    }

If what your other columns are indeed called petType, petName and petDescription, then you can replace the above with:

$petType = $row['petType'];
$petName = $row['petName'];
$petDescription = $row['petDescription'];

The way it works is this, and I will show you in a graphical way:

$petType = $row['petType'];
    ^        ^      ^
    |        |      | column name in table
    |        |
    |        | variable in loop
    |
    | variable to echo

However, there is something that seems a bit unclear and that both your DB and table hold the same name of pet.

Make sure that is indeed the case.

Add or die(mysqli_error($conn)) to mysqli_query().

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Hey, you guys are awesome. I tried the suggestions that you gave me yet, the code isn't working. My database and table are both "pet" and fields are as you see below.New code- $host = 'localhost'; $username = ' '; $password = ' '; $database = 'pet'; $conn = mysqli_connect($host, $username, $password, $database); $query = mysqli_query($conn, "SELECT * FROM pet WHERE petType = 'horse'"); while($row = mysqli_fetch_array($query)): { $petType = $row['$petType']; $petName = $row['$petName']; $petDescription = $row['petDescription']; echo "$petType

    $petName
    $petDescription"; }
    – divinelotus Jan 04 '15 at 03:11
  • @divinelotus You're welcome. You will need to define "isn't working" though. Go over my entire answer again and very carefully. – Funk Forty Niner Jan 04 '15 at 03:11
  • @divinelotus the last part of your code reads as `
    $petName
    $petDescription"; } ` that should read as `
    $petName
    $petDescription"; } ?>` - notice the `` that should be `?>`
    – Funk Forty Niner Jan 04 '15 at 03:16
  • @divinelotus Plus, I spotted another thing, a colon in `while($row = mysqli_fetch_array($query)):` which should not be there and I have deleted it in my answer. An oversight on my part. It should just be `while($row = mysqli_fetch_array($query))` - TBH, my screen is a bit dirty and I didn't see it originally because of it lol – Funk Forty Niner Jan 04 '15 at 03:18
  • 1
    Awesome, Fred -ii-, it worked!!!! Thanks a million. I was struggling for the past three days to make this work and it worked because of you!!! You are a rock-star.. – divinelotus Jan 04 '15 at 03:22