php mysql help

jrobert755

New Member
Messages
15
Reaction score
0
Points
0
this is not working. it should get a list of values from a table, but it never does, and just stops running, displaying whats before it, and thats all (its in an include command on another page) could someone try to fix it?

PHP:
mysql_connect("localhost", "jrobert7", "password");
    mysql_select_db("jrobert7_testing");
    $result= mysql_query("SELECT * FROM articles");
    while($results = mysql_fetch_array($result))
    {
        echo "<div class=\"summary\">
                <div class=\"body\">
                    <h2><u><a href=\"".$results['link']. "\">".$results['name']. "</a></u></h2>
                    <p style=\"margin-top:-2px;\">
                    <em>
                    ".$results['link']. "<small>(2 hits)</small>
                    </em>
                    "$results['description'] "<a  class=\"more\" href=\"".$results['link']. "\" style=\"color:#105CB6;\">More...</a>
                    </p>
                    <div class=\"options\">
                        <a class=\"space compick\" href=\"".$results['link']. "\">0 comments</a>
                        <a class=\"space share\" href=\"".$results['link']. "\">Share</a>
                        <a class=\"space views\" href=\"".$results['link']. "\">".$results['views']. "\"views</a>
                    </div>
                </div>
                <ul class=\"fav\">
                    <li class=\"favcount\">
                        <a class=\"favcount\" href=\"".$results['link']. "\"><strong>".$results['fav']. "</strong><br />
                        <span>favs</span></a>
                    </li>
                    <li class=\"favit\">
                        <a class=\"favit\" href=\"".$results['link']. "\">fav it</a>
                    </li>
                </ul>
            </div>";
    }
 
Last edited:

blobtech

New Member
Prime Account
Messages
17
Reaction score
0
Points
1
First of all: its wise to not show your database password to the public.
You should edit that out =D.

But.. about your script. It seems fine, no PHP errors in it.
Are you sure it can succesfully connect to the database (as there is no code to catch the error when it can't)?

Try altering the lines
PHP:
mysql_connect("localhost", "jrobert7", "password");
mysql_select_db("jrobert7_testing");

To
PHP:
mysql_connect("localhost", "jrobert7", "password") or die("Could not connect: " . mysql_error());
mysql_select_db("jrobert7_testing") or die("Could select DB: " . mysql_error());

And tell us what output it gave.

Another error could be that the database is empty, and therefor the while loop never runs because mysql_fetch_array will return false as it can not find a row.

You can check the number of rows returned by your query with the following function
and output a message that there are no results when it equals 0:
PHP:
$num = mysql_num_rows($result);
 
Last edited:

VPmase

New Member
Messages
914
Reaction score
1
Points
0
1) Never reveal your password and name to anyone unless you know them completely...

2) I myself have had errors with using the \" inside "s so try this:


Code:
echo ‘<div class=“summary”>
                <div class=“body”>
                    <h2><u><a href=“’.$results['link']. ‘“>‘.$results['name']. ‘</a></u></h2>
                    <p style=“margin-top:-2px;”>
                    <em>
                    ‘.$results['link']. ‘<small>(2 hits)</small>
                    </em>
                    ‘$results['description'] ‘<a  class=“more” href=“’.$results['link']. ‘“ style=“color:#105CB6;”>More...</a>
                    </p>
                    <div class=“options”>
                        <a class=“space compick” href=“’.$results['link']. ‘“>0 comments</a>
                        <a class=“space share” href=“’.$results['link']. ‘“>Share</a>
                        <a class=“space views” href=“’.$results['link']. ‘“>‘.$results['views']. ‘“views</a>
                    </div>
                </div>
                <ul class=“fav”>
                    <li class=“favcount”>
                        <a class=“favcount” href=“’.$results['link']. ‘“><strong>‘.$results['fav']. ‘</strong><br />
                        <span>favs</span></a>
                    </li>
                    <li class=“favit”>
                        <a class=“favit” href=“’.$results['link']. ‘“>fav it</a>
                    </li>
                </ul>
            </div>‘;
 
Last edited:

jrobert755

New Member
Messages
15
Reaction score
0
Points
0
changed my password (copy- paste error :mad:). didn't work, no error and no results

tested it on a different host that i had before x10 and it works almost perfectly (need to work on css to get layout right though), and it got the data i wanted. its probably a restriction on the php on the server
 
Last edited:

blobtech

New Member
Prime Account
Messages
17
Reaction score
0
Points
1
Just saw a mistake, but I do not understand why PHP does not error.

There are no dots around
PHP:
$results['description'] // 6th line from the start of `echo`
and therefor the strings are not joined.

I tested a small script with something similar on my server, and it just returns a blank page, so I guess that this is it.
 
Last edited:

jrobert755

New Member
Messages
15
Reaction score
0
Points
0
yeah, like i said, uploaded to another hosting site i had before, fixed that problem, and it worked like a charm.
 

mephis

New Member
Messages
39
Reaction score
0
Points
0
try changin this line:
PHP:
while($results = mysql_fetch_array($result))
to this:
PHP:
while($results = mysql_fetch_assoc($result))
 

blobtech

New Member
Prime Account
Messages
17
Reaction score
0
Points
1
Actually, mysql_fetch_assoc is almost the same thing.

PHP:
mysql_fetch_assoc($result);
Is the equivelant of calling
PHP:
mysql_fetch_array($result, MYSQL_ASSOC);

However, mysql_fetch_array has as default optional parameter MYSQL_BOTH. That means it will return an associative as well as a numeric array.

Which is the same as
PHP:
mysql_fetch_row($result) + mysql_fetch_assoc($result);

There is no need for changing that line.
 

brunoais

New Member
Messages
115
Reaction score
0
Points
0
this is not working. it should get a list of values from a table, but it never does, and just stops running, displaying whats before it, and thats all (its in an include command on another page) could someone try to fix it?

PHP:
mysql_connect("localhost", "jrobert7", "password");
    mysql_select_db("jrobert7_testing");
    $result= mysql_query("SELECT * FROM articles");
    while($results = mysql_fetch_array($result))
    {
?> div class=\"summary\">
                <div class=\"body\">
                    <h2><u><a href="<?=$results['link'] ?>"><?=$results['name']?> "</a></u></h2>
                    <p style=\"margin-top:-2px;\">
                    <em>
                    <?=$results['link']?><small>(2 hits)</small>
                    </em>
                    <?=$results['description']?><a  class="more" href="<?=$results['link']?>"
etc....
It makes more simple coding and it's less probeble for you to make any mistakes.
just make a:
<?=
befora the variable and
?>
after the variable.
;)
 
Top