Querying a row from MySQL database with PHP

Dell / M6300
May 11, 2009 at 11:40:48
Specs: Windows XP, 8 GB
This is a basic question, and I have some experience with PHP and databases. However, I can't seem to do this:

I just want to extract the contents of a row into an array. I've looked everywhere, but I only get sample codes that don't work.

Here is the portion of my code that is just returning a bunch of "Array" words in a row, instead of the strings that make up the row.

$result3 = mysql_query("SELECT * FROM VTABLE;") or die(mysql_error());
for($i = 0; $array[$i] = mysql_fetch_assoc($result3); $i++) ;
//array_pop($array);

echo $array[0] .'    '. $array[1] .'    '. $array[2] .'    '. $array[3] .'    '. $array[4] .'    ';

Any help would be appreciated.


See More: Querying a row from MySQL database with PHP

Report •


#1
May 11, 2009 at 11:58:26
I think I've found a temporary solution. I think I was looking in the wrong place: (the problem was that I was looking at the Ref ID, and not the table element names. (Also, in the example below, I was able to extract several values, by delimiting the SELECT names with a comma. An this is extracted with array syntax with the variable $row)


$result4 = mysql_query("SELECT num,PROGRESS FROM VTABLE WHERE num = '8'");

$row = mysql_fetch_row($result4);

echo $row[0]; // 8, or the num value from above.
echo $row[1]; // the 'progress' value (from above)


Report •

#2
May 12, 2009 at 16:20:51
I'm not sure if it's what you're after, but you can use print_r($array) to output the keys and values of an associative array.

In your first post,

$result3 = mysql_query("SELECT * FROM VTABLE;") or die(mysql_error());

if(mysql_num_rows($result3) > 0) {
   while($row = mysql_fetch_assoc($result3)) {
      print_r($row); echo "<br>";
   }
}

or perhaps

$result3 = mysql_query("SELECT * FROM VTABLE;") or die(mysql_error());

if(mysql_num_rows($result3) > 0) {
   while($row = mysql_fetch_assoc($result3)) {
      foreach($row as $key => $value) {
         echo $key . " = " . $value . "<br>";
      }
   }
}

______________________
My work in progress. I hate JS. :P


Report •

#3
May 18, 2009 at 10:21:59
Is there a way, once that databse is entered into a website to add the code needed for one of the columns in the mySql database ? I have a certain number of columns in my database and the first one is the "Add to Cart" column in which I want to stick a picture of a shopping cart with code links to the shopping cart form. I need to enter this code into each row of the Add to cart column and haven't found a way to do this yet.

Report •

Related Solutions

#4
May 19, 2009 at 12:57:06
Do you mean add / remove on an existing database / table? If so, then you'd need the alter privilege. You can either alter a database or a table.

If you mean update a column, then it would be something like

$res = mysql_query("update tbl_name set some_field=some_value where some_qualifier=some_requirement;");

If you need to insert a value, then

$res = mysql_query("insert into tbl_name values(...);");

HTH

______________________
My work in progress. I hate JS. :P


Report •


Ask Question