-2

Hello can you help me with this script?

Im trying to pull information on my slider from database.

$query = "SELECT * FROM `slider1`";
$select_from_slider1 = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_from_slider1)){
    $slider1_title = $row['slider1_title'];
    $slider1_content = $row['slider1_content'];
    $slider1_moreinfo = $row['slider1_moreinfo'];
    ?>
    <h2><?php echo $slider1_title ?></h2>
    <p><?php echo $slider1_content ?></p>
    <?php echo $slider1_moreinfo ?>
<?php } ?>

This is the error i get: enter image description here

Lol im sorry i figured it out myself, the reason for the error was that i dublicated many times include "database".... Thanks guys for the fast replies.!

Ledeniqt
  • 37
  • 1
  • 7
  • 2
    Check where you define `$connection`, doesn't look like its defined anywhere. Maybe you forgot to include the connection, or named the connection something other than `$connection`? – Qirel Jun 01 '17 at 13:13
  • ^^ Or you are calling this code in a function in which `$connection` is not in scope (was defined outside the function) – Michael Berkowski Jun 01 '17 at 13:14
  • You don't define $connection, so it's undefined;) – Mat Jun 01 '17 at 13:14
  • You should use PDO rather than mysqli. – Maxime Flament Jun 01 '17 at 13:18
  • 1
    @MaximeFlament That's rather opinionated - The MySQLi API supports prepared statement too, and its still a good, perfectly valid API to use, *if you want* to use it, there's nothing wrong about it. – Qirel Jun 01 '17 at 13:19
  • No it's not. mysqli's prepared statements are impossible on client side. Furthermore, PDO supports way more databases drivers than mysqli. For example, see: https://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059 – Maxime Flament Jun 01 '17 at 13:22
  • 1
    Yes, it *is* opinionated. If you like it - fine, but there's no need to force your choice upon others, if their choice works for them. Its rare to suddenly change your database driver out of the blue, and MySQLi has features which PDO doesn't. http://php.net/manual/en/mysqlinfo.api.choosing.php - There's nothing wrong with using `mysqli_*` if you want to - I personally like PDO better, but `mysqli_*` is just as valid as PDO. – Qirel Jun 01 '17 at 13:26
  • I solved it thanks guys! – Ledeniqt Jun 01 '17 at 13:31

1 Answers1

0

Looks like the var $connection isn't a mysqli connection. You'll need the following somewhere this script can access it.

$connection = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

http://php.net/manual/en/mysqli.construct.php

Blueline
  • 388
  • 1
  • 10
  • $db['db_host'] = "****"; $db['db_user'] = "****"; $db['db_pass'] = "****"; $db['db_name'] = "*****"; foreach ($db as $key => $value) { define(strtoupper($key), $value); } $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); if($connection){ //echo "connected"; im using this script – Ledeniqt Jun 01 '17 at 13:20
  • 1
    Sure, is this file included? Just do a simple test like create '$test = 5;' and see if you can echo $test in the file you are running the query from. If you can't echo 5 then you know it's an include problem, if you can echo 5 then there's probably an issue with the connection its self, like wrong password, no access to the db, etc. If there is a connection error you can access this with http://php.net/manual/en/mysqli.error.php – Blueline Jun 01 '17 at 13:38
  • It was the include "database.php" yep! thanks i solved it – Ledeniqt Jun 01 '17 at 13:45