-2

I have been trying to fix my code and it does not work at all. I have a local server and it does work (use Mamp on my macbook) I edited a small table called "forms" in my "hexameter" database.

Here's my code:

connection.php

<?php
$servername = "";
$username = "";
$password = "";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?> 

Connection does work!

input:

<!DOCTYPE html>
<html>
<body>

<form action="http://localhost:8888/index.php" method="GET">
Hexameter: <input type="text" name="Hexameter"><br>
<input type="submit" name"Eingabe">
</form>

</body>
</html>

index.php:

<!DOCTYPE html PUBLIC>
<html >
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php

$query = $_GET['Hexameter'];

$query = htmlspecialchars($query); 

$query = mysql_real_escape_string($query);

$raw_results = mysql_query("SELECT * FROM forms
        WHERE `text` LIKE '%".$query."%')" or die(mysql_error());


if(mysql_num_rows($raw_results) > 0){ 

    while($results = mysql_fetch_array($raw_results)){

            echo "<p><h3>".$results['title']."</h3>".$results['text']."            

 </p>";
    }
} else{ 
    echo "No results";
}

}

?>
</body>
</html>

I get always to index.php but there is nothing displayed.

halfer
  • 19,824
  • 17
  • 99
  • 186
Moe Joe
  • 75
  • 7

3 Answers3

0

When using mysqli, you have to add the connection string to your query.

In your case:

 $raw_results = mysqli_query($conn, "SELECT * FROM forms
    WHERE `text` LIKE '%".$query."%'") or die(mysqli_error());

Try that.

John Beasley
  • 2,577
  • 9
  • 43
  • 89
0

As several commenters noted, you're connecting with the database using mysqli, and then calling mysql functions. Worse than that, there's no database connection referenced anywhere in index.php. You omitted the $link_identifier parameter from your mysql_query call.

Switch your calls in index.php to mysqli calls and make sure you're using a valid $conn.

Juan Tomas
  • 4,905
  • 3
  • 14
  • 19
0

You should first require the connection.php before perform query on database.

Add this line at index.php:

require "connection.php";

After that, the connection are required in mysqli_query function, so add $conn as a parameter:

$raw_results = mysqli_query($conn, "SELECT * FROM forms
WHERE `text` LIKE '%".$query."%')" or die(mysqli_error());

I think that will solve your problem.