Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    PHP


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





If Result Of Mysql Query Is Only One Row/column, Why Use An Array?


If I'm doing a very specific select statement, which I know will only ever return one value, can I get that resulting bit of data without storing it in an array or object?
If not, is mysql_fetch_array() the fastest method for getting this one value?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Can I Put A Mysql Query Result Directly Into A Multidimensional Array?
I have two joined tables:

Departments - which contains the department description and a key
Positions - which contains position data

What I'm attempting to achieve is to display the department title in one table cell and then list all the positions associated with that department in the cell under neither the title. I need to generate a table two column table with as many rows as required, depending on the number of departments, which is dynamic.

Do this I'm trying to turn the result of my SQL query into multidimensional array e.g. dept = VioP positions = "designer", "engineer" etc, which I can do then use to generate the table.

I've done a search on the forum and looked around the manual and I'm stumped on how to achieve this, a point in the right direction would be greatly appreciated. The query I'm using is below. PHP Code:

View Replies !
Query Result To Array
i have a table with two field ID and CAPACITY. i want the ID to point to the capacity on an array

$top = "SELECT * from topics";
$db = new mysqli('localhost','root',?');
$db -> select_db('testing');

$res_top = $db->query($top);
$topics = array();
while ($row = $res_top->fetch_row())
{
$topics[$row['id']] = $row['capacity'];
}

View Replies !
Moving A Query Result Into An Array
I have read all the array stuff done searches for examples, but am still not clear if there is a way to do a query from a MySQL table and move those vaules into an arry.

I am sure it can be done, but I thought I would find a function that would do it. Am I just overlooking something?

$resultID = mysql_query("SELECT DISTINCT slot FROM items");
while(list ($slot) = mysql_fetch_row($resultID)){echo"$slot";}

So anyone have a good way to move the result into some array?

View Replies !
Query Giving Array As Result
I just started PHP and getting the following error when trying to read and echo something from my DB. I get Array as an result of a working SQL query (I tested it in PHPMyAdmin SQL Query) and it should be something else. Array doesnt return in the database i selected, but still it's here.

<?
$host="localhost";
$user="xxxxxxxxx";
$password="xxxxxxxxxxx";
$dbname="mohaaleague";
$connection = mysql_connect($host, $user, $password) or die ("Kan niet verbinden");
$clan = "Belgian Fun Clan";
$db = mysql_select_db ($dbname,$connection) or die("Je Stinkt");
//SELECT clan FROM clan WHERE clan='Belgian Fun Clan'
$query = "SELECT clan FROM clan" or die("Query Mislukt");
$result = mysql_query($query) or die("Query Mislukt1");
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die("Query Mislukt2");
extract($row);

echo($row. "<br>");
if ( $clan == $row )
{
echo "het werkt";
}
else
{
echo "het werkt niet";
}
$disconnect = "mysql_close";
?>

View Replies !
Alter Column Name In Query (mysql)
is there any way to alter the column name in the query when fetching an array (mysql)

View Replies !
MySQL Query Returning Empty Column
Here's my code:

$query_get_machine = sprintf("SELECT machines.ID, machines.NotifyID, machines.Name, notify.ID, notify.`Data` FROM machines RIGHT JOIN notify ON machines.notifyID = notify.ID WHERE machines.ID = %s", GetSQLValueString($colname_get_machine, "int"));

Can anyone tell why the notify.`Data` column might be returned empty? All others display as they should.

View Replies !
Mysql Query Result
I have successfully selected 4 rows using a mysql SELECT query. However, I would like to random pick a row from this result so that every page refresh will yield something different.

View Replies !
Same Mysql Query Doesn't Always Return A Result
We have no access to a mysql NG on my provider's server, so we ask here:

We have a long query (long in text) with a UNION between 2 select.

We have been informed that some times the query doesn't return any result.
We have tried on our server and we always get a result. BUT, trying on the
hosting server, many times the query doesn't return any result and doesn't
get any error.

Any idea ? does Union have any problem ? how to check if the query failed ?
My hoster said that sometimes the table can be locked (if the server is
overloaded) and then mysql doesn't return any result, but this seems to me
an aberration.

the query takes about 0.0050 sec to execute when it doesn't return any
result, and 0.030 when I get results

View Replies !
MySQL Query Result Rows
Still learning the ropes of PHP/MySQL here so pardon if this is something from a 101 class. I am wanting to know if/how I can query a MySQL database, pull two rows of information and assign them individual variable names. I know this isn't too clear. Let me try to explain what I want to do. Let's say I have something similar to this:
Code:

$query = mysql_fetch_assoc(mysql_query("SELECT entry_id, user_id, date FROM my_entries WHERE user_id = '$user_info[user_id]' ORDER BY date DESC LIMIT 2"));


What I would like to be able to do is assign separate variables for the info on the two rows returned by the query. I want to have the following variables:

$first_date = $query[date]; <= from the first row
$second_date = $query[date]; <= from the second row

I can then use the two variables to compare to each other, etc. Can this be done with a single query? Or are two queries required (if so how)?

View Replies !
Processing MySQL Query Result
after executing the query, say it returns 1000 results. if i just want to do some sampling and like to have a look on 4 of them, how can i refer to these 4 random records out of 1000? i'm working on processing the result of $res_array which has 1000 records.

query = select * from test;

result = mysql_query(query);

records1000 = db_result_to_array($result);

//how to select/remove records1000 to get say, records4?


function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = $result->fetch_assoc(); $count++) {
$res_array[$count] = $row;
}
return $res_array;
}

