son.of.the.morning Posted December 14, 2011 Share Posted December 14, 2011 I want to use all five results separately in order to use in a Jquery slider. <?php $a = "SELECT * FROM blog_posts ORDER BY id DESC LIMIT 5"; $query = mysql_query($a) or die (mysql_error()); $query_results = mysql_fetch_array($query); ?> I know if i was going to grab vars from an normal array i would just use... <?php echo $array_reults['0'] ?> Can any one help me with this problem? Quote Link to comment Share on other sites More sharing options...
kney Posted December 14, 2011 Share Posted December 14, 2011 <?php $a = "SELECT * FROM blog_posts ORDER BY id DESC LIMIT 5"; $query = mysql_query($a) or die (mysql_error()); $query_results = mysql_fetch_array($query); // should work echo $query_results[0]; ?> Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 14, 2011 Share Posted December 14, 2011 If you'd rather access your row data by column name, use either mysql_fetch_assoc, or use the MYSQL_ASSOC flag in mysql_fetch_array. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 14, 2011 Author Share Posted December 14, 2011 well there is more then one column in the table so i need to select one record and one column at a time. <?php echo $query_results['title']['0']'; // i need to get the a single record from an single column. ?> Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 14, 2011 Share Posted December 14, 2011 You mean something like this: $query = "SELECT * FROM blog_posts ORDER BY id DESC LIMIT 5"; $results = mysql_query($query); while($row = mysql_fetch_assoc($results)) { echo $row['title']; } ? I ask because that's pretty much the universal pattern for retrieving and doing something with multiple rows of data. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 14, 2011 Author Share Posted December 14, 2011 I dont want to loop data i wana be able to just use each record and column indervidualy Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 14, 2011 Share Posted December 14, 2011 You can't access sequential records without a loop, AFAIK. The fetch functions only return one row at a time. Calling a fetch function repeatedly moves its internal pointer to the next row, which is why it's used in a loop. For what you want to do, using a loop is the best way to go about it. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 14, 2011 Share Posted December 14, 2011 I know your religion prevents you from posting fully-formed questions, so here's a random code snippet that may or may not be what you're looking for: $query = "SELECT * FROM blog_posts ORDER BY id DESC LIMIT 5"; $results = mysql_query($query); $rows = array(); while($row = mysql_fetch_assoc($results)) { $rows[] = $row; } echo $rows[2][3]; Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 14, 2011 Author Share Posted December 14, 2011 I tryied what you said but didnt have any luck. I thought i could use this method, but this also does fuck all! <?php $a = "SELECT * FROM blog_posts ORDER BY id DESC LIMIT 2"; $query = mysql_query($a) or die (mysql_error()); //$query_results = mysql_fetch_array($query); while ($row = mysql_fetch_assoc($query)){ $title = $row['title']; $post = $row['post']; $img = $row['img']; } ?> Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 14, 2011 Share Posted December 14, 2011 Do what I said and then print_r($rows); You'll see the structure of the array and be able to use it then. If you still can't use it, copy and paste the results of print_r($rows) here. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 14, 2011 Author Share Posted December 14, 2011 it just returns the results one result from the query like so... Array ( [id] => 272 => aston_martin21.jpg [title] => TEST POST! => Grand avant creve lilas il du se monde. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 14, 2011 Share Posted December 14, 2011 That's impossible if you copied and pasted my code. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 14, 2011 Author Share Posted December 14, 2011 thats what i used. <?php $a = "SELECT * FROM blog_posts ORDER BY id DESC"; $query = mysql_query($a) or die (mysql_error()); while ($rows = mysql_fetch_assoc($query)){ print_r($rows); } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 14, 2011 Share Posted December 14, 2011 Only record in table? Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 14, 2011 Share Posted December 14, 2011 Copy. Paste. Seriously, what you have is nowhere near what I put. Do you not realize that $row and $rows are separate variables? One is singular, because it's one row. The other is plural, for all rows. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 14, 2011 Author Share Posted December 14, 2011 no, there are numerious records. I maybe a retard when it comes to PHP but am not that stupid lol Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted December 14, 2011 Share Posted December 14, 2011 You're not reading what I wrote. You are looking at my code, which contains TWO VARIABLES: $row AND ANOTHER ONE CALLED $rows. See how they have two different names? You cannot print $row when I ask you to print $rows. See? Two different variables? You want to use multiple rows, right? Use $rows for that, not $row, because clearly $row is one row and $rows is ROWS. Go back. Copy my code. Paste my code. DO NOT CHANGE MY CODE. At the END of my code, AFTER my code, print_r($rows); Then, using that output, figure out what you need to do to get the data you want. You can alter the echo in my code to echo whatever you decide you need to echo. Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 14, 2011 Share Posted December 14, 2011 are we going to try and market this thread to SNL or Conan? Quote Link to comment 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.