0

I know that the above message occurs becomes of wrong MySQL query, but what's wrong with my MySQL query here in this code? I did a lot of research to find it, but couldn't find the resolution. Table name: pic_msg Columns: image_path, ip, index(auto_increment)

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

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


<?php
$test_ip = "test-ip";

$sql = "SELECT image_path FROM pic_msg  WHERE ip = '$test_ip' ORDER BY index DESC LIMIIT 1";
$result = mysqli_query($con, $sql);
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["image_path"]."<br>";
    }
} else {

}

mysqli_close($con);
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Yogie
  • 986
  • 2
  • 12
  • 14
  • 1
    **WARNING**: When using `mysqli` you should be using parameterized queries and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation to accomplish this because you will create severe [SQL injection bugs](http://bobby-tables.com/). You're also doing zero error checking here, which is the source of a lot of your problems. – tadman Dec 03 '14 at 21:06
  • 1
    Check your spelling... `LIMIIT` – gen_Eric Dec 03 '14 at 21:06

1 Answers1

1

index is a reserved word - you should escape it. Additionally, you have a typo - it's limit, not limiit:

$sql = "SELECT `image_path` FROM `pic_msg` WHERE `ip` = '$test_ip' ORDER BY `index` DESC LIMIT 1";
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thanks a lot bro, may god bless you! Where can I study more about MYSQL in layman language (language similar to w3cschools)? Thanks again :) My code started working. If I had reputations, I'd have voted you up, for sure. – Yogie Dec 03 '14 at 21:32