1

I am trying to build a customer e-commerce backend. I've done stuff like this many times before and don't consider myself "new" to php & mysql, but I am stuck and can't figure out what is wrong.

I just want to display the content of a mysql row at a specific location (using the "WHERE" command).

But when I load the page, the content part (in the tables) comes up empty. There is definitely content within the table at that location and everything else on the page displays EXCEPT for the actual customerResults.

Here is my code:

<head>
<title>Customer Summary</title>
<?php  
    session_start();
    require 'database_connect.php';
    $customerTable = "customer";

    if(isset($_GET['customer_click'])){
        $customerId = $_GET['customer_click']; 
    }
?>
</head>
<h3>Customer <?php echo"$customerId"?></h3>
<table align="center" width="600px">
    <tr>
        <td><a href="index.php">Summary</a></td>
        <td><a href="personal.php">Personal</a></td>
        <td><a href="billing.php">Billing</a></td>
        <td><a href="order_history.php">Order History</a></td>
    </tr>
</table>
<table align="center" width="400px">
    <tr>
        <?php 
            $customerSelect = "SELECT * FROM $customerTable WHERE id = '$customerId' ";
            $customerResult = mysql_query($customerSelect);
            if (!$customerResult){
                echo "No results, but why?!?!? </br/>";

            }
            if  (mysql_num_rows($customerResult)==0){
                echo "Results are empty...but why!?!?!";

            }

            while ($customerData = mysql_fetch_assoc($customerResult)){ 
                echo $customerData['id'];
                echo $customerData['email'];
            }

        ?>      
    </tr>
</table>

I could be over-looking something simple, but I really can't figure this out

