Jump to content

how to get single vars from a mysql_fetch_array()


son.of.the.morning

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.