Get mysql login information from config
#1
Hi, there
I'm using a simple php script for advertising which is connected to the mysql db.
Here the script:
<?php
$mysql_hostname = "localhost"; //host
$mysql_user = "mybb"; //user
$mysql_password = "sdl209a"; //pass
$mysql_database = "mybb"; //db
$link = mysqli_connect($mysql_hostname,$mysql_user,$mysql_password,$mysql_database) or die("Error " . mysqli_error($link));

$get = mysqli_query($link,"SELECT * FROM ads WHERE size='468x60' ORDER BY RAND() LIMIT 0,1");
while($row = mysqli_fetch_assoc($get))
{
$link = $row['link'];
$banner = $row['image'];
$name = $row['name'];
echo "<a target='_blank' href='$link' rel='nofollow' title='$name' alt=''><img src='$banner' title='$name' width='468px' height='60px' style='border: 0px' /></a>";
}
@mysqli_free_result($get);
@mysqli_close($link);
?>
I want to make this script to get the mysql_user, mysql_password and mysql_database directly from the config.php
Reply
#2
What about using require_once()?
Reply
#3
I tried but I cannot get the data (query) properly.
Even I don't need this:
$mysql_hostname = "localhost"; //host
$mysql_user = "mybb"; //user
$mysql_password = "sdl209a"; //pass
$mysql_database = "mybb"; //db
Because on all pages that I use this code I'm already connected to the DB but as I said I can't manage to query the data properly.
Reply
#4
If you aren't using the MyBB whole framework maybe you can make use of the DB classes?

If you are able to connect with your current code then maybe you are doing something wrong right after.
Reply
#5
I tried that way:
<?php
$get = mysqli_query("SELECT * FROM ads WHERE size='468x60' ORDER BY RAND() LIMIT 0,1");
while($row = mysqli_fetch_assoc($get))
{
$link = $row['link'];
$banner = $row['image'];
$name = $row['name'];
echo "<a target='_blank' href='$link' rel='nofollow' title='$name' alt=''><img src='$banner' title='$name' width='468px' height='60px' style='border: 0px' /></a>";
}
@mysqli_free_result($get);
@mysqli_close($link);
?>
But I get theese warnings:
Warning [2] mysqli_query() expects at least 2 parameters, 1 given - Line: 2 - File: global.php(844) : eval()'d code(52)
Warning [2] mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given - Line: 3 - File: global.php(844) : eval()'d code(52)

How to fix it?
Reply
#6
https://www.php.net/manual/en/mysqli.query.php

For the second you will need to check if $get is false before running your while loop. I think we would need to know a little more about your database structure to guess if your query is correct (assuming you are indeed connected).

Also, why don't you try using object oriented style?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)