View Replies !
MySQL Query - INSERT - Unknown Column 'test' In 'where Clause'
I have a registration script on my website, and it was working fine, I added a second MySQL INSERT query to it, for another table so I can use JOIN's later on with the second table, but I keep getting the same error:

Unknown column 'test' in 'where clause' can someone see whats wrong with this? Code:

View Replies !
How Can I Populate A Listbox With The Result Of Mysql Query?
I would like to populate a listbox with the result of a mySQL Query.

View Replies !
How To Get Mysql Query Result Into Temp. Txt File
I did a lite search in the archive but didn't find anything specific to my problem. Here is what I am trying to do:

I've got a table with about 25k entries of company addresses. I need to be able to pull specific data out of the table and put it in a temporary text file and then display the result in the browser for the user to save locally.

The query part and the format of the data is all simple enough but I am having trouble figuring out how to get the data from the query into a temporary text file and then displaying that file. here is what I whipped up but it obviously doesn't work: Code:
<?
$tmpfname = tempnam("/path/to/file/", "FOO");
$list = mysql_query("select * from FH_LIST where State='AK'");
while ($list_results = mysql_fetch_array($list)) {
$data = blah blah blah;
fwrite($tmpfname, ".$data
");
}

fopen($tmpfname, "r");
fpassthru($tmpfname);
?>

I get an error message saying:
Warning: Supplied argument is not a valid File-Handle resource (referring to $data).

View Replies !
MySQL Result Is Empty, But The Query Is Correct
I have written a script that grabs info from a database, and outputs it to a form. I figured I'd tidy things up a bit, and use a function to handle the queries, where I had previously hardcoded almost the same query several times. Code:

View Replies !
Selecting A Random Row Of A Mysql Query Result?
Let say I have a mysql query, which might yield more than 1 row when queried. How can I set up the query, so that it RANDOMLY selects one of the results? I know that I can do a LIMIT 1 command, but this will always yield the first row.

View Replies !
Grouping Mysql Query Result By Dates
I have a mysql query result that displays a set of news heading thats are sorted by the date that it was created. I was wondering how to group them so it produces the following layout. Code:

View Replies !
Loading A MySQL Column Into An Array
ok i have a forum i am creating and i store the id of the forums where people are moderators in their userinfo. i speperate them with a comma. i need to either load them into an array or use explode() but i'm not sure how to create the array. i'm thinking that using an array would be faster because i could search in it to find out if the user is a mod.

View Replies !
MySQL, Turn Column Into Array
Today is the first time I've used MySQL with PHP.

What is the best way to turn a column into an array?

This is how I am doing it at the moment, surely this is sub-optimal:

$query = 'SELECT something FROM table'
$result = mysql_query( $query );
$i = 0;
while( $arrayTemp = mysql_fetch_array( $result ) )
{
$array[ $i++ ] = $arrayTemp[ 0 ];
}
echo "<ul>";
foreach( $array as $string )
{
echo "<li>$string</li>";
}
echo "</ul>";

View Replies !
Send Mysql Query Result Displayed In A Table
I have just created a shopping cart and I would like to send my user the receipt of there order by email. The receipt is generated by a mysql query and is displayed within a table. I have worked out how to send a html using simple HTML code as my “$messageâ€&#65533; variable: $success = mail($to, $subject, $message, $headers);

But I can not get it to send my mysql query result displayed in a table. How can i create the result of my mysql query and make it a simple variable i.e $message.

View Replies !
Select Single Column From Mysql Into Array
Is there a way to select a single column from a mysql database and
directly put the results into an arrray? Here is the workaround below,
but I would like to elimniate the array_push step:

$uids=array();
$res = mysql_db_query("db", 'select uid from tbl;', $link);
while ($row = mysql_fetch_row($res)) {
array_push ($uids, $row[0]);

}

View Replies !
Mysql Result Into Array?
I have a shopping cart script sending info to a processor. I need to send the qty's and item name's in some sort of string via a single variable to the process form.
i.e. (3) Hipster Turnips, (6) Butter Milk Baby Brains, (2) Super Freaks Code:

View Replies !
Mysql Result Set As An Array
the question is, is there a way of assigning each value returned from the sql query and assinging them to a single array. for example i have a a query like so;

$table = "sales_table";
        
$query = "select treatments from $table where
date between  &#3921;/08/2006' and &#3925;/08/2006'";
$result = mysql_query($query);

because the values stored in the treatments field are imploded as a single string when the user submits the form the value looks like; 1, 22, 33. Code:

View Replies !
MySQL Result As Array
$db= new MySQL($host,$dbUser,$dbPass,$dbName);
$sql=("SELECT * FROM sticker_files WHERE sticker_cat_id=&#398;'");
$catdirs = $db->query($sql);
$dirs=$catdirs->fetch()

and I need a for each loop (i think) to insert that data below:

$params = array(
'mode' => 'Jumping',
'perPage' => 3,
'delta' => 2,
'itemData' => array(****RESULTS HERE AS COMMA SEPARATED string)
);

View Replies !
Mysql Result To Array
I have this query, it gives the suspected information in myphpadmin:

select listid, count(*) from table1 group by listid

How do I put this the result into an array in php? I have problems with the
count(*) field

View Replies !
Array With MySQL Result
//MAIN FLOW
$counter = 0;
$sql[$counter] = "SELECT * FROM category WHERE parent = 0 ORDER BY parent";
$result[$counter] = mysql_query($sql) or die(display_and_log_error("SQL ERROR: ".__LINE__));
while($myrow[$counter] = mysql_fetch_assoc($result[$counter])){
echo $myrow[$counter][category_name];
...

View Replies !
Replacing Array With Mysql Result
I wanted to repalce the following line of code $data = array(40,21,17,14,23); with

for($i=0;$i<$numrows;$i++)
{
//print(mysql_result($result,$i,2));print("<br>");
array_push($data,mysql_result($result,$i,2));}

where mysql_result($result,$i,2) is the value and when i print it displays the values. But $data array is the Y axis value for drawing a chart. Here I wanted to replace the hard coded value with values from mysql but the second code does not function. Does anybody have idea how can I replace the $data array or what may be the problem with my coding The first one works but the second one does not work but in both cases it does not display any error.

View Replies !
Sort A Mysql Result According To A Php Array
Hi everybody !
I have a question for the gurus ;+)
I have to arrays :

PHP Code:




$id = array(45,9,21);
$pos = array(3,1,2);






I select in my mysql table using the ids :

PHP Code:




$sql = "SELECT * FROM MyTable WHERE id IN(".implode("",$id).")";






and I now want to sort the result according to the $pos array.
Is there a way to do this directly in mysql ?
If not, what would be the smartest way to do it in PHP ?
Because I would prefer NOT to do the following :

PHP Code:




while ($t = mysql_fetch_array($ret)) {
    $a_field1[] = $t["field1"];
    $a_field2[] = $t["field2"];
    $a_field3[] = $t["field3"];
    $a_field4[] = $t["field4"];
...
}
array_multisort($pos,$a_field1,$a_field2,$a_field3,$a_field4)






I fear it could be a very time consuming process when there is a lot of results and a lot of fields. But may be the no other solution...

Thanks

View Replies !
Column Data-Type For Storing Serialized Array In MySQL?
I'm really not sure - I used BLOB in the past- but the arrays are only a few kilobytes. But VARHCAR only allows 255 characters at most and the arrays might contain many more characters than that.

View Replies !
MySQL Result To Real Array In Function
I'd like to create a function which input is the result of a mySQL
query.
The output should be exactly the same, only not a mySQL result array,
but a 'real' array.
So it should also get the fieldnames returned by mySQL and use those as
keys.

I can't get things to work properly: it should return a
multidimensional array,
like

$result_array[1] = array(
[field1] => field1 value,
[field2] => field2 value,
etc.
)

somehow my result is (with code below)

$result_array[1] = array(
[0] => field1 value,
[field1] => field1 value,
[1] => field2 value,
[field2] => field2 value,
etc.
)

+++++ code ++++++

$get_res= mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[] = $res;

}while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};

