Jump to content

Retrieve results from mysql as array and echo it out


Danbannan

Recommended Posts

Hello, im new here, and i have little experience to php and mysql as i started for 2 weeks ago.

I started out with some tutorials and feeling im getting the hang of it.

 

Enough of me, lets get to the point:

<?php

$con = mysql_connect('localhost',$user,$pass)or die(mysql_error());
$selectdb = mysql_select_db($selectdb)or die(mysql_error());

$sql = "SELECT * From table";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
$myarray = array($result);
$i =0;

while ($i < $num){

echo $myarray[$i];
$i++;
}

?>

 

Here i have written a dummyscript that does what the original script does, it tries to fetch the keys from the table and then trying to loop it and echo out the results.

The output in the browser is this: Resource id #3

 

I know this probably is a simple fix but i cant seem to get it sorted out. Hope some of you could help me get this baby work, or maybe have another way of doing it more "simple".

 

 

Thanks in advance!

Dan-Levi

Link to comment
Share on other sites

array($result) will not give you the data in the resource. You will need to use mysql_fetch_array or similar function to get those data.

 

Here is a sample code.

<?php

$con = mysql_connect('localhost',$user,$pass)or die(mysql_error());
$selectdb = mysql_select_db($selectdb)or die(mysql_error());

$sql = "SELECT * From table";
$result = mysql_query($sql);

//This loops until mysql_fetch_array returns false
//which means you have reached the last row
while ($row = mysql_fetch_array($result))
{
print_r($row);
}

?>

Link to comment
Share on other sites

Very interesting dolrichfortich :-) Thank you for clearing up some.

 

So i did not exactly use your example to go fourth, instead i kept the original while loop and thought about designing it somewhat different.

This is what i came up with:

 

<?php
//row details for the while loop (mysql_result())
$dbrow1 = "row1";
$dbrow2 = "row2";

$con = mysql_connect('localhost','$dbuser','$dbpass');
$select_db = mysql_select_db($dbname);

$sql = "SELECT * FROM testtable";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
$i = 0;

while ($i < $num) {
$dbresult1 = mysql_result($result, $i, $dbrow1);
$dbresult2 = mysql_result($result, $i, $dbrow2);

$i++;
}

//put the results automatically into an array

?>

What i am struggeling here is to make this piece of code automatically make an array of the results. Im pretty sure that isnt a big of a problem either, i know to make an array of this you need to:

 

array($dbresult1, $dbresult2);

 

But i cant seem to came up with how to approach this.

 

Again, thank you dolrichfortich, here is yet a challenge! hehe  :)

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.