Jump to content

echo specific row from Mysql table


Bentley4

Recommended Posts

Hi guys,

 

How do I call the 2nd row of 'TSuperQuestion'?

<?PHP  
$con = mysql_connect("localhost","ph6theo", "xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error("oop"));
  }
mysql_select_db("ph6theo_testDB") or die(mysql_error());
$result = mysql_query("SELECT * FROM QANDATable ");
$row = mysql_fetch_array($result);
echo $row['TSuperQuestion'];
?>

 

Link to comment
Share on other sites

If you aren't able to simply add a parameter to the query string that would select the proper record, you can use mysql_data_seek.

 

<?PHP  
$con = mysql_connect("localhost","ph6theo", "xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error("oop"));
  }
mysql_select_db("ph6theo_testDB") or die(mysql_error());
$result = mysql_query("SELECT * FROM QANDATable");

mysql_data_seek($result, 1);

$row = mysql_fetch_array($result);
echo $row['TSuperQuestion'];
?>

Link to comment
Share on other sites

Ok thats good mysql_error("oop")

but a few lines down you have or die(mysql_error());

 

Add limit 2 to the end of your query. If you date your information as it is added in or are using an auto numbering field, you could sort  by DESC.

 

Desmond.

 

Link to comment
Share on other sites

Have you given each entry in your table a unique key such as an ID?

 

If so, just alter your SQL query to only select the second row:

 

"SELECT * FROM QANDATable WHERE id = 2";

 

The above would work if your table had a column named id and the second row you are after was the 2nd record in the table.

Link to comment
Share on other sites

How do you know you want row 2 from your table, given that what is stored in second row in your table can be anything because the database engine will reuse positions in your table as you delete/add information.

 

What is specific about the row you want? A question number? An id number? A keyword that is part of the data?

 

You query for specific data, not a specific row.

Link to comment
Share on other sites

Have you given each entry in your table a unique key such as an ID?

Just each row has an id.

 

"SELECT * FROM QANDATable WHERE id = 2";

 

I did this:

$result2 = mysql_query("SELECT * FROM QANDATable WHERE id = 2");
$row2 = mysql_fetch_array($result2);
echo $row2['TQuestion'];

 

This works. But is there really no faster way then having to declare 2 new variables each time I want another element from a different row and column?

 

Link to comment
Share on other sites

How do you know you want row 2 from your table, given that what is stored in second row in your table can be anything because the database engine will reuse positions in your table as you delete/add information.

The point of this table is just to recall info. Not to store answers. I will do that in a different table.

So the elements will always remain fixed.

 

What is specific about the row you want? A question number? An id number? A keyword that is part of the data?

The id number

Link to comment
Share on other sites

The goal you are trying to achieve is not clear from your statements.

 

A) If you want to loop through all the rows that your query matched, you use a loop to do so.

 

B) If you want to access any row that your query matched, you can use mysql_result.

 

C) If you want to execute a query that gets a specific row or rows from your table you write and execute a query that has a WHERE clause that matches the row(s) that you want to retrieve.

 

In general, you write one query that gets the row(s) you want in the order that you want them and you simply iterate over the row(s) in your php code to use them or display them the way you want.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.