Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from MySQL database by specific id in url [duplicate]

Tags:

php

mysql

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.

like image 760
dan Avatar asked Nov 24 '25 18:11

dan


1 Answers

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'];
}
?>
like image 91
DirtyBit Avatar answered Nov 26 '25 09:11

DirtyBit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!