Link to home
Start Free TrialLog in
Avatar of KennethSumerford1
KennethSumerford1Flag for United States of America

asked on

PHP MySQL SQLi at Go Daddy 2011

The code below works well, thanks to Experts-Exchange, on my PC but not at Go Daddy.  I created a new DB  at Go Daddy, checked it but this code does not work on my site.  
What do I need to do to correct his problem? (passwords and hosts have been changed
for security reasons in this code below)

 function fnAddRecSeletRecs($hostName1, $userName3, $password3, $databaseName3 ) {
            // Add one record and display all records in the Prospect table.
            
      try {
            $conn1 = new mysqli($hostName1, $userName3, $password3, $databaseName3) ;
      /* check connection */
      if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
             
            exit(); // <==///========
            }
      echo ' database ' . $databaseName3 . ' is now connected to program. ' ;
        
      // Create SQL code to insert a record into the Prospect table.
      $SQL1 = "INSERT INTO Prospect (CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile) ";
      $SQL1 .= " VALUES('N', 'Y', 'John', 'Hammer', 'John@HammondTrees.com', 'Test only in 2011' )";
                
      $conn1->query($SQL1); // run the query
      printf (" |  New Record has id %d.\n", $conn1->insert_id);
      echo ' \  Prospect table ' ;
      // this works OK
            
      // use a Select to show current records
      $SQL2 = "SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile " ;
      $SQL2 .= " FROM Prospect " ;
      $result2 = $conn1->query($SQL2) ;
      $K1 = 0 ;

      while($row22 = $result2->fetch_array(MYSQLI_ASSOC)) {
            $K1 = $K1 + 1 ;
      echo $K1 . "  ";      
      echo "CustomerYN = ".$row22["CustomerYN"].", ActiveYN = ".$row22["ActiveYN"].", ";
      echo "FirstName = ".$row22["FirstName"].", LastName = ".$row22["LastName"].", ";
      echo "EmailMain = ".$row22["EmailMain"].", CoProfile = ".$row22["CoProfile"]."<br>";
      } // == end of while
      
      /* close connection */
      $conn1->close();
      return true ; // <===///==========
 } // === end of try

 catch ( Exception $e1) {
      echo "  error in database and table operations-- " . $e1 . " | " ;
                return false ; // <===///==========
  }
 } // === end of function ======================
      
      
    try
    {
    // Create DB connection to MySQL DB in MySQLi'
    $hostName1 = "localhost";
    $userName1 = "root";
    $password1 = "kxxx";
   
    $hostName2 =  "mktgdb.db.12345.hostedresource.com"; // host at GoDaddy
    // $userName2 = "Saaaaa"; // for Local DB on Dell 460
      $userName2 = "mktgdb";
    $password2 = "kxxxx";
    $databaseName1 = "mktgdb";
          $completedTF1 = false;
   
    $completedTF1 = fnAddRecSeletRecs($hostName2, $userName2, $password2, $databaseName1) ;// <---
    if ($completedTF1 = true) {
            echo " | function completed OK. | " ;
    }    
   
    } // == end of try
   
    catch ( Exception $e1)   {
          echo " Error in database operation.  Error:  " . $e1 . " | " ;
    } // === end of catch ==========
   
    ?>
    </p>

The error msg in Firefox follows:  Fatal error: Call to a member function fetch_array() on a non-object in D:\Hosting\7296466\html\Database-ops.php on line 119
SOLUTION
Avatar of evedder
evedder
Flag of Mexico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KennethSumerford1

ASKER

There is data in the table.  I checked it with PhpMyAdmin at Go Daddy yesterday.
I tried SQL instead of SQLi and it did Not work either.

