Note that there are some explanatory texts on larger screens.

plurals
  1. POphp mysql infinite loop
    primarykey
    data
    text
    <p>I have the following code which stays in a constant loop unless I comment *$conn->query($insert_statement.$values);*. With that line commented I get exactly the correct printed output as it will finish the loop at the last insert statement.</p> <pre><code>for($count =0; $count &lt; $rs-&gt;num_rows; $count++){ $values = ""; foreach($keys as $key){ $values = $values . "'".$row-&gt;$key."', "; } $values = substr($values, 0, strlen($values)-2) . ");\n"; echo $insert_statement.$values."\n"; $conn-&gt;query($insert_statement.$values); } </code></pre> <p>Here's the entire code. I am trying to make a script the backs up a table.</p> <pre><code>&lt;? $date = getdate(); $date = $date['month']."_".$date['mday']."_".$date['year']; $conn = new mysqli("localhost", "root", "ranger", "customers"); $conn-&gt;query("drop table if exists backup_$date"); $rs = $conn-&gt;query("DESCRIBE customers"); $create_statment = "create table backup_$date ("; $primary_key = ""; $keys = array(); $insert_statement = "insert into backup_$date ("; $count = 0; while($row = $rs-&gt;fetch_object()) { if($count &gt; 0){ $keys[] = "".$row-&gt;Field.""; $insert_statement = $insert_statement . "`".$row-&gt;Field."` ,"; } if($row-&gt;Null == "NO"){ $create_statment = $create_statment . "`".$row-&gt;Field."` ".$row-&gt;Type." not null"; if($row-&gt;Extra == "auto_increment"){ $create_statment = $create_statment ." not null auto_increment, "; }else{ $create_statment = $create_statment . ","; } }else{ $create_statment = $create_statment . " `".$row-&gt;Field."` ".$row-&gt;Type.", "; } if($row-&gt;Key == 'PRI'){ $primary_key = "primary key(".$row-&gt;Field.")"; } $count++; } $create_statment = $create_statment . " $primary_key);"; $conn-&gt;query($create_statment); $rs = $conn-&gt;query("select * from customers;"); $insert_statement = substr($insert_statement, 0, strlen($insert_statement)-1).") values ("; $row = $rs-&gt;fetch_object(); for($count =0; $count &lt; $rs-&gt;num_rows; $count++){ $values = ""; foreach($keys as $key){ $values = $values . "'".$row-&gt;$key."', "; } $values = substr($values, 0, strlen($values)-2) . ");\n"; echo $insert_statement.$values."\n"; $conn-&gt;query($insert_statement.$values); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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