Jump to content

Problem with DB connection


silviya

Recommended Posts

Hello there! I'm new to this forum and I'm new to PHP coding also. I wrote something that doesn't make exactly what I thought it will. Can you give me a hand please. There we go:

 <?php
            error_reporting(E_ALL);

            $Host = "***";
            $User = "***";
            $PassWord = "****";
            $DataBaseName = "***";
            $TableName =      "products";

            mysql_connect($Host, $User, $PassWord);
            mysql_select_db($DataBaseName);

            if (isset($_GET['id'])) {
                $id = (int)$_GET['id'];
                // $select =  "SELECT id FROM" . $TableName;
                if ($id > 0) {

                    $select = "select description from" . $TableName . "WHERE id = " . $id;
                    $result = mysql_query($select);
                    echo $result;
                    echo $id;
                }

                else {
                    echo "id not > 0";

                }
            }
            else {
                echo "ID not set;
            }
            ?>
<p><a href="proba.php?id=1" name="id=1"> Find out more about it. </a></p>
<a href="proba.php?id=2" name="id=2"> Find out more about it. </a></p>
<a href="proba.php?id=3" name="id=3"> Find out more about it. </a></p>

 

That's a testing page only. What happens here is that only the ID is displayed.

The funny thing is when i add this code to the original page, my apache gives errors on the localhost and i get

Premature end of script headers
. Obviously I'm doing something wrong, but I don't know what it is. oh, when i run this code on the original page but remove the password, apache is not shoting... :shrug:

I feel like and idiot...

Link to comment
https://forums.phpfreaks.com/topic/178709-problem-with-db-connection/
Share on other sites

On my localhost, where it actually does something: PHP Version :5.3.0 

The testing server that i use is free hosting provider's, yes. Awardspace. Used 4.4.1, later on tested with 5.2.5. Same thing. Premature end of script.

 

Again I'm saying. Just this code, run on different file on the localhost, brings back just the ID. Run with the rest of the page, brings apache error. Alone the script is just showing the ID again on the free server, with the rest of the page - gives Premature end of script errror.

Well just off the top of my head, you attempt to connect to a database and assume that is successful.  At no time do you actually check a result, to insure that things actually worked.  This is not a good approach, and can often result in problems.

gizmola, there is no excuse for what I did...corrected.

            <?php
            error_reporting(E_ALL);
            .....

            $connect = mysql_connect($Host, $User, $PassWord);

            if (!$connect) {
            die("Could not connect to database:" . mysql_error());
            }
            mysql_select_db($DataBaseName);
            if (!$DataBaseName) {
                die("Could not select database:" . mysql_error());
            }


            if (isset($_GET['id'])) {
                $id = (int)$_GET['id'];
                if ($id > 0) {

                    $select = "select description from" . $TableName . "WHERE id = " . $id;

  $result = mysql_query($select, $connect);

  if (!$result) {
    die("Could not execute query:" . mysql_error());
  }
  
  else {
                    echo $result;
                    echo $id;
                }
                }
                else {
                    echo "id not > 0";

                }
            }
            else {
                echo "id not set";
            }
            mysql_close($connect);
            ?>
<p><a href="proba.php?id=1" name="id=1"> Find out more about it. </a></p>
<a href="proba.php?id=2" name="id=2"> Find out more about it. </a></p>
<a href="proba.php?id=3" name="id=3"> Find out more about it. </a></p>

 

But no difference at all

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.