0

i have a table named "index", with 2 rows in it: title and text

the code is:

<?php
$con = mysql_connect("localhost", "elenbyin_vadim", "passr422");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("elenbyin_vadim", $con);

$sql = "SELECT * from index";
$result = mysql_query($sql,$con);

echo mysql_result($result,0);

mysql_close($con);
?>

but i'm getting an annoying error: Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/elenbyin/public_html/elenby.co.il/vadim/tryout.php on line 14

and i have no idea what to do anymore, no matter what i have tried - doesn't work! how can i fix this and get the information from the tables?

Lior Elenby
  • 41
  • 1
  • 1
  • 4
  • 1
    abandon mysql_* now. Sooner or later you'll have to because they are going to be removed from the language. http://stackoverflow.com/questions/13944956 – TecBrat Sep 22 '13 at 19:18
  • @TecBrat If you're going to preach about mysql_*, link to http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860046#12860046 – BLaZuRE Sep 22 '13 at 19:34
  • @BLaZuRE Both links are valid to make this point. I can promise you that the next time I want to post a similar link, I won't remember which one I used before, nor which one you suggested. :-) – TecBrat Sep 22 '13 at 21:30
  • @TecBrat :(. The link I gave is an excellent link for beginners who are wanting to use mysql_*. Just search for deprecated mysql_ and sort by votes. – BLaZuRE Sep 22 '13 at 23:31

4 Answers4

4

There is a problem in your query index is reserved mysql keyword try using back-ticks arround table name

SELECT * from `index`

If you try to echo the error you will get

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index

Reserved Keywords

M Khalid Junaid
  • 62,293
  • 9
  • 87
  • 115
0

The error message is telling you that $result===false which means your query did not execute. I was going to say "Check your credentials" but another answer pointed out that you have a keyword as a column name. Fix that and you'll be fine, but please see the comment I put on your question.

TecBrat
  • 3,469
  • 2
  • 26
  • 41
0

Your query has an error, you can add

if (!$result) {
die('Could not query:' . mysql_error());
}

to see it.

M Khalid Junaid
  • 62,293
  • 9
  • 87
  • 115
Technobyte
  • 32
  • 2
0

INDEX is a mysql reserved keyword .

you should first check your query has return any record or not

$sql = "SELECT * from index";
$result = mysql_query($sql,$con);
if($result)
{
echo mysql_result($result,0);
}
Rajeev Ranjan
  • 4,163
  • 2
  • 27
  • 40