-3

i want to get the backgroundColor of the Name headerBackgroud and its not printing anything, can you please help me?

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "DB#1";

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

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

$sql = "SELECT backgroundColor FROM background WHERE Name = 'headerBackground'";
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)) {
    echo $row['backgroundColor'];
}

1 Answers1

0

You mix up mysqli and mysql api, so just use PDO uniformly as given:

$dbc = new PDO('mysql:host=localhost;dbname='.$database, $user, $password);
$sql = "SELECT backgroundColor FROM background WHERE Name = :n"; 
$stmt = $dbc->prepare($sql);
$stmt->bindParam(':n', "headerBackground");
$stmt->execute();
if($stmt->rowCount() > 0){ 
  $data = $stmt->fetch(PDO::FETCH_ASSOC);
  $headerB = $data['headerBackground'];
}
manniL
  • 7,157
  • 7
  • 46
  • 72
  • 1
    Um, the OP's using `mysqli_` to connect with - how does this "answer" the present problem? You've made a mention of it... ok, but it doesn't solve what's posted. Maybe the OP's preference IS mysqli. – Funk Forty Niner Mar 15 '15 at 20:23
  • It "answers" the problem by providing a solution for getting the data from the database he wanted. My thought behind it was to use PDO, so the OP won't mix up mysqli and mysql. – manniL Mar 15 '15 at 20:30
  • Not in my books, sorry. That's just "me", but am sure there are others who feel the same way. ;-) – Funk Forty Niner Mar 15 '15 at 20:32
  • No problem, I understand your opinion @Fred-ii- :) – manniL Mar 15 '15 at 20:36
  • 1
    If I may add and for any future questions you may encounter: You see, many people who post code, often would like to know "why" their code failed in the first place, in order to not do the same mistake again. In doing so, you would have shown them where their errors were and how they could be fixed. I had a few questions in the past where I posted an alternate piece of code to fix the problem, but they were persistent in wanting to know where their faults were. Offering an alternate method is good, I'm all for it, but the initial question should have been addressed. I'm just trying to help. – Funk Forty Niner Mar 15 '15 at 20:56
  • 1
    Yeah, you are right. I'll first explain why the code of the OP doesn't work and then provide an alternative code snippet when I encounter such a question again :) – manniL Mar 15 '15 at 21:00
  • Thank you all guys for helping me :) – Bar Nisanov Mar 16 '15 at 08:58
  • yeah it doesnt, i found the right code: – Bar Nisanov Mar 16 '15 at 09:29
  • $host = "localhost"; $username = "root"; $password = ""; $dbname = "DB#1"; mysql_connect($host, $username, $password) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); $sql = mysql_query("SELECT backgroundColor FROM backgrounds WHERE name = 'headerBackground'"); $row = mysql_fetch_assoc($sql); echo $row['backgroundColor']; – Bar Nisanov Mar 16 '15 at 09:29
  • but still thank you! – Bar Nisanov Mar 16 '15 at 09:30