1

Setup -> Using host gator , php, phpmyadmin, mysql:

PHP Code (in the file called ajaxTest.php):

<?php

$con=mysqli_connect("localhost","refinedc_dbadmin","password","refinedc_currency");

// Check connection
if (mysqli_connect_errno()) 
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{



$result = mysqli_query($con,"SELECT * FROM ITEMS");

if (!$check1_res) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}

echo '[';

while($row = mysqli_fetch_array($result)) 
{
  echo '{';
  echo '"ID":' . '"' . $row['ID'] . '",';
  echo '"YEAR":' . '"' . $row['YEAR'] . '",';
  echo '"QUANTITY":' . '"' . $row['QUANTITY'] . '",';
  echo '"DENOMINATION":' . '"' . $row['DENOMINATION'] . '",';
  echo '"TYPE":' . '"' . $row['TYPE'] . '",';
  echo '"COUNTRY":' . '"' . $row['COUNTRY'] . '",';
  echo '"COIN_NAME_OR_TITLE":' . '"' . $row['COIN_NAME_OR_TITLE'] . '",';
  echo '"COLLECTIBLE_METAL_ONE":' . '"' . $row['COLLECTIBLE_METAL_ONE'] . '",';
  echo '"COLLECTIBLE_METAL_TWO":' . '"' . $row['COLLECTIBLE_METAL_TWO'];
  echo '},';
}

echo ']';

}
mysqli_close($con);

?>

phpmyadmin table: enter image description here


mysql access levels for user and database: enter image description here


user is added to the database: enter image description here


And when i hit the page this is the issue i get: enter image description here


UPDATE - Changed the SQL to read SELECT * FROM items... Now get this when I try to hit the page (hits the error code and runs exit() but prints out no error: enter image description here


Why am i receiving a table does not exist error? I have already shortened the name of the database and rebuilt a new one, but it still says it doesn't exist!

5
  • 2
    do you mean SELECT * FROM items? Commented Jun 24, 2014 at 3:45
  • @bansi when i change it to lower case, it still goes into that error check and calls 'exit()' but does not print out any errors... Commented Jun 24, 2014 at 3:51
  • 1
    what is $check1_res? I think you should be using if (!$result){ Commented Jun 24, 2014 at 4:19
  • 1
    Also, are you trying to convert it to json? then try using json_encode Commented Jun 24, 2014 at 4:27
  • @bansi you sweet bastard! that did the trick! I am not too sure what $check1_res was, it was on W3schools sample php ..... but your way worked and returned a json data to me! Thank you so much. you should make this into an answer with all three of your tips and I will pick it as correct ;) Commented Jun 24, 2014 at 13:53

2 Answers 2

4

On windows table names are case insensitive while on *nix systems table names are case-sensitive.

Host gator is likely running on unix, hence if your table is named

 items

you need to call it as such

  $result = mysqli_query($con,"SELECT * FROM items");
Sign up to request clarification or add additional context in comments.

1 Comment

when i change it to lower case, it still goes into that error check and calls 'exit()' but does not print out any errors...
1

Here is the corrected code. I have added comment in the code itself.

<?php

$con=mysqli_connect("localhost","refinedc_dbadmin","password","refinedc_currency");

// Check connection
if (mysqli_connect_errno()) 
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{

    $result = mysqli_query($con,"SELECT * FROM `items`");
    // Check if result is ok
    // if there is an error $result will be false and the error details is available in mysqli_error()
    if (!$result) {
        printf("Error: %s\n", mysqli_error($con));
        exit();
    }

    // Use json_encode to convert data to json.
    // It is too tedious and error prone to convert data to json by hand.
    $data = array();
    while($row = mysqli_fetch_array($result)) 
    {
        //Store the result to array
        $data[]=array(
            "ID" => $row['ID'],
            "YEAR" => $row['YEAR'],
            "QUANTITY" => $row['QUANTITY'],
            "DENOMINATION" => $row['DENOMINATION'],
            "TYPE" => $row['TYPE'],
            "COUNTRY" => $row['COUNTRY'],
            "COIN_NAME_OR_TITLE" => $row['COIN_NAME_OR_TITLE'],
            "COLLECTIBLE_METAL_ONE" => $row['COLLECTIBLE_METAL_ONE'],
            "COLLECTIBLE_METAL_TWO" => $row['COLLECTIBLE_METAL_TWO']
        );
    }
    //echo the json
    echo json_encode($data);
}
mysqli_close($con);

BTW: don't use W3schools if you really want to learn and do it in the correct way. Lot of the things there are outdated and misleading.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.