www.webdeveloper.com
Results 1 to 2 of 2

Thread: Query MySQL via php hyperlink

Hybrid View

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

    Query MySQL via php hyperlink

    What I'm trying to do is set up a hyperlink to run a MySQL query when clicked.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <?php
    	include'config.php';
    	mysql_connect("$host", "$username", "$password") or die(mysql_error());
    	mysql_select_db("$db_name") or die(mysql_error());
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    
    	<head> 
    		<link rel="stylesheet" href="Style.css" type="text/css" />
    	</head>
    
    	<body>
    		<div class="menu">
    			<ul>
    				<li><a href="index.php">Home</a></li>
    
    				<li> <a href="<?php $result=mysql_query("SELECT * FROM movies WHERE (Title='Page - Test1') "); ?>">Test1</a> </li>
    			</ul>	
    		</div>
    
    		<?php
    			while($row=mysql_fetch_array($result))
    			{
    				echo ''.$row['Page'].'';
    			}
    		?>
    
    	</body> 
    </html>

  2. #2
    Join Date
    Aug 2012
    Posts
    155
    You cannot run a query in a link. You either need to have the query on the page the the link directs to or you jquery and ajax to run the query and return results to the same page (i.e. without refreshing the page. So in the first instance your link would be normal (<a href="test1.php">Test1</a>) and then on test1.php you run your query (for example).
    PHP Code:
    <?php
    include'config.php';
        
    mysql_connect("$host""$username""$password") or die(mysql_error());
        
    mysql_select_db("$db_name") or die(mysql_error());
    $result=mysql_query("SELECT * FROM movies WHERE Title='Page - Test1' "); ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">

        <head> 
            <link rel="stylesheet" href="Style.css" type="text/css" />
        </head>

        <body>
            <div class="menu">
                <ul>
                    <li><a href="index.php">Home</a></li>

                    <li> <a href="test1.php">Test1</a> </li>
                </ul>    
            </div>

            <?php
                
    while($row=mysql_fetch_array($result))
                {
                    echo 
    ''.$row['Page'].'';
                }
            
    ?>

        </body> 
    </html>

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