3

Im new to PHP MySQL. Im developing a songbook site.

I'm trying to pull data in a database from the ID in the URL site/publicsong.php?id=12.

    <?php


// Create connection
$conn = new mysqli($servername = "localhost";$username = "dansdlpe_dan";$password = "g+GbMr}DU4E@";$db_name = "dansdlpe_lyrics";);

// Check connection
$db_name = "dansdlpe_lyrics";
mysqli_select_db($conn,$db_name);

$id = $_GET['id'];
$id = mysqli__real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics_a` WHERE `id`='" . $id . "'";
$result = mysqli__query($conn,$query);

echo $row['id']; while($row = mysqli__fetch_array( $result )) {

echo "<br><br>";
echo $row['eng_title'];
echo $row['eng_lyrics'];
echo $row['alphabet'];
}
?>

I changed mysql_ to mysqli_ and added $conn.

And still i result is blank. Please help guys. Thanks in advance.

dan
  • 89
  • 1
  • 1
  • 15
  • you're mixing up the _deprecated_ mysql_* API's with mysqli – DirtyBit Sep 25 '15 at 08:20
  • also, youre not handling connection or query failures.. – amdixon Sep 25 '15 at 08:20
  • Andd this `echo $row['id']; while($row = mysql_fetch_array( $result )) {` doesn't do what you think it would. – DirtyBit Sep 25 '15 at 08:21
  • mixing mysql and mysqli in your code – Saty Sep 25 '15 at 08:21
  • And Im assuming that you haven't actually left the `$username = " "; $password = " "; $db_name = " ";` empty in your file, have you? Ps. Post your HTML too? – DirtyBit Sep 25 '15 at 08:23
  • @HawasKaPujaari no they are not empty. Please post a proper code. Im stuck. tq – dan Sep 25 '15 at 08:25
  • @dan Be a little polite eh? Everyone needs to know more about all of your relevant code at first x) Add your HTML to your question. – DirtyBit Sep 25 '15 at 08:27
  • @EliasNicolas I updated the question please take a look. If it is wrong Please format it to the right way. Thank you. – dan Sep 25 '15 at 08:42

3 Answers3

4

So I will stick to what you already have with some fixes.

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
$servername = "localhost";
$username = "xxxx";
$password = "xxxxxx";
$db_name = "xxxxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error){
  die("Connection failed: " . $conn->connect_error);
} 
$id = $_GET['id'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics_a` WHERE `id`='" . $id . "'";
$result = mysqli_query($conn,$query);

while($row = mysqli_fetch_array($result)) {
echo "<br><br>";
echo $row['id'];
echo $row['eng_title'];
echo $row['eng_lyrics'];
echo $row['alphabet'];
}
?>
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
-1

Try this:

<?php
$servername = "localhost";
$username = "";
$password = "";
$db_name = "test";


// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection

 mysql_select_db($db_name);

$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `lyrics` WHERE `id`='" . $id . "'";
$result = mysql_query($query);

while($row = mysql_fetch_array( $result )) {

echo "<br><br>";
echo $row['title'];
echo $row['content'];
 }
 ?>
sumitjainjr
  • 741
  • 1
  • 8
  • 28
-2

No need to use the id as a string,
you can use like : id=" . $id;

aldrin27
  • 3,407
  • 3
  • 29
  • 43
Prem
  • 22
  • 4