Jump to content

Connecting to MySQL database but not returning anything


Merriick

Recommended Posts

Im using a online beginners tutorial to retrieve multiple rows from the database, thats not happening

 

Instead its returning one row and it appears to be blank. No doubt theres some holes but stuffed if i know what

 

anyway

 

Code

$link = mysql_connect("$dbServer1", "$dbUser1", "$dbPass1") or die("Could not connect post");
    print "Connected successfully to $dbServer1 Server<br>";
    mysql_select_db("$dbName1") or die("Could not select database your_db_name1");
    print "Database $dbName1 selected successfully<br><br><br>";
   
   $query=("SELECT * FROM phpc_uid");
   $result=mysql_query($query);
   $num=mysql_numrows($result);
   
   print "Number of rows equals $num";
   
   $i=0;
   while ($i < $num) {
   
   $subject=mysql_result($result,$i,"subject");
   $Description=mysql_result($result,$i,"description");

   print "$Subject and $Description";

$i++;
}

 

Page is returning this

 

Connected successfully to Calendar

Database calendar selected successfully

 

 

Number of rows equals 1

Warning: mysql_result() [function.mysql-result]: subject not found in MySQL result index 4 in C:\wamp\www\Calendar\testmysql.php on line 43

 

Warning: mysql_result() [function.mysql-result]: description not found in MySQL result index 4 in C:\wamp\www\Calendar\testmysql.php on line 44

 

Its connecting after that, im guessing doing nothing

 

Link to comment
Share on other sites

Well, off hand the only thing that comes to mind is that the result may have different field names then the ones you are trying to access. For example, the case of the letters is important ("subject" != "SUBJECT").

 

But, I would also suggest some changes to that logic.

 

1.

$query=("SELECT * FROM phpc_uid");

Typically, you should refrain from using "*" as your select criteria unless you need all the data because it uses up additional resources and can be a performance issue on high-traffic sites. Plus, depending on how the data is used it could cause a security problem if fields are added in the future. Besides in this example you are only using two fields, so you should only query for those two.

 

2.

while ($i < $num) {

It really makes no sense to define the number of rows returned and then use a counter to get those rows. Just use a while loop that extracts each row.

 

3. The select query has no error handling, so that could be failing.

 

Example:

<?php
    
$link = mysql_connect("$dbServer1", "$dbUser1", "$dbPass1") or die("Could not connect post");
echo "Connected successfully to $dbServer1 Server<br>\n";
    
mysql_select_db("$dbName1") or die("Could not select database your_db_name1");
echo "Database $dbName1 selected successfully<br><br><br>\n";
    
$query = "SELECT subject, description FROM phpc_uid";
//$query = "SELECT * FROM phpc_uid"; //DEBUG LINE
$result=mysql_query($query);
//$firstrow = mysql_fetch_assoc($result); //DEBUG LINE
//print_r($firstrow); //DEBUG LINE
//exit();  //DEBUG LINE
    
if(!$result)
{
    //Error running query
    echo "The query failed.<br /><br />\n";
    echo "Query: {$query}<br /><br />\n";
    echo "Error: " . mysql_error();
}
elseif(mysql_numrows($result)<1)
{
    //No results from query
    echo "No results returned\n";
}
else
{
    //Results were returned, show results
    $num_rows = mysql_numrows($result);
    echo "Number of rows returned: {$num_rows}<br /><br />\n";
    while ($row = mysql_fetch_assoc($result))
    {
        echo "{$row['subject']} and {$row['description']}<br />\n";
    }
}

 

If you don't get the results you expect, uncomment the debug lines to validate what is returned/available in that table.

Link to comment
Share on other sites

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.