2
<?php
    $conn = mysqli_connect('hostname','username','passwd','dbname');
    $query = 'SELECT name,mobileno FROM test';
    $result = mysqli_query($conn,$query);

    while($row = $result->fetch_array()) {
        $name = $row['name'];
        $monileno = $row['mobileno'];

        $query2 = "INSERT INTO test2(name2,mobileno2) VALUES('$name','$monileno')";
        mysqli_query($conn,$query2);
       }
?>


CREATE TABLE `test` (
  `id` INT(9) AUTO_INCREMENT PRIMARY KEY, 
  `name` varchar(128) NOT NULL,
  `mobileno` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `test` (`id`, `name`, `mobileno`) VALUES
(1, 'Omar Faruq', '01911775477'),
(2, 'omar Al Faruq', '01911775477'),
(3, 'Helal Uddin', '01914351075');

CREATE TABLE `test2` (
  `id` INT(9) AUTO_INCREMENT PRIMARY KEY, 
  `name` varchar(128) NOT NULL,
  `mobileno` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

This code works on Xampp/wamp but when I upload this script to RHEL7.2 web server it does not works.

Hkachhia
  • 4,463
  • 6
  • 41
  • 76
Omar Faruq
  • 31
  • 4

2 Answers2

0
<?php
$conn = mysqli_connect('hostname','username','passwd','dbname');
$query = 'SELECT name,mobileno FROM test';
$result = mysqli_query($conn,$query);

while ($row = mysqli_fetch_assoc($result)) { 
  $name = mysqli_escape_string($conn, $row['name']);
  $monileno = mysqli_escape_string($conn, $row['mobileno']); 

$query2 = "INSERT INTO test2(name2,mobileno2) VALUES('$name','$monileno')";
mysqli_query($conn,$query2);
?>

and change on Database :

CREATE TABLE test2 ( id INT(9) AUTO_INCREMENT PRIMARY KEY, name2 varchar(128) DEFAULT NULL, mobileno2 varchar(128) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Finally it works on RHEL7. I am really happy...... Thanks All

Omar Faruq
  • 31
  • 4
-2

try replacing

    while($row = $result->fetch_array()) {

with

    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    // OR
    while ($row = mysqli_fetch_assoc($result)) {

It seem's like you we're mixing between the object-oriented usage style and the procedural style.

I'm surprised this worked for you. probably might be due to different php versions.

same thing can apply to

VALUES('$name','$monileno')";
//replace with
VALUES('" . mysqli_escape_string($conn, $name) . "', '" . mysqli_escape_string($conn, $monileno) . "')";
yosher lutski
  • 306
  • 2
  • 8