Bentley4 Posted May 21, 2011 Share Posted May 21, 2011 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/ Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2011 Share Posted May 21, 2011 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218433 Share on other sites More sharing options...
otuatail Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218434 Share on other sites More sharing options...
Bentley4 Posted May 21, 2011 Author Share Posted May 21, 2011 If you aren't able to simply add a parameter to the query string that would select the proper record, ... Where exactly do I add the parameter in the query string for this code? The text I want is in row two. Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218438 Share on other sites More sharing options...
mkmDesign Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218440 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218441 Share on other sites More sharing options...
Bentley4 Posted May 22, 2011 Author Share Posted May 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218643 Share on other sites More sharing options...
Bentley4 Posted May 22, 2011 Author Share Posted May 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218644 Share on other sites More sharing options...
PFMaBiSmAd Posted May 22, 2011 Share Posted May 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237050-echo-specific-row-from-mysql-table/#findComment-1218659 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.