Jump to content

Recommended Posts

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?

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.
?>

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.

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.

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];

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'];
			}
		?>

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); 
			}
		?>

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.

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.

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.