-2

I have a MySQL database and am trying to print out the contents of specific rows. For some reason echo $row[1]; prints out the contents of row 1 perfectly, but I'm unable to do the same for the other rows (echo $row[2];, echo $row[3];, etc). The table has a unique index column with four rows labeled 1-4.

What I'd eventually like to do is print out just the contents of the last row, which I thought would look something like echo $row['$maxrows']; however I can't even get the syntax for printing any row past 1!

I think that this may be an issue with my table, but can't quite see what it is as there is an index column. Any suggestions or pointers would be appreciated?

What might I do to echo rows in my table past row 1?

UPDATE

var_dump($row); returns the below

{ [0]=> string(1) "1" ["Index"]=> string(1) "1" [1]=> string(121) "https://www.tilley.com/media/catalog/product/cache/image/1100x1100/e9c3970ab036de70892d86c6d221abfe/t/t/ttw2_black2_a.jpg" ["Sketch"]=> string(121) "https://www.tilley.com/media/catalog/product/cache/image/1100x1100/e9c3970ab036de70892d86c6d221abfe/t/t/ttw2_black2_a.jpg" }

The table has two columns:

1-Name: Index - int(11) - AUTO_INCREMENT    
2-Name: Sketch - text

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM Table";
$result = mysqli_query($conn, $sql);

$maxrows = mysqli_num_rows($result);

//$result = $conn->query("SELECT * FROM Table");
$row = $result->fetch_array(MYSQL_BOTH);
echo $maxrows;
echo $row[1];
echo $row[2];
echo $row[3];
echo $row[4];

//echo $result;

$conn->close();
John Conde
  • 207,509
  • 96
  • 428
  • 469
user586011
  • 1,783
  • 4
  • 16
  • 24
  • hoe many columns have your table .. update you question and add the related schema – scaisEdge Aug 26 '18 at 16:08
  • 3
    What does `var_dump($row);` show? That's always the first debugging step (if you don't use an IDE with xdebug that is). – John Conde Aug 26 '18 at 16:08
  • 1
    Also remember when using array indexes for column access... it starts with `[0]`. And when you do something like `$row['$maxrows'];` you are trying to access a keyname of `$maxrows` and not the actual value number... because you used single quotes. And if you still tried to do `$row[$maxrows];` without single quotes... it would not show the last column, since indexes start with zero and maxrows is the number of ROWS returned, and not COLS! :) – IncredibleHat Aug 26 '18 at 16:23
  • `MYSQL_BOTH` you can't mix different apis here, that should read as `MYSQLI_BOTH`. – Funk Forty Niner Aug 26 '18 at 16:28
  • Arrays are zero-based btw. – Funk Forty Niner Aug 26 '18 at 16:28
  • And avoid mixing procedural mysqli with object oriented. It just confuses matters unless you are really versed in the intricacies of both, which you are not based on this question. – IncredibleHat Aug 26 '18 at 16:31
  • var_dump($row); returns... { [0]=> string(1) "1" ["Index"]=> string(1) "1" [1]=> string(121) "https://www.tilley.com/media/catalog/product/cache/image/1100x1100/e9c3970ab036de70892d86c6d221abfe/t/t/ttw2_black2_a.jpg" ["Sketch"]=> string(121) "https://www.tilley.com/media/catalog/product/cache/image/1100x1100/e9c3970ab036de70892d86c6d221abfe/t/t/ttw2_black2_a.jpg" } – user586011 Aug 26 '18 at 17:01
  • columns aren't named 1, 2, etc! I've added the table schema to the question. – user586011 Aug 26 '18 at 17:06
  • when I change mysql_both to mysqli_both var_dump($row); returns NULL – user586011 Aug 26 '18 at 17:10

1 Answers1

0

Your result array has 4 keys: 0, Index, 1 and Sketch, that's the results of the first row only. You're querying 2 columns but try to access 4 columns. Column numbering starts with 0. And you're not iterating the result rows. That $row = $result->fetch_array(MYSQL_BOTH); should be in a loop.

  1. Turn on error reporting. PHP would show you which undefined variables your accessing.
  2. Use PDO in favor to mysqli.
  3. Don't create an array with both column name indexes and numeric indexes (don't use MYSQL_BOTH). Use PDO::FETCH_ASSOC or PDO::FETCH_NUM.
  4. Start using var_dump() instead of echo for debugging. Always.
  5. Use a foreach loop when processing the result array.
steffen
  • 13,255
  • 2
  • 36
  • 69
  • I'm trying to print the most recently created row, how is this possible using a loop? Can't I use SELECT more easily? – user586011 Aug 26 '18 at 21:07
  • 1
    @user586011 If you want to select only the most recent row, you have to add a 'created' or 'updated' column. There's no guarantee about which row you would receive with a single select - you have to formulate that by adding an `ORDER BY` clause in your `SELECT`. – steffen Aug 26 '18 at 22:48
  • I tried adding an Index column and "SELECT * FROM TableName ORDER BY Index DESC LIMIT 1" but got the error "Notice: Trying to get property of non-object in /home2/hotelowl/public_html/inkstationery/fetch2.php on line 23 Total results: bool(false)" – user586011 Aug 29 '18 at 15:23
  • @user586011 It's good that you see PHP Notices now. If you're with PDO, you should really enable Exceptions. See here https://stackoverflow.com/a/32648423/845034. – steffen Aug 29 '18 at 18:36