473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to select more than two variables in a mysql_fetch_object while loop

Hello,

I am trying to select three variables from my mySQL table. I have am creating a function so that I can display the three values later in a foreach loop. I am trying to pull the variables url, name, and s_name.

I can't seem to get all three variables in one array. Could you please help me figure out how to improve the function and then show me how to display it.

Here is the function:
Expand|Select|Wrap|Line Numbers
  1. function getURL(){
  2.   $sql = 'SELECT * FROM searches LIMIT 10';
  3.   $result = mysql_query( $sql );
  4.   $searches = array();
  5.   while( $obj = mysql_fetch_object( $result ) ){
  6.          $searches[ $obj->url ] = $obj->name, $obj->s_name;
  7.          }
  8.          return $searches;
  9.      }
  10.  
And here is how I plan on displaying it:
Expand|Select|Wrap|Line Numbers
  1. <?php foreach( $searches as $url => $name, $s_name ) { ?>
  2.    <a href="<?= $url; ?>"><?= $name; ?>, <?= $s_name ?></a>
  3. <?php } ?>
  4.  
Any assistance would be greatly appreciated.
Sep 22 '10 #1

✓ answered by BaseballGraphs

This has been resolved:
Expand|Select|Wrap|Line Numbers
  1.  $sql = 'SELECT * FROM searches ORDER BY id DESC LIMIT 5';
  2.   $result = mysql_query( $sql );
  3.   while( $obj = mysql_fetch_object( $result ) )  {
  4.   $data = array();
  5.   $data['url'] = $obj->url;
  6.   $data['name'] = $obj->name;
  7.   $data['s_name'] = $obj->s_name;
  8.   $data['id'] = $obj->id;
  9.   $data['team'] = $obj->team;
  10.   $searches[] = $data;
  11.   ksort($searches);
  12.   }
  13.  

5 2589
johny10151981
1,059 1GB
try this
Expand|Select|Wrap|Line Numbers
  1.   $i=0;
  2.   while( $obj = mysql_fetch_object( $result ) )
  3.   {
  4.    $searches[$i]['url'] = $obj->url;
  5.    $searches[$i]['name'] = $obj->name;
  6.    $searches[$i]['s_name'] = $obj->s_name;
  7.   }
  8.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php foreach( $searches as $url) { ?>
  3.    <a href="<?= $url['url']; ?>"><?= $url['name']; ?>, <?= $url['s_name'] ?></a>
  4. <?php } ?>
  5.  
by the way the produced URL will be erroneous. there is space in your url and comma
Sep 22 '10 #2
Thanks very much for the reply. It seems to be working, however, it doesn't appear to be returning the last 10 values in my table, it is only returning the most recent value.

How can I get the output to display the last 10 values in my table?

Here is the code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. $sql = 'SELECT * FROM searches LIMIT 10';
  3.   $result = mysql_query( $sql );
  4.   $searches = array();
  5.   $i=0;
  6.   while( $obj = mysql_fetch_object( $result ) )  {
  7.   $searches[$i]['url'] = $obj->url;
  8.   $searches[$i]['name'] = $obj->name;
  9.   $searches[$i]['s_name'] = $obj->s_name; }
  10.  
Expand|Select|Wrap|Line Numbers
  1. <?php foreach( $searches as $url) { ?>
  2.   <a href="<?= $url['url']; ?>"><?= $url['name']; ?>, <?= $url['s_name'] ?></a>
  3. <?php } ?>
  4.  
Sep 22 '10 #3
johny10151981
1,059 1GB
you have limit your search to 10. It mean it will display only top 10 query output. remove the limit 10.
Sep 22 '10 #4
It is still only outputting the last value from my table. I want it to output the last 10 values in my table. How do I alter the code to do that?

Expand|Select|Wrap|Line Numbers
  1. $sql = 'SELECT * FROM searches';
  2.   $result = mysql_query( $sql );
  3.   $i=0;
  4.   while( $obj = mysql_fetch_object( $result ) )  {
  5.   $searches[$i]['url'] = $obj->url;
  6.   $searches[$i]['name'] = $obj->name;
  7.   $searches[$i]['s_name'] = $obj->s_name; }
  8.  
Expand|Select|Wrap|Line Numbers
  1. <?php foreach( $searches as $url) { ?>
  2.  <a href="<?= $url['url']; ?>"><?= $url['name']; ?>, <?= $url['s_name'] ?></a>
  3. <?php } ?>
  4.  
Sep 22 '10 #5
This has been resolved:
Expand|Select|Wrap|Line Numbers
  1.  $sql = 'SELECT * FROM searches ORDER BY id DESC LIMIT 5';
  2.   $result = mysql_query( $sql );
  3.   while( $obj = mysql_fetch_object( $result ) )  {
  4.   $data = array();
  5.   $data['url'] = $obj->url;
  6.   $data['name'] = $obj->name;
  7.   $data['s_name'] = $obj->s_name;
  8.   $data['id'] = $obj->id;
  9.   $data['team'] = $obj->team;
  10.   $searches[] = $data;
  11.   ksort($searches);
  12.   }
  13.  
Sep 23 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: z0ink | last post by:
I am working on a small graphing application. In the process of graphing I use 3 seperate scripts for getting the job done. The first is the page that the use sees and selects all the data from. ...
3
by: Craig Keightley | last post by:
I have the following form <FORM NAME="form1" METHOD="POST"> <?php do { ?> <input name="approve" type="checkbox" id="approve" value="<?php echo $row_rs; ?>"> <select name="select"> <option...
0
by: petrovitch | last post by:
While using the following loop to extract images from the google search engine I discovered that preg_match_all works much faster parsing small strings in a loop than extracting all of the urls at...
1
by: Gordon Williams | last post by:
Hi, I have been using this code for a number of weeks without problems. Now I am getting an exception after the program has run a short while. It has already passed through this function...
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
2
by: Chris Zopers | last post by:
Hello, I've created a stored procedure that loops through a cursor, with the following example code: DECLARE curPeriod CURSOR LOCAL for SELECT * FROM tblPeriods DECLARE @intYear smallint...
6
by: dpswissboy | last post by:
I have a form with 30 input boxes named box1, box2, box3, etc. Instead of: var1 = Request.Form("box1") var2 = Request.Form("box2") etc. ..I am trying to collect the values in a loop statement...
4
by: ataurrehman | last post by:
I want to use select case in a loop as under: Dim i As Integer i = 1 While i < 6 Select Case Field1 Case "Title" strFld(i) = "( & ': ' & )" Case "Author" strFld(i) = "( & ', ' &...
10
by: Raven7738 | last post by:
Hey, I'm using Access 2007. I have a code to open a new message in outlook 2007. Private Sub btnEmail_Click() Dim ol As Outlook.Application Dim inbox As Folder Dim msg As MailItem
3
doma23
by: doma23 | last post by:
I have this code: For COUNT = 1 To 5 EmptyFields_Period = True For Each ctl In Form_frmMain.Controls If ctl.ControlType = acTextBox Then If ctl.Tag = "Period" &...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.