Problems communicating with mysql in php

86 Views Asked by At

I'm learning php and mysql at the moment and I'm working on getting my current website to be dynamic.

I've looked at some guides and worked on my own code and to my knowledge I believe it should work, but when I check it online it does nothing.

Is there anything obvious I have missed in the code below, and is there anyway to display errors in php which would help me discover what I've done wrong?

<?
  function functions($method, $query) {
    $con = mysql_connect("hostname","username","password");

    mysql_select_db("databasename", $con) or die(mysql_error());


    if($method = 'get') {
      $result = mysql_query($query);
      return $result;
      echo $result;
    }

    if($method = 'send') {
      $result = mysql_query($query);
    }
  }

  $query = "SELECT * FROM 'content' where 'ID' = 1";

  $heading = functions('get', $query);

?>

<h1><?php echo $heading;?></h1>
5

There are 5 best solutions below

0
On BEST ANSWER

First enable error reporting. Add this line inside your php tags as the first line.

error_reporting(E_ALL);

Then avoid using Short tags. Use <?php ?> tags. Sometimes short tags may have not enabled by your php configuration.

Check whether anything is returned from your query. Whether it was successful or failed.

and comparison operator is == or ===. You have used the assign operator (=) inside your function. You need to fix that, otherwise nothing will return.

and when you echo mysql_query(); it returns the resource only (if it was successful, otherwise false). You have to use mysql_fetch_array() or to get data from the query before you echo.

Finally, avoid using mysql_* extensions. They are deprecated. Use MySQLi or PDO_MySQL instead.

0
On

I'm not sure what you're asking, but if you want to know how to connect to a current MySQL database, start with this.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
 $conn->close();
 ?> 
0
On

The problem is your query, and yes you can get error messages but you need to use error checking. You're not checking to see if your query was successful or not. Try something like this:

if ( ! $result = mysql_query($query))
{
    // NB: die should never be used on a live website, use only whilst building the website
    die('ERROR['.mysql_errno().']: '.mysql_error());
}

That will give you an error with your query because you're using quotation marks around your table and column. MySQL doesn't use quotation marks there ('), but ticks `. It's a subtle but important difference, and they are optional. This would work fine:

SELECT * FROM content WHERE ID = 1
0
On

You are using wrong comparison... = has to be == in $method = 'get' and $method = 'send':

if($method == 'get') {
  $result = mysql_query($query);
  return $result;
  echo $result;
}

if($method == 'send') {
  $result = mysql_query($query);
}

And also you are using PHP short tag <?. So please check your php.ini on your server, if it is not disabled. You should user <?php.

And as others mentioned, you should not use mysql_*, its deprecated. Instead of that use MySQLi or PDO.

And if you want to see errors in your PHP, simply add this line to your code, somewhere in the beginning:

error_reporting(E_ALL ^ E_NOTICE);

It will show you all errors in your code, except notification messages, which you dont need. If you want to see also this notification messages, change it to:

error_reporting(E_ALL);
0
On

Is there anything obvious I have missed in the code below

You should be careful with your use of = and == because they mean different things. In your above example you have written the following:

if($method = 'get') {
  $result = mysql_query($query);
  return $result;
  echo $result;
}

This is actually checking to see if the assignment ($method = 'get') was successful. I imagine what you want to do is check to see if the $method is equal to 'get' which you would do like this:

if($method == 'get') {
  $result = mysql_query($query);
  return $result;
  echo $result;
}

Another thing to note is that you've named the function 'functions' which might be a little confusing. I suggest you try to name functions something a bit more descriptive so you can find it more easily in the future and as your code grows.

As others have commented you should probably use MySQLI now instead so here's a link to that too. It's not as daunting as it looks but it much better to learn now. PHP Mysqli

is there anyway to display errors in php which would help me discover what I've done wrong?

Often looking at the errors that you are getting is the only way to get a good idea of the issue you're having. Here's a great Stack Overflow question about errors and the frustrations that you might have with no errors showing: How to get useful error messages in PHP?