0

Heads Up! Just starting out in PHP so I apologize in advance if this is some sort of n00b error. I have a little script in PHP which reads a table from a MySQL database and provides the values of a single column as a drop-down list. The PHP code is as follows:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
    $dbc = mysql_connect("localhost", "someDBuser", "somecorrectpass");
    $db = mysql_select_db("someDB");
    $results= mysql_query("SELECT name FROM sometable");
?>
<select name="eventid">
    <option value="0">Choose</OPTION>
    <?php
        while($row = mysql_fetch_array($results)) {
            echo '<option value="'.$row['name'].'">'. $row['name'].'</option>';
        }
    ?>
</select>
</form>
</body>
</html>

Now when I call the file from the browser, I get a Blank Screen. I enabled PHP Error reporting and get the following error Fatal error: Call to undefined function mysql_connect() in /var/www/html/testdel.php on line 12.

Next I tried running the file in the backend using the PHP command. It ran fine and gave me the required html output like so:

$ php /var/www/html/testdel.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" action="/var/www/html/testdel.php">
<select name="eventid">
    <option value="0">Choose</OPTION>
    <option value="Ex1005">Ex1005</option><option value="Ex1009">Ex1009</option><option value="user1">user1</option></select> 
</form>
</body>
</html>

And when I put this output in a file (say "testdel.html", for example), I get the correct output.

What am I doing wrong in the original PHP file? Is it something related to permissions? The current file permissions are 644. Or do I need to include(use) some module?

rahuL
  • 3,330
  • 11
  • 54
  • 79
  • Please use mysqli_ or PDO. Mysql_ has been deprecated. see here http://es1.php.net/function.mysql-connect. Please also confirm if you have ssh access to the server. – The Humble Rat Jan 22 '14 at 14:13
  • 1
    A *Fatal Error* stops the execution of the PHP script. In this case, it appears your MySQL extensions aren't being loaded. Did you check `phpinfo();` output to see if they're available? Have a look at [this question](http://stackoverflow.com/questions/8551398/). Also, see **[Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)**. – Amal Murali Jan 22 '14 at 14:14
  • check whether mysql extension is enabled or not – Shidil Eringa Jan 22 '14 at 14:14
  • 1
    Which version of `PHP` are you running? – Nick R Jan 22 '14 at 14:15
  • Calling php from command line and using it as e.g. apache module does neither mean that they have the same settings and modules nor that it is the same version of php. – t.niese Jan 22 '14 at 14:15
  • @TheHumbleRat - Thank you. Going through those docs now – rahuL Jan 22 '14 at 14:23
  • 1
    @i.h4d35 I have included an answer using mysql_i. This or PDO is the best method. – The Humble Rat Jan 22 '14 at 14:30

4 Answers4

3

First

NEVER do connection or sql instructions directly in your page, like that, it's a terrible bad pratice and not safe of course.

Create for example, a file/Class just for connect, like connection.php and them call the method, for example: Connection::start();.

For SQL instructions, do the same. Create your class, for example: class SomeTable, and them call the method you need: SomeTable::getAll();.

It's just a example.

Second

Use PDO MySQL

Possible problems

Check if your extension php_mysql is uncommented and present in you php.ini. In UNIX, maybe this /etc/php/php.ini. In windows I don't know. Like that:

extension=php_mysql.so
extension=php_pdo_mysql.so

OR, in Windows:

extension=php_mysql.dll
extension=php_pdo_mysql.dll

To make sure this, create a file, create a file in you DocumentRootwith this code <?php phpinfo(); ?> and search for mysql or --with-mysql. For example:

  • Create a file in /var/www/info.php
  • Write this code: <?php phpinfo(); ?>
  • In your browser, call this file: http://localhost/info.php
  • And search for mysql

Image of phpinfo()

PDO - MySQL


If you have any observation, tell me in the comments.

And sorry for my english.

Patrick Maciel
  • 4,874
  • 8
  • 40
  • 80
  • 1
    Thanks a lot - the extensions weren't loaded. I've also taken your advice to use PDO instead of direct connections – rahuL Jan 22 '14 at 14:28
  • @i.h4d35 It is best to do. Tip: Continue studying PHP (O.O, PDO, Security, Performance, Upload, etc) to learn all that the language offers, and later, for better productivity, learn some framework (or create your own, if you feel better). Recommend Laravel 4.1 and Phalcon but initially CodeIgniter and CakePHP for a more basic idea of how everything works and also for know other frameworks. – Patrick Maciel Jan 22 '14 at 14:36
1

Try to use the following code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
    $db = new mysqli('hostname', 'root', 'root', 'db_name');
    if($db->connect_errno > 0)
    {
      die('Unable to connect to database [' . $db->connect_error . ']');
    }
    $sql = 'SELECT name FROM sometable';

    if(!$result = $db->query($sql))
    {
       die('There was an error running the query [' . $db->error . ']');
    }
?>
<select name="eventid">
    <option value="0">Choose</OPTION>
    <?php
        while($row = $result->fetch_assoc())
        {
            echo '<option value="'.$row['name'].'">'. $row['name'].'</option>';
        }
    ?>
</select>
</form>
</body>
</html>

This now uses mysql_i instead of mysql_ which is now deprecated.

If you have access to your serv via SSH you may also need to install php-mysql as seen in this answer for a similar question

Fatal error: Call to undefined function mysql_connect()

Community
  • 1
  • 1
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
0

You can take these measures to find/fix your issue. This might be helpful to you.

  1. You can verify that your installation of PHP has been compiled with mysql support or not now. Create a php test web page containing

    <?php
    phpinfo();
    ?>

and load it in your browser. It will show environment loaded configurations. Search the page for MySQL. If you don't see it, you need to recompile PHP with MySQL support, or reinstall a PHP package that has it built-in.

2. Verify that the line to load the mysql extension in php.ini has been uncommented. In Linux, the line is extension=mysql.so and in Windows, the line is extension=php_mysql.dll. Uncomment the line by removing the semi-colon.

3. If any changes is made in php.ini then restart the apache service, for making the changes into effect.

Suraj Kumar
  • 564
  • 1
  • 3
  • 11
-4

I only see one problem..

$results= mysql_query("SELECT name FROM sometable");

To:

$results = mysql_query("SELECT name FROM sometable");

The other codes are perfect!

  • 1
    And what is the *problem* you see in the OP's code? Both the lines in your answer are exactly the same. – Amal Murali Jan 22 '14 at 14:17
  • I'm not seeing the fix other than a space after `$results` which is not the issue here – The Humble Rat Jan 22 '14 at 14:17
  • Downvoted for a very minor and subtle change - spacing, while a good practice, does not have anything to do with the OP's issue, and it wont break the query as you have implied. – Adam Jan 22 '14 at 15:07