Viewed   71 times

What are some different ways to loop through a mysql result set? I'm new to PHP and MySQL so I'm looking for simple ways to loop through and an explanation as to how the provided code works.

 Answers

4

Here is a full example:

http://php.net/manual/en/mysqli-result.fetch-array.php

  1. Connect
  2. Select database
  3. Make query
  4. Cycle on the result and fetch array to get the row
Tuesday, December 13, 2022
2

This answer has been edited several times based on new info provided by the OP

Is root actually allowed to connect to the server from the host that you are connecting from? If the error string is returning the canonical name of the server, there's a very good chance that 'localhost' is not pointing to 127.0.0.1 :

"Access denied for user 'root'@'MY.SERVER.HOST.NAME' to database 'dbname'"

That should echo something like "Access denied for user 'root'@localhost'", not the name of the server.

Try:

$con = mysql_connect("127.0.0.1","root","pass");

Edit (After more information provided in comments)

If you are connecting from a totally different host, you have to tell MySQL user@remote_hostname_or_ip is allowed to connect, and has appropriate privileges to create a database and users.

You can do this rather easily using phpmyadmin (on the MySQL server), or a query like:

CREATE USER 'root'@'192.168.1.1' IDENTIFIED BY PASSWORD 'secret';

GRANT ALL PRIVILEGES ON * . * TO  'root'@'192.168.1.1' IDENTIFIED BY PASSWORD 'secret' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

I would advise not naming this user 'root' , just create a user with all of the global privileges needed. In the example, I used 192.168.1.1, that could easily be a hostname, just make sure DNS is set up appropriately. Specify the host to match exactly as it appears in logs when you connect to the remote server.

You may also want to adjust limits to taste. More information on the CREATE USER syntax can be found here, GRANT here.

Edit

If using MySQL 4 - CREATE is not an option. You would just use GRANT (4.1 Docs On User Management)

Edit

If using C-Panel, just use the API. While yes, it does have its quirks, its easier to maintain stuff that uses it rather than ad-hoc work arounds. A lot of successful applications use it without issue. Like any other API, you need to stay on top of changes when using it.

Saturday, August 13, 2022
 
tpojka
 
2

obviously 'localhost' isn't the correct URL that you want to connect to.

if you're running this app from an emulator and you want to connect to a web server running on the pc running the emulator, use 10.0.2.2, more about this - http://developer.android.com/tools/devices/emulator.html

if you're not on an emulator, use the actual ip/host of the machine running the web server. make sure its accessible to the Android device first.

Wednesday, September 14, 2022
4

If you are using mysql2 gem then you should be getting the mysql2 result object and according to the docs you should be able to do the following

results.each do |row|
  # conveniently, row is a hash
  # the keys are the fields, as you'd expect
  # the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
  # Here's an otter: http://farm1.static.flickr.com/130/398077070_b8795d0ef3_b.jpg
end

Checkout the documentation here

So in you case you can do the following

<% @results.each do |val| %>
   <%= "#{val['id']}, #{val['name']}, #{val['age']}" %>
<% end %>

Edit: you seem to be referring to the wrong doc check the Mysql2 gems doc.

Friday, September 30, 2022
 
4

A regular for each will do:

foreach (var item in collection)
{
    // do your stuff   
}
Monday, August 8, 2022
 
kvista
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :