barryflood22 Posted January 14, 2014 Share Posted January 14, 2014 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/ Share on other sites More sharing options...
Ch0cu3r Posted January 14, 2014 Share Posted January 14, 2014 (edited) 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 January 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465261 Share on other sites More sharing options...
barryflood22 Posted January 14, 2014 Author Share Posted January 14, 2014 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465262 Share on other sites More sharing options...
barryflood22 Posted January 14, 2014 Author Share Posted January 14, 2014 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465265 Share on other sites More sharing options...
Ch0cu3r Posted January 14, 2014 Share Posted January 14, 2014 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465266 Share on other sites More sharing options...
barryflood22 Posted January 14, 2014 Author Share Posted January 14, 2014 thanks, fully working now :-) Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465267 Share on other sites More sharing options...
Barand Posted January 14, 2014 Share Posted January 14, 2014 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) Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465268 Share on other sites More sharing options...
ginerjm Posted January 14, 2014 Share Posted January 14, 2014 I'm still wondering why it works when you don't select a database name. Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465269 Share on other sites More sharing options...
JIXO Posted January 14, 2014 Share Posted January 14, 2014 (edited) 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 January 14, 2014 by JIXO Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465270 Share on other sites More sharing options...
ginerjm Posted January 14, 2014 Share Posted January 14, 2014 I saw the part about the passwords, but it didn't mention dbname. Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465272 Share on other sites More sharing options...
Barand Posted January 14, 2014 Share Posted January 14, 2014 I saw the part about the passwords, but it didn't mention dbname. mysql_select_db($db) on line 9 of reply #4 Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465273 Share on other sites More sharing options...
JIXO Posted January 14, 2014 Share Posted January 14, 2014 It didn't mention removing database username too , but obviously there is no way the code is fully working without specifing a database name Quote Link to comment https://forums.phpfreaks.com/topic/285371-issues-with-basic-php-echo/#findComment-1465274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.