View Replies !
Changing Array Format From Result Of MySQL
here is what MySQL returns in a visual way..

game_id | team_id | stat_id
1 | 1 | 1
1 | 2 | 3
1 | 3 | 7
1 | 3 | 6
2 | 1 | 1
2 | 4 | 3

in records with the same game_id, there will be no repeating team_id
in records with the same team_id, there will be no repeating stat_id

so, after I got the query, I made them into this format

PHP Code:

View Replies !
Making Mysql Result Into Multi-dimensional Array
My php problem is I want to make the result of 3 tables linked through foreign keys into a 3-dimensional array that reflects their relationship without repetition and without having to call the database more three times.

I know I'm doing a couple of things wrong, one thing for instance is initializing a variable at the end of the loop to remember the previous value s that it does not repeat itself, surely there must be a more elegant way to go about this, but I'm lost and I've been stuck on this all day. Code:

View Replies !
How To Query From One Column Then Sort Using Another Column
I am trying to get the last 100 albums I entered into my database using the album_id (Highest being the latest entry) then sort them alphabetically using the album_title column.

I can get the latest 100 easily but cannot figure out how to also sort the results by the album_title before I display them.

This is what I have so far which gets what I want but not in alphabetical order.

<?php
$sqlquery = mysql_query("SELECT * FROM albums ORDER BY album_id desc limit 100", $db);

