-2

I just one to show one thing. Randomly generated with an ID. Every ID has a string of text. I want to show the string only. I get an error when I try this:

$RandInt=rand(1, 2);
$query="SELECT 'text' FROM quotestable WHERE 'ID' = $RandInt"; 
mysql_result($query, 1);  

Warning: mysql_result() expects parameter 1 to be resource, string given

user3604398
  • 31
  • 1
  • 1
  • 3
  • 1
    Remove your quotes from 'text' and 'id'. Also, your query never gets executed. – ElGavilan May 08 '14 at 11:59
  • You are not even executing your query. [mysql_result()](http://www.php.net/manual/en/function.mysql-result.php) expects a resultset object while you are sending it a string. You first need to execute your query and then pass the resultset to `mysql_result()`. – AyB May 08 '14 at 11:59
  • possible duplicate of [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – Michael Berkowski May 08 '14 at 13:00

4 Answers4

1

Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

You execute a query as follows:

// (text and ID should be escaped using backticks, since they are variables)
$query = "SELECT `text` FROM quotestable WHERE `ID` = $RandInt"; 
$result = mysql_query($query); 

And then fetch a row from the $result:

$row = mysql_fetch_assoc($result);

And then use the $row fetched wherever you want:

echo $row["text"];
John Bupit
  • 10,406
  • 8
  • 39
  • 75
0

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future.

Besides of the fact that you're using deprecated functions:

#1 Your query has some mistakes:

$query = "SELECT text FROM quotestable WHERE id = '$RandInt'";
  • Remove the ' from text, add ' to $RandInt

#2 You aren't executing your query, do:

$result = mysql_query($query);
mysql_result($result,1);

Instead of using deprecated functions, you should use mysqli.

Robin
  • 1,208
  • 8
  • 17
0
myslq_connect('localhost','db_username','db_password');  //Your db criterias
$RandInt=rand(1, 2);
$query="SELECT text FROM quotestable WHERE 'ID' = $RandInt"; 
$result = mysql_result($query);

$row = mysql_fetch_array($result, MYSQL_ASSOC);

echo $row['text'];
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
0

User mysql_query() before mysql_result()

$RandInt=rand(1, 2);
$query="SELECT text FROM quotestable WHERE ID = $RandInt"; 
$result = mysql_query($query);
echo mysql_result($result, 1); 

but i suggest you to use "mysqli_" functions as "mysql_" functions are deprecated

PravinS
  • 2,640
  • 3
  • 21
  • 25
  • `mysql_query($query);` maybe? Also, I'm not sure whether single quotes around the column name is valid. – AyB May 08 '14 at 12:03