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
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'];
 }
?>
Link to comment
Share on other sites

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'];

}
  ?>
  
  
  
Link to comment
Share on other sites

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'];
}
?>
Link to comment
Share on other sites

Hi,

 

I'm still wondering why it works when you don't select a database name.

 

The username, password and database name were removed when pasted here for sure :

 

 

I have removed the passwords for obvious reasons.

Edited by JIXO
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.