while ($result = mysql_fetch_row($sqlquery))
{
echo "<a href='music_info.php?album_id=$result[0]'><span style='text-decoration: none'>$result[1]</span><br></a>";
}
?>

I have been trying to add a second ORDER BY to the query like so.

$sqlquery = mysql_query("SELECT * FROM albums ORDER BY album_id desc limit 100 ORDER BY album_title", $db);

but this obviously isn't the way I should be doing it.

View Replies !
Retrive A Result Set With An Autonumbering Column
I have been trying to retrive a result set with an autonumbering column so that the results can be listed with 1, 2, 3 etc. Tho code i am using is this:

SELECT @id:=@id+1, column_name FROM table_name;

Instead of an autonumbered row it just returns NULL in the MYSQL/PHPMYADMIN where i tested the querie. I have searched on the net and on this site for a solution but with no result. -just to clarify it is not the primary key wich is also autonumbered/incremented i am refering to.

View Replies !
Order The Sql Result By Largest String In A Column?
I suppose this might be mainly an sql question but, is there a way to order the sql result by largest string in a column? You could use an array to sort the result but i was just wondering if there was a direct way using the sql statement.

View Replies !
Turn Select Query Result Into Hyperlink To Other Query
I have a query which gives results of selecting coursenames from a table called trainingtopics but this does so in a continuous bulk of text without any breaks between each record. my question how do I create line breaks between each record and also how can I force each query result to become a hyperlink which when clicked runs another query that gives details of that course.

