1

I KNOW this has been asked already but nothing seems to work for my problem. I need to get some data from the database and display it in the HTML.

The code is thus:

<?php

$host="localhost"; // Host name 
$username="bluecode_power"; // Mysql username 
$password="bluecode123"; // Mysql password 
$db_name="bluecode_login"; // Database name 
$tbl_name="news"; // Table name 

$myConnection= mysqli_connect("$host", "$date", "$user", "$title", "$text")or die("cannot connect"); 
mysqli_select_db("$db_name")or die("cannot select database");

$query = "SELECT * FROM news"; //You don't need a ; like you do in SQL
$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr><td>" . $row['date'] . "</td><td>" . $row['news'] . "</td></tr>";  //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML

mysql_close(); 

?>

Thanks for any ideas :)

crablab
  • 25
  • 1
  • 3
  • 9
  • 1
    You're mixing `mysql_` and `mysqli_` functions. – Jay Blanchard Oct 27 '14 at 21:45
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 27 '14 at 21:45

3 Answers3

1

Where do you define $text variable? The value of $text is a string, although that fifth parameter in mysqli_connect() is the port # and expects a long or an integer value to be passed.

$myConnection= mysqli_connect("$host", "$date", "$user", "$title", "$text")or die("cannot connect");

should be:

$myConnection = mysql_connect( $host, $username, $password, $db_name ) or die("cannot connect");

Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
0

You have a problem at this line
$myConnection= mysqli_connect("$host", "$date", "$user", "$title", "$text")or die("cannot connect");
Main problem is $text.
its not defined and its the port number.Initialize $text before this line.
You passing some variable that are not initialized
like $date it should be $username
$user should be $password
$title should be $db_name
You dont need this line if you use 5 parameters
mysqli_select_db("$db_name")or die("cannot select database");

Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
  • I think I have managed to get confused between SQL and SQLi. I am trying to run this: mysql_connect("$host", "$date", "$user", "$title", "$text")or die("cannot connect"); – crablab Oct 27 '14 at 22:19
-1

use

$dbconnect=mysqli_connect("localhost", "root","","buba_user","users");

to connect to the database. Of course you need to put your own info but it will work, I getting connected to my database using this structure so it should work for you, remember to put the variables in order.