I want php to print out the value of a query. This is my 1st time coding with SQL.

This query below I use grabs the value of the column title in the table userdata.

$queryreadtitle = mysql_query('SELECT title FROM userdata');

I need the value of that to be printed. Thanks!

    $result = mysql_query('SELECT title FROM userdata');
    while ($row = mysql_fetch_assoc($result))
    {
    	echo $row['title'] . '<br>';
    }
    

      Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/adamb10/public_html/UltraBlog/index.php</b> on line 14

        Hi,

        change the query php code to:

        $result = mysql_query('SELECT title FROM userdata') or die("Error: ".mysql_error()); 
        

        That will show you the error.

        Thomas

          Originally posted by tsinka
          Hi,

          change the query php code to:

          $result = mysql_query('SELECT title FROM userdata') or die("Error: ".mysql_error()); 
          

          That will show you the error.

          Thomas [/B]

          Thanks. Turns out I specifyed the wrong table.

          http://adamb10.com/UltraBlog/index.php

          Why is it not printing by welcome to?

            Please post the code.

            The link you provided shows the parsed code. I can't see the PHP source. One thing I noticed. It seems like HTML comments are shown because of a missing exclamation mark in <-- (it should be <!--)

            Thomas

              <?
              session_start();
              include_once("Sources/sys.php");
              ?>
              <html>
              
              <head>
              <meta http-equiv="Content-Language" content="en-us">
              
              <title>
              <? 
              include '/home/adamb10/public_html/UltraBlog/Sources/mysqlconnect.php';
              $result = mysql_query('SELECT title FROM Userdata') or die("Error: ".mysql_error());
              while ($row = mysql_fetch_assoc($result))
              {
                  echo $row['title'];
              }
              ?>
              </title>
              <link rel="stylesheet" type="text/css" href="http://adamb10.com/UltraBlog/themes/style1.css"/>
              </head>
              <?
              if($action == "login"){
              return('/Sources/sys.php');
              }
              if($action == "acp"){
              return('/Sources/sys.php');
              }
              if($action == "settings"){
              return('/Sources/sys.php');
              }
              if($action == "about"){
              return('/Sources/sys.php');
              }
              if($action == "addentry"){
              return('/Sources/sys.php');
              }
              ?>
              <body>
              <center><table width="80%" cellspacing="1" cellpadding="1" border="0" class="border">
              
              <td class="windowbg2" colspan="4"><center><font size="3"><b>Welcome to <? while ($row = mysql_fetch_assoc($result))
              {
                  echo $row['title'];
              };
              ?></b></font></center>
              <tr>
              <?
              include '/home/adamb10/public_html/UltraBlog/Sources/urls.php';
              ?>
              
              <------HEADER------>
              <br><br><br><br>
              <table width="80%" cellspacing="1" cellpadding="1" border="0" class="border">
              <td class="titlebg"><b><center>Entry Title:</center></b>
              <td class="titlebg" width="145"><b><center>Date:</center></b></td>
              <tr>
              
              <td class="titlebg" colspan="2"></td>
              </tr>
              </table>
              <br>
              <------FOOTER------>
              <br><br><br><br><br><br><br>
              <center><font size="1">Powered by UltraBlog Version 0.5</font></center>
              </body>
              
              </html>
              

                Hi,

                the if sequence (returning sys.php) doesn't make sense. What do you want to do with that code ?

                Try the following code:

                <?PHP
                session_start(); 
                require_once("Sources/sys.php"); 
                require_once("Sources/mysqlconnect.php");
                
                $title = "My Standard Title";
                
                // there is a WHERE clause missing to get
                // a specific title
                $res = mysql_query("SELECT title FROM Userdata") or die("Error: ".mysql_error());
                
                if ($row = mysql_fetch_array($res)) {
                    $title = $row["title"];
                }
                ?> 
                <html> 
                  <head> 
                    <meta http-equiv="Content-Language" content="en-us" /> 
                    <title>
                      <?PHP echo $title; ?>
                    </title> 
                    <link rel="stylesheet" type="text/css" href="themes/style1.css" /> 
                  </head> 
                  <body> 
                    <center>
                      <table width="80%" cellspacing="1" cellpadding="1" border="0" class="border"> 
                        <tr>
                          <td class="windowbg2" colspan="4" align="center">
                            <font size="3"><b>Welcome to <?PHP echo $title; ?></b></font>
                          </td>
                        </tr>
                      </table>
                <?PHP
                include "Sources/urls.php"; 
                ?> 
                    <br /><br /><br /><br /> 
                <!------HEADER------> 
                    <table width="80%" cellspacing="1" cellpadding="1" border="0" class="border"> 
                      <tr>
                        <td class="titlebg" align="center">
                          <b>Entry Title:</b> 
                        </td>
                        <td class="titlebg" width="145" align="center">
                          <b>Date:</b>
                        </td> 
                      </tr> 
                    </table>
                <!------FOOTER------> 
                    <br /><br /><br /><br /><br /><br /><br /> 
                    <center><font size="1">Powered by UltraBlog Version 0.5</font></center> 
                  </body> 
                </html>
                

                I'd suggest to change the css styles so that you don't need to use e.g. <b>..</b> inside the <td>...</td> tags and so on.

                Additionally, I'd suggest to change the web server configuration so that DirectoryListing is not allowed. You can get a listing of each directory on the server.

                Thomas

                  The ifs are for sys.php. They are used for the ?action=blah urls. If I didn't include those the index page would print on every page that uses the action urls.

                    Sorry,

                    I messed up the HTML code. Take your original code and do the following:

                    Predefine a variable (e.g. $title) on top of the script. Then execute the SQL query and overwrite the variable if a row has been returned. Use <?PHP echo $title; ?> inside the HTML code where you want to print the title.

                    Thomas

                      Write a Reply...