/    Sign up×
Community /Pin to ProfileBookmark

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>
[/CODE]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@evenstar7139Sep 03.2012 — Why do you need these two parts of your script:
[code=php]echo "Connected to your mysql<br/>";[/code]
and
[code=php]echo "Connected to database<br/>";[/code]

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.
Copy linkTweet thisAlerts:
@bionoidSep 03.2012 — 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
[COLOR="red"]while($row=mysql_fetch_array($result))[/COLOR]
{
echo "".$row['Information']."";
}
?>
</div>

<div id="mystickytooltip" class="stickytooltip">
<div style="padding:5px">
<?php
[COLOR="red"]mysql_data_seek($result, 0);[/COLOR]
[COLOR="red"]while($row=mysql_fetch_array($result))[/COLOR]
{
echo "".$row['Tooltip']."";
}
?>
</div>
</div>[/CODE]
Copy linkTweet thisAlerts:
@RagingFredauthorSep 03.2012 — 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.
Copy linkTweet thisAlerts:
@evenstar7139Sep 03.2012 — 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?
×

Success!

Help @RagingFred spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...