<?php
$user = "root";
$host = "localhost";
$password = "";
$connection = mysql_connect($host, $root, $password) or die ("Couldn't connect to server.");
$database = "courses";
$db = mysql_select_db($database) or die ("Couldn't select database.");

$sql = "SELECT coursename FROM trainingtopics";
echo $sql."=sql<br>";

$result = mysql_query($sql) or die(mysql_error());
echo $result."=result<br>";

while($row=mysql_fetch_array($result))
{ // NOTE this one ABOVE the echo
echo "result found!";
echo $row[0];
}
?>

View Replies !
MySql Query Into Array
A bit of a newb question here; I'm building a little application using PHP and MySql and have a question on how best to perform a specific function...

I need to select the last 20 records form a table, and with those 20 records I need to take the 10 with the lowest value and add them together to come out wiht a single number. My table only has 2 columns: ID and Number.

I can easily get the 20 lowest using:

$query_rsLimited = "SELECT * FROM test ORDER BY testID DESC LIMIT 20";
$rsLimited = mysql_query($query_rsLimited, $ConnClub) or die(mysql_error());
$row_rsLimited = mysql_fetch_assoc($rsLimited);
$totalRows_rsLimited = mysql_num_rows($rsLimited);

My question is how best to get the 10 highest 'Number' values out of the 20 records and sum them.

View Replies !
Array In Mysql Query
I have file extension loaded in a DB and if they are active i call them in a mysql_query. the way i will call all active files is PHP Code:

in_array($getf,$image_ext);

PHP Code:

$query = "select * from ".$prefix."search WHERE (type = 'jpg' OR type = 'png' OR type = 'gif' OR type = 'jpe' OR type = 'jpeg') AND (file_name LIKE '%$res%' OR name LIKE '%$res%') ORDER BY name ASC";

View Replies !
Speeding Up Query/code (query Within Result Set)
I'm looking for ideas on how to speed up this script. Basically it finds all the zip codes in a zipcode table, then looks for all the records in another table with those zip codes.

Right now it finds all the zips then within that WHILE, it looks for a record in another table with that zip: PHP Code:

View Replies !
MySQL Results Array Not Same As Query In DB
I am running a query in a MySQL database, and the results I am getting in my array do not match the results from running the exact same SQL in MySQL. Code:

View Replies !
Create A Mysql Query Out Of An Array.
I am trying to create a Mysql query out of an array. Here is my code.

$lookup = mens jackets;
$lookup = explode(' ',$lookup);
$count=count($lookup);

