Computing.Net > Forums > Web Development > Querying a row from MySQL database with PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Querying a row from MySQL database with PHP

Reply to Message Icon

Name: genscripter
Date: May 11, 2009 at 11:40:48 Pacific
OS: Windows XP
CPU/Ram: 8 GB
Product: Dell / M6300
Subcategory: PHP
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: genscripter
Date: May 11, 2009 at 11:58:26 Pacific
Reply:

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)


0

Response Number 2
Name: shutat
Date: May 12, 2009 at 16:20:51 Pacific
Reply:

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


0

Response Number 3
Name: jamesfleeger
Date: May 18, 2009 at 10:21:59 Pacific
Reply:

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.


0

Response Number 4
Name: shutat
Date: May 19, 2009 at 12:57:06 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Please tell me some WEBMA... Prog to create HTML Menu



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Querying a row from MySQL database with PHP

Downloading a file from mysql www.computing.net/answers/webdevel/downloading-a-file-from-mysql/2373.html

PEAR isn't returning every column www.computing.net/answers/webdevel/pear-isnt-returning-every-column/3634.html

mail merge with php and mysql? www.computing.net/answers/webdevel/mail-merge-with-php-and-mysql/3975.html