www.webdeveloper.com
Results 1 to 5 of 5

Thread: php echo not working

  1. #1
    Join Date
    Jan 2006
    Location
    Somewhere in the backwoods of Arkansas.
    Posts
    35

    php echo not working

    I'm trying to get two separate mysql cells to be echoed on a single page. The first cell is working with no problems, but I haven't managed a way to get the second one to work.

    Code:
    <?php
    
    
    	//Connect to server and database
    	include'config.php';
    	mysql_connect("$host", "$username", "$password") or die(mysql_error());
    	//echo "Connected to your mysql<br/>";
    	mysql_select_db("$db_name") or die(mysql_error());
    	//echo "Connected to database<br/>";
    
    	$result=mysql_query("SELECT * FROM books WHERE Genre LIKE '%Fiction%' ORDER BY title ASC");
    ?>
    
    <html>
    	<head>
    		<link rel="stylesheet" type="text/css" href="style.css" />
    		<script type="text/javascript" src="jquery.js"></script>
    
    	</head>
    
    	<body>
    			<div>
    				<?php include("menu.php"); ?>
    			</div>
    
    
    		<div id="Items">
    		<br /> <br />
    			<?php
    				while($row=mysql_fetch_array($result))
    				{
    					echo "".$row['Information']."";
    				}
    			?>
    		</div>
    
    		<div id="mystickytooltip" class="stickytooltip">
    			<div style="padding:5px">
    				<?php
    					while($row=mysql_fetch_array($result))
    					{
    						echo "".$row['Tooltip']."";
    					}
    				?>
    			</div>
    		</div>
    
    		</body>
    </html>

  2. #2
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Why do you need these two parts of your script:
    PHP Code:
    echo "Connected to your mysql<br/>"
    and
    PHP Code:
    echo "Connected to database<br/>"
    On both your mysql_connect and mysql_select_db functions you have "or die(mysql_error()). If something goes wrong. this little bit of code is going to let you know. If you don't see any messages then you can just assume your connection is successful.

    Also I don't recommend ever using "SELECT *" in your queries unless you really need to select every single column in a table because it's a waste of CPU cycles and probably RAM too. The more your site wastes computer "power," if you will, the less people your site will be able to handle on it at one time. The alternative to "SELECT *" is listing the columns you want e.g. on an employee table I might do "SELECT `employee_name`, `employee_id`, `department_id`"

    Anyway, I'm not entirely sure if my advice about this next part is the best advice, but what I would have personally done is an entirely separate query for "Tooltip". If someone has a better idea, they'll probably come along and correct me. I am an intermediate PHP programmer so and they're, like, pros, so yeah.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    One reason I can see that might be your problem is that you're iterating through the same resultset twice, the second time obviously starting at the end of the recordset and immediately returning false.

    You could either try and extract both fields of information at the same time and store it in an array for further processing, or rewind the recordset using mysql_data_seek:

    Code:
    			<div>
    				<?php include("menu.php"); ?>
    			</div>
    
    
    		<div id="Items">
    		<br /> <br />
    			<?php
    				while($row=mysql_fetch_array($result))
    				{
    					echo "".$row['Information']."";
    				}
    			?>
    		</div>
    
    		<div id="mystickytooltip" class="stickytooltip">
    			<div style="padding:5px">
    				<?php
    					mysql_data_seek($result, 0);
    					while($row=mysql_fetch_array($result))
    					{
    						echo "".$row['Tooltip']."";
    					}
    				?>
    			</div>
    		</div>
    JavaScript: Learn | Validate | Compact | bionoid

  4. #4
    Join Date
    Jan 2006
    Location
    Somewhere in the backwoods of Arkansas.
    Posts
    35
    evenstar
    I have those two lines as I'm still learning and they there just so it if fails I can see why.

    In regards to the SELECT * If I change it to only scan a certain column it throws an index error.


    bionoid
    That fixed it. Now time to go read up on mysql_data_seek.


    Thanks everyone.

  5. #5
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    If it fails the "or die(mysql_error())" part of your code will tell you why.

    An "index" error? That shouldn't be. Can you show us the error, and the query?
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles