mostafatalebi Posted February 14, 2013 Share Posted February 14, 2013 Hello I need to get all of my result in an array, I mean each as an array's element. So if I say $result[63] it right goes to 63th row and fetch the result. I'm hostile to the column id, but if there don't be any other solution, I should go to it. Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/ Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 Uhm. You are "hostile" to using the functionality designed to make life easier on you because....? Are you saying you want all the results in an array, and then access the array key? Or you want to give a function the ID and get that row? Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412434 Share on other sites More sharing options...
mostafatalebi Posted February 14, 2013 Author Share Posted February 14, 2013 With Hostile I mean the id column of sql table do not update accordingly. OK if the id is the only solution I employ it. My table has many many rows, and I need only the last six rows. I want to get all the rows and then by using array keys fetch them: $result[$size-1]; $result[$size-2] and ...; If I could store only last six rows directly from the SQL statement, then life would be much easier. Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412436 Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 SELECT cols FROM tbl ORDER BY id_col DESC LIMIT 6 Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412437 Share on other sites More sharing options...
mostafatalebi Posted February 14, 2013 Author Share Posted February 14, 2013 Thank you Jessica. Again you helped me Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412438 Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 See, it's a lot easier when you actually ask the question. Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412439 Share on other sites More sharing options...
mostafatalebi Posted February 14, 2013 Author Share Posted February 14, 2013 Sorry I again encountered a problem The below part of script doesn't work with "prepare", while it easily runs with "query"; DEFINE("SELECT", "SELECT english, persian FROM "); $statement = SELECT . $table . " ORDER BY id DESC LIMIT 6"; if($data = $database->query($statement)) { $b = $data->fetch_array(MYSQLI_NUM); echo $b[0] . " " . $b[1]; $data->close(); } Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412448 Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 Why are you creating your query using a constant, a variable table name, and a regular string? Your code doesn't have anything about prepare() in it. Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412454 Share on other sites More sharing options...
mostafatalebi Posted February 14, 2013 Author Share Posted February 14, 2013 It is due to security reason. I often use it. recently I read a book at which this method was also used. When I use prepare in place of query (and of course some more change such as execute() added) it errs that the method mysqli_stmt::fetch_array is undefined. Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412459 Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 POST THE CODE for crying out loud. http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php http://www.php.net/manual/en/class.mysqli-stmt.php There is no fetch_array on a prepared statement. The error message is pretty clear. Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412462 Share on other sites More sharing options...
Zane Posted February 14, 2013 Share Posted February 14, 2013 I am taking a shot in the dark here, but maybe this is what you are wanting? $data = array(); while($r = mysql_fetch_array($query)) { $k = $r['id']; $data[$k] = $r; } Quote Link to comment https://forums.phpfreaks.com/topic/274485-getting-results-in-array/#findComment-1412492 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.