Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Foreach could be used for this as well. It doesn't HAVE to loop over and insert all records. However, here is how it can be done with a for statement</p> <pre><code>//indexes are 0-indexed, meaning $xml[0] = first, $xml[119] = last (for 120 records) for($i=0;$i&lt;119;$i++) { $user = $xml[$i]; //Provided that the element you are looping over is an array of entities //Database insert logic here } </code></pre> <p>Please, please for the love of little puppies consider PDO and prepared statements, rather than using deprecated mysql_* functions, and unsanitized input. If your XML contains the name "O'malley" it WILL break your query!!</p> <p>Updated to include sample for foreach and with fields referenced:</p> <pre><code>//indexes are 0-indexed, meaning $xml[0] = first, $xml[119] = last (for 120 records) for($i=0;$i&lt;119;$i++) { $user = $xml[$i]; //Provided that the element you are looping over is an array of entities $name = $user-&gt;name; $sex = $user-&gt;sex; $address = $user-&gt;address; $result = mysql_query("insert into abc (name,sex,address) values ($name,$sex,$address)",$con) } echo $i+1." "."Records successfully added"; </code></pre> <p>With Foreach:</p> <pre><code>$limit = 119; $i = 0; //indexes are 0-indexed, meaning $xml[0] = first, $xml[119] = last (for 120 records) for($xml as $user) { if($i &gt; $limit) { break; } $name = $user-&gt;name; $sex = $user-&gt;sex; $address = $user-&gt;address; $result = mysql_query("insert into abc (name,sex,address) values ($name,$sex,$address)",$con) $i++; } echo $i+1." "."Records successfully added"; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload