Welcome to WebmasterWorld Guest from 50.16.68.229

Forum Moderators: open

mySql db query returning column headers, not values

mySql_db_query returning column headers, not values, and i dont know why.

   
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
12:48 pm on Apr 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove the single quotes from your select

$query = "SELECT id, title, carImg FROM content";
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
4:19 pm on Apr 9, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month