homepage Welcome to WebmasterWorld Guest from 54.166.15.152
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
mySql db query returning column headers, not values
mySql_db_query returning column headers, not values, and i dont know why.
tomking




msg:4437945
 12:37 pm on Apr 6, 2012 (gmt 0)

i got so much help from this thread [webmasterworld.com...]
but im still having trouble, snippet from my code

$query = "SELECT 'id', 'title', 'carImg' FROM content";

$result = mySql_db_query ($dbname, $query, $link);

$resultArray = array();

$numResults = 0;
while($row = mysql_fetch_array($result))
{
$cell = 0;
$resultArray[$numResults][0] = $row[$cell++];
$resultArray[$numResults][1] = $row[$cell++];
$resultArray[$numResults][2] = $row[$cell++];
$numResults++;
}

for($i = 0; $i < $numResults; $i++)
{
for($j = 0; $j < 3; $j++)
{
echo $i . ',' . $j . ' = ' . $resultArray[$i][$j] . '<br/>';
}
}


my results:
0,0 = id
0,1 = title
0,2 = carImg
1,0 = id
1,1 = title
1,2 = carImg

expected results
0,0 = 20
0,1 = someTitle
0,2 = someImgRef
1,0 = 21
1,1 = someTitle
1,2 = someImgRef

any help would be much appreciated, see a lot of very smart and helpful people here when i read.

thanks

tom

 

Frank_Rizzo




msg:4437949
 12:48 pm on Apr 6, 2012 (gmt 0)

Remove the single quotes from your select

$query = "SELECT id, title, carImg FROM content";

tomking




msg:4437954
 1:08 pm on Apr 6, 2012 (gmt 0)

thank you so much, i'm feeling quite blonde at the moment.

and i knew it was going to be something stupid.

much appreciated.

Tom

rocknbil




msg:4438792
 4:19 pm on Apr 9, 2012 (gmt 0)

tomking, I don't understand why you're doing the complicated array thing. Do you have a reason for that? Why can't you just do this?


$query = "SELECT id, title, carImg FROM content";
$result = mySql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo "<p>id: " . $row['id'] .
"title: " . $row['title'] .
"image: " . $row['carImg'] . "</p>";
}


Put that in a test file and give it a try. It should give you exactly the same results as you have there without all the added memory used by arrays you don't really need.

If you want to "store it for later" (though there is almost always a way to avoid that,)just index it by the record ID instead of a counter.


$query = "SELECT 'id', 'title', 'carImg' FROM content";
$result = mySql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo "<p>id: " . $row['id'] .
"title: " . $row['title'] .
"image: " . $row['carImg'] . "</p>";
$hugeResultsArray[$row['id']] = array($row['title'],$row['carImg']);
}


Dereferencing it will be the same as what you're doing there, you would just use a foreach instead of a counter.

tomking




msg:4439500
 10:11 am on Apr 11, 2012 (gmt 0)

fairhi rocknbil, sorry for the delay, had a few days off!

i dont have a large amount of php experience so it's probably not the most effiecnt way at all. What i was hoping to do, instead of doing a 2d array was just store the results object, but it just didnt appear to work.

i do need to store it for later, nearly immediately after i need to resort the results dynamically. with the newest result being in the center, the next result to the right, the next to the left, the next to the right etc,

eg results [0] [1] [2] [3] [4]
need them in this order
[4] [2] [0] [1] [3]

and i need it to work for n results.

tom

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved