Jump to content

Issues with Basic PHP echo


barryflood22

Recommended Posts

I am having issues with a basic PHP echo script. I am trying to echo all titles in a table, the table name is right and the database is connecting as I am getting a "connected" message appearing on screen, apart from that i'm getting absolutely nothing being output.

 

I have removed the passwords for obvious reasons.

<?php
	$host = 'localhost';
	$user = '';
	$db = '';
	$password = '';
	
	// Create connection
$con=mysqli_connect($host,$user,$password,$db);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  else echo "connected";
  ?>
  
  
  
  <?php
  
$result= mysql_query("SELECT * FROM jos_content");
 while($row = mysql_fetch_array($result)){
      echo $row['title'];
 }
?>
Link to comment
https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/
Share on other sites

Just changed it to this but still no luck

<?php
	$host = 'localhost';
	$user = '';
	$db = '';
	$password = '';
	
	// Create connection
mysql_connect($host, $user, $password, $db) or die(mysql_error());
echo "Connected to MySQL<br />";
  ?>
  
  <?php
  
$result= mysql_query("SELECT * FROM jos_content");
 while($row = mysql_fetch_array($result)){
      echo $row['title'];
 }
?>

Got it working with this script

<?php
	$host = 'localhost';
	$user = '';
	$db = '';
	$password = '';
	
	// Create connection
$link = mysql_connect($host, $user, $password);
mysql_select_db($db, $link);

$result = mysql_query("SELECT * FROM jos_content", $link);
while($row = mysql_fetch_array($result)){
      echo $row['title'];

}
  ?>
  
  
  

I still recommend mysqli

<?php
	$host = 'localhost';
	$user = '';
	$db = '';
	$password = '';
	
	// Create connection
$con=mysqli_connect($host,$user,$password,$db);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  else echo "connected";
  ?>
  
  
  
  <?php
  
$result= mysqli_query($con, "SELECT * FROM jos_content");
while($row = mysqli_fetch_assoc($result)){
      echo $row['title'];
}
?>

 

I still recommend mysqli

 

 

+1

 

You are going to have to change from mysql_* functions at some point. The sooner you change the less you will have to rewrite when they stop working completely (which will be when you upgrade to PHPv5.5 or later)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.