function fnAddRecSeletRecs($hostName1, $userName3, $password3, $databaseName3 ) {
            // Add one record and display all records in the Prospect table.
            // Use SQL instead of SQLi at Go Daddy.
            
      try {
            //  $conn1 = new mysqli($hostName1, $userName3, $password3, $databaseName3) ;
      $conn1 =  mysql_connect($hostName1, $userName3, $password3) ;      
      /* check connection */
      /* if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());            
            exit(); // <==///========
            } */
      // select the DB
      mysql_select_db( $databaseName3, $conn1 ) ;      
      echo ' database ' . $databaseName3 . ' is now connected to program. ' ;
        
      // Create SQL code to insert a record into the Prospect table.
      // example of protecting entry from a Form
      $firstName1 = "Duke";
      $firstName1 = trim( strip_tags($firstName1));
      $firstName1 = mysql_real_escape_string($firstName1, $conn1);
      
      $SQL1 = "INSERT INTO Prospect (CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile) ";
      $SQL1 .= " VALUES('N', 'Y', '" . $firstName1 . "', 'Stout', 'S@HammondTrees.com', 'Test only in 2011' )";
      print " query 1 = " . $SQL1 . "  | ";
                
      // $conn1->query($SQL1); // run the query
      mysql_query($SQL1, $conn1);
      // printf (" |  New Record has id %d.\n", $conn1->insert_id);
      echo ' \  Prospect table ' ;
      // this works OK
            
      // use a Select to show current records
      $SQL2 = "SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile " ;
      $SQL2 .= " FROM Prospect " ;
       print " query 2 = " . $SQL2 . "  | ";
      // $result2 = $conn1->query($SQL2) ;
      $r1 = mysql_query($SQL2, $conn1) ;
      $K1 = 0 ;

      while ($row = mysql_fetch_array($r1)) {
            print "<p> " . $row[''] . " ";
            print $row['CustomerYN'] . " ";
            print $row['ActiveYN'] . " ";
            print $row['FirstName'] . " ";
            print $row['LastName'] . " ";
            print $row['EmailMain'] . " -----CoProfile:  ";
            print $row['CoProfile'] . " ";
            // print $row[''] . " ";
             print "</p> \n" ;
      }
      
      /* close connection */
      // $conn1->close();
      mysql_close($conn1);
      return true ; // <===///==========
 } // === end of try

The SQL queries and error is below.

query 1 = INSERT INTO Prospect (CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile) VALUES('N', 'Y', 'Duke', 'Hammer', 'John@HammondTrees.com', 'Test only in 2011' ) | \ Prospect table query 2 = SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile FROM Prospect |
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\7296466\html\Database-ops-SQL.php on line 132

Here is line 132:
while ($row = mysql_fetch_array($r1)) {
 
Avatar of team2005
team2005

Hi!

Do you get any error message ?
Hi!

Try this

while ($row[] = mysql_fetch_array($r1)) {

I tried it and below is what I got, including the SQL and the error msg.

database mktgdb is now connected to program. query 1 = INSERT INTO Prospect (CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile) VALUES('N', 'Y', 'Duke', 'Stout', 'S@HammondTrees.com', 'Test only in 2011' ) | \ Prospect table query 2 = SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile FROM Prospect |
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\7296466\html\Database-ops-SQL.php on line 133
I need to check with Go Daddy about the Username and password.  There could be a problem with how they want to see a few items.  I will also check about the Host name being in the correct format.
I talked to Go Daddy and things seem to be OK.  I even created a new DB and get the same error msg below.

 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\7296466\html\Database-ops-SQL.php on line 132

Here is the code that I tried:
  while ($row = mysql_fetch_array($r1)) {
and also--
  while ($row[] = mysql_fetch_array($r1)) {
Below is some code and print from the PHP page.

// use a Select to show current records
      $SQL2 = "SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile " ;
      $SQL2 .= " FROM Prospect " ;
       print " query 2 = " . $SQL2 . "  | ";
      // $result2 = $conn1->query($SQL2) ;
      $r1 = mysql_query($SQL2) ;
      $K1 = 0 ;

      if ($r1) {
            while ($row = mysql_fetch_array($r1)) {
            // while ($row[] = mysql_fetch_array($r1)) {
                  print "<p> " . $row[''] . " ";
                  print $row['CustomerYN'] . " ";
                  print $row['ActiveYN'] . " ";
                  print $row['FirstName'] . " ";
                  print $row['LastName'] . " ";
                  print $row['EmailMain'] . " -----CoProfile:  ";
                  print $row['CoProfile'] . " ";
                  // print $row[''] . " ";
                   print "</p>  " ;
            }
      }
      else {
            echo " |  ==result== did Not work for mysql_query() | ";
      }

  =======
 
database mktgdb2 is now connected to program. query 1 = INSERT INTO Prospect (CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile) VALUES('N', 'Y', 'Duke', 'Stout', 'S@HammondTrees.com', 'Test only in 2011' ) | \ Prospect table query 2 = SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile FROM Prospect |

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\7296466\html\Database-ops-SQL.php on line 132
Here is the new print from the Web page.

database mktgdb2 is now connected to program. query 1 = INSERT INTO Prospect (CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile) VALUES('N', 'Y', 'Duke', 'Stout', 'S@HammondTrees.com', 'Test only in 2011' ) | \ Prospect table query 2 = SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile FROM Prospect |

| ==result== did Not work for mysql_query() | | function completed OK. |

So the problem is probably here:

// $conn1->query($SQL1); // run the query
      mysql_query($SQL1, $conn1);
      // printf (" |  New Record has id %d.\n", $conn1->insert_id);
      echo ' \  Prospect table ' ;
      
            
      // use a Select to show current records
      $SQL2 = "SELECT CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile " ;
      $SQL2 .= " FROM Prospect " ;
       print " query 2 = " . $SQL2 . "  | ";
      // $result2 = $conn1->query($SQL2) ;
      $r1 = mysql_query($SQL2) ;
Is any body there?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will try Select * .
Below is the new result, which is not correct.

database mktgdb2 is now connected to program. query 1 = INSERT INTO Prospect (CustomerYN, ActiveYN, FirstName, LastName, EmailMain, CoProfile) VALUES('N', 'Y', 'Duke', 'Stout', 'S@HammondTrees.com', 'Test only in 2011' ) | \ Prospect table
query 2 = SELECT * FROM Prospect |  
| ==result r1== did Not work for mysql_query() | | function completed OK. |
It is 11 PM here in Texas.  I will work on this problem tomorrow around 2 PM.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Go Daddy and MySQL have some peculiar ways it seems, but it could be my lack of understanding of MySQL.
Hi!

Happy that you solve the problem :)