for($i=1; $count >= $i; $i++){
$query =("select * from stuff WHERE description LIKE ('$lookup[0]')
$query .= AND ('$lookup[$i]')
$query .= ");}

here are my results I am getting; Code:

View Replies !
Mysql Query Built From Array
i want to query a database for a number of ids (pids) with a query partly built from an array. i get the pids through: Code:

View Replies !
Use Query Result Field As Query Key For New Query..
$query  = "SELECT ProjID, UserID, ProjDesc, file, OrigProj, OrigUser, ProjDate FROM projects";
$result = mysql_query($query);

I would like to take the field 'UserID', and utilise it for a Query statement to my users table in my database, to read and fill in information to my table, which looks like this:
PHP Code:

View Replies !
Making A Multidimentional Array From A Mysql Query
I'm very new to php and coding generally so I'm sorry for any frustrating ignorance I display. I have been looking about this forum and search a couple of times but couldn't find what I was looking for, I'm thinking it's a common situation though so no doubt someone will be able to point me to where I should be looking. Anyway, my problem:

I have a simple mysql table of data of the form:

| ID | name | value |
--------------------
| 21 | Dave | 8767 |
| 56 | Fred | 34565 |
| 71 | Pete | 92104 |

I'm looking to put it into a multidimensional array something like this: Code:

View Replies !
MySQL Query Into Multi-dimensional Array
Basicly, What im trying to do is have it so I have a multi-dimensional array so like the first "Dimension" is the row and the second "Dimension" is the collumn. Code:

View Replies !
Php Generated Form Array And Formatting For Mysql Query
As part of a form I have created I have a MySQL generated table who's fields include soccer team names, opponents, next match date, matchid etc. . Each row has a check box with the value of the checkbox being the unique row ID in the MySQL table (match id). When a user selects the teams from the list he would like more information about, the checkbox variable is parsed to the script as an array "$var[]".

If I use the following code, I can see all the contents of the $Var array that has been parsed to the script.

[php
<?
$number=count($var);
for($a=0;$a<=$number;$a++){
echo $var[$a];
}
?> /php]

The problem is how do I format the above output so that I can include it in a MySQL query statement? I have no way of knowing how many teams have been selected or the key index number for each variable in the array. How do I code this so that each row matchid is assigned it's own unique varable such as team 1, team2 etc?

I was thinking of using a loop to go through the array and generate $var appended with a number using the the PHP count() function. but seeing that I am fairly new to PHP I'm not sure if this will work or where to start!

View Replies !
Variable To Search An Array Created By MySQL Query
I'm trying to use a variable to search an array created by MySQL query. Let's say I have this query:

PHP Code:

mysql_select_db($database_myconn, $myconn);
$query_rs_cars = "SELECT car_id, car_make FROM cars";
$rs_cars = mysql_query($query_rs_cars, $myconn) or die(mysql_error());
$row_rs_cars = mysql_fetch_assoc($rs_cars);
$totalRows_rs_cars = mysql_num_rows($rs_cars);

View Replies !
Using Query Result To Do Another Query In Loop
I have 2 tables, one called users2 and one called books. Books has a field called UserId, which is the ID of the user that added the book to the database.

My problem, is that i need to take this ID from the book table, and use it to get some information from the user table. Here is my code to get the ID from the book table: Code:

View Replies !
How To Query One Column?
this is php mysql query script from the patients table register, i want when i enter one value for example for sex m (male),the script will show only male patients all rows,but this script show all the registered parients in any query Code:

View Replies !
Inserting Data From Array Into MySQL Query Generated By While Loop
Without a doubt this issue is dead simple so please excuse the ignorance. Also not sure what I should search for to find a solution, which must already exist on the forum.

Here is my code without any repetition and it works fine. The id is retrieved from the database and is successfully passed to the query, which I have verified updates the categoryID for the correct row. Code:

View Replies !
My Query Of A Column Only Returns One Value
I have no idea why this query doesn't work. It only returns one value from the column "groupname," but it should return every value. It does not die and return any of the errors though.

I'm pretty sure the SQL is fine, so I think it must be the php somewhere, but I have been working on this for a while, so I have checked it quite a few number of times already. Code:

View Replies !
Array Explode Problem Result In A 6 Item Array
I have the following:

$BB_Basket = "basket=¦APL6007,2.00¦AVX8900,1.00¦ARCAV500,1.00";
$basket_containers = explode('¦', $BB_Basket);
unset($basket_containers[0]);
print_r($basket_containers);

This results in

Array ( [1] => APL6007,2.00 [2] => AVX8900,1.00 [3] => ARCAV500,1.00 )

Now when I try to explode further with

$basket_items = explode(',', $basket_containers);
print_r($basket_items);

This results in

Array ( [0] => Array )

This should result in a 6 item array. Any ideas what I'm doing wrong?

View Replies !

Copyright © 2005-08 www.BigResource.com, All rights reserved