Jump to content

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

You cannot use mysqlI_* (mysql improved) functions and mysql_* (original) functions together. You can only use one or the other, personally I'd use mysqli as the original library is deprecated and could be removed from future versions of PHP. 

Edited by Ch0cu3r

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)

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.