paulcruse3
  • 179
  • 2
  • 20
  • New enough for me :). Welcome to Stack Overflow! Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is good PDO tutorial](http://goo.gl/vFWnC). – Madara's Ghost Jul 26 '12 at 18:44
  • So what errors are you getting? What have you tried? – Mike Brant Jul 26 '12 at 18:46
  • Have you verified you are not getting any Mysql errors? When `mysql_query` returns a `FALSE` result, it can mean either there were no rows, or the query did not work successfully. – mellamokb Jul 26 '12 at 18:46
  • What happens when you run it? PHP error, SQL error, empty result... – Vatev Jul 26 '12 at 18:46
  • Are you sure the $customerTable and more importantly the $customerId variables are set properly? – Florin Stingaciu Jul 26 '12 at 18:46
  • try just echo out the query, see what it generates and put it in mysql and execute from there. – lusketeer Jul 26 '12 at 18:48
  • @MikeBrant I am not getting any errors, it just displays NOTHING. Everything on the page shows up, except for the important part the actual database content – paulcruse3 Jul 26 '12 at 18:50
  • @Shade: Than you have error reporting disabled. See my answer. – Madara's Ghost Jul 26 '12 at 18:50
  • @mellamokb how do I know if the query didn't work sucessfully? how can a query not run successfully if I am calling it correctly... – paulcruse3 Jul 26 '12 at 18:51
  • @Flo I am sure the $customerId is correct because the "

    Customer

    " shows the correct CustomerId
    – paulcruse3 Jul 26 '12 at 18:52
  • @Shade: There could be an error in your syntax. `$customerId` may not be what you think it is. There could be a connection problem. There could be a server problem. A meteor may have crashed on the server farm. The thing is, you don't know, and you're not checking for any of these (save for the meteor, you'll probably know about that one soon enough :). – Madara's Ghost Jul 26 '12 at 18:53
  • @user1301840 how do I just echo out the query? I have tried echo'ing out the $customerSelect and it displays the correct mysql command... which query should I echo out and how? Thanks – paulcruse3 Jul 26 '12 at 18:53
  • @Shade there are a lot of reasons a query may not run successfully even if it is correct. In your case 'No results, but why?!?!? ' would mean the query did not run successfully. – Vatev Jul 26 '12 at 18:54
  • @Shade if the query is correct, then it shouldn't create a problem. did you try just run the query `$customerSelect` in mysql terminal or phpmyadmin? – lusketeer Jul 26 '12 at 18:59
  • @Truth: 'code' id 'code' in table is an int and when I echo out 'code' customerSelect 'code' it seems to me be an int. is there anyway to check? and if its wrong how do I fix it? – paulcruse3 Jul 26 '12 at 19:00
  • @user1301840 I just ran the `$customerSelect` in phpmyadmin, but I took out the variable `$customerTable ` & `$customerId`and replaced them with what I wanted it to be (`$customerTable` = customer) (`$customerId` = 5). And using that it DID WORK. But the actual page still does not show any results – paulcruse3 Jul 26 '12 at 19:07
  • @Shade it would help to show us what actually print out for this page, it seems like you aren't connected to the database – lusketeer Jul 26 '12 at 19:09
  • @user1301840 [link]http://crinno.com/custom/admin/customer_details/index.php?customer_click=5[link] is the link. EVERYTHING displays EXCEPT for what is in the `while` statement... – paulcruse3 Jul 26 '12 at 19:14
  • @Shade yea, looks like it's not even connecting to database, where you put that `database_connect.php` ? – lusketeer Jul 26 '12 at 19:22
  • I think I found the answer, the `$customerId` is a string, NOT an INT like it should be, and like it/I is expecting. I am going to change it now and then upload the answer... thanks everyone – paulcruse3 Jul 26 '12 at 19:25
  • @user1301840 the database_connect.php works for every other file I have. Including my orders page... so how could that be the problem – paulcruse3 Jul 26 '12 at 19:35

1 Answers1

4

Let's see:

  • Line 27: Undefined variable 'customerSelct'.
  • Line 41: Undefined variable 'customerDdata'.
  • Line 43: Undefined variable 'result'.

Plus Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Example code using PDO:

<?php
    try {
        session_start();
        if (!isset($_GET['customer_click'])) {
            throw new Exception('Customer ID not provided.');
        }
        //Assuming the ID must be a number.
        if (!is_numeric($_GET['customer_click'])) {
            throw new Exception('Customer ID must be numeric.');
        }
        $customerID = $_GET['customer_click'];
        $db         = new PDO("mysql:host=localhost;dbname=database_name_here", "user", "pass");
        //Have PDO to not emulate prepared statements by default.
        //Instead use MySQL's native prepare engine.
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        //PDO will throw PDOExceptions on every error.
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $query = "SELECT * FROM `customer` WHERE `id` = :id";
        $stmt  = $db->prepare($query);
        //Bind ID as a number and not as string.
        $stmt->bindValue(":id", $customerID, PDO::PARAM_INT);
        $stmt->execute();
        //Fetch all results into $result.
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    catch (PDOException $e) {
        //A database error has occurred!
        die("Database Error occurred! " . $e->getMessage());
    }

    catch (Exception $e) {
        //General error occurred!
        die("Error! " . $e->getMessage());
    }
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <pre>
        <?php print_r($result); ?>
    </pre>

</body>
</html>
Zoe
  • 27,060
  • 21
  • 118
  • 148
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Line 27 was just there to see if the mysql was running correctly. Line 41 was a typo and I don't see any result on line 34. What do u mean by msql_* code? What should I be using? – paulcruse3 Jul 26 '12 at 18:55
  • @Shade: Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is good PDO tutorial](http://goo.gl/vFWnC). – Madara's Ghost Jul 26 '12 at 18:56
  • @Shade: Also my bad, it was 43. – Madara's Ghost Jul 26 '12 at 18:57
  • yea those were all typos on my part, when fixed, pages still doesn't load correctly. I will have to look over the sites you gave me, thanks for the info. could my `mysql_*` code be causing the issue? – paulcruse3 Jul 26 '12 at 19:11
  • @Shade: Probably not, but it's definitely the cause of the headaches you're having. – Madara's Ghost Jul 26 '12 at 19:11
  • You are correct, the `$customerId` is a string, NOT an INT like it should be, and like it/I is expecting. I am going to change it now and then upload the answer... thanks – paulcruse3 Jul 26 '12 at 19:24
  • Even after changing to an int. it still doesn't work... I am not sure what the problem is but i want to pull my hair out lol. If you have any other clues please let me know... – paulcruse3 Jul 26 '12 at 19:38
  • @Shade: http://stackoverflow.com/questions/11675954/select-mysql-data-at-specific-row/11676050#comment15477872_11676050. Here's a hint. I hate repeating myself, let alone 3 times. – Madara's Ghost Jul 26 '12 at 20:18
  • Lol thanks for the link (again), I guess my last question will be is, what is the equivalent to my desired code in MySQLi? Or if you don't feeling like going there I'll just figure it out myself... thanks again – paulcruse3 Jul 26 '12 at 23:27
  • @Shade: I can show you PDO's if you want. IMO it's better than MySQLi anyway. (It's been a ong while since I've coded mysqli) – Madara's Ghost Jul 27 '12 at 07:11