LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2010, 01:15 PM   #1
iWant2Know
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Rep: Reputation: 0
MySQL (phpMyAdmin) & PHP


I have written a program in PHP that will execute SQL queries and display the results in my browser. The program is not performing the functions when information is input and query button is submitted. The page returns with no information in the text box. I am usig a remote server, MySQL for my server is managed by phpMyAdmin but this is not specified in the program. Could this be part of the issue?

I am a newbie and need some clarity on how to use php programming to request and display results from MySQL which is managed by phpMyAdmin from a remote server.

Thanks.
 
Old 08-22-2010, 01:59 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. Yes, you should be able to do exactly what you're describing.

2. No, phpAdmin has nothing to do with it.

SUGGESTION:
Please post your PHP code, or a short, equivalent test case.
It should be fairly short (under 50 lines).
Please use code tags.

Once we see the code, we should be able to figure out what's going wrong.

Thank you in advance .. PSM

PS:
Typically, your web server and your MySQL server are both co-located on the same host.
Is this your scenario, or are HTTP and MySQL on two different servers?

Last edited by paulsm4; 08-22-2010 at 02:01 PM.
 
Old 08-22-2010, 01:59 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
You should approach this problem logically:
  1. Have you tried to connect using the mysql client?
    Code:
    mysql -h your.mysql.host -u user -ppass
    . If that fails, connecting using PHP is not much use. You should check why mysql is not allowing remote connections. You might want to login on the mysql box and try the same, omitting the -h your.mysql.host
  2. Have you tried this example? http://www.php.net/manual/en/functio...etch-array.php

jlinkels
 
Old 08-22-2010, 10:06 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Here's another good "hello world" type example:
http://www.daniweb.com/forums/thread87999.html

Code:
<html>
  <form method="post" action="test.php">
    User Name : <input name="UserName" type="text" id="UserName" 
      value="User Name" size="20" maxlength="30" />
    <input type="submit" name="submit" value="Submit">
  </form>
</html><?php
  if (isset($_POST['submit'])) {
    mysql_connect("Localhost","username","password")
      or die("Failure to communicate");
    mysql_select_db("mydb")
      or die("Could not connect to Database");
    if ($_REQUEST['UserName'] == NULL) {
      echo "Please Enter a User Name";
    }
    else {
      $p="INSERT INTO mytable(UserName)VALUES('$UserName')";
      $q=mysql_query($p);
    }
  }
  else {
    echo "Enter a User Name";
  }
 ?>
 
Old 08-23-2010, 12:41 AM   #5
iWant2Know
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Code

<?php
/*Program: mysql_send.php
*Desc: PHP program that sends an SQL query to the
* MySQL server and displays the results.
*/

echo "<html>
<head><title>SQL Query Sender</title></head>
<body>";
if(ini_get("magic_quotes_gpc") == “1”)
{
$_POST[‘query’] = stripslashes($_POST[‘query’]);
}
$host=" ";
$user=" ";
$password=" ";

/* Section that executes query and displays the results */
if(!empty($_POST[‘form’]))
{
$cxn = mysqli_connect($host,$user,$password,
$_POST['database']);
$result = mysqli_query($cxn,$_POST[‘query’]);
echo "Database Selected: <b>{$_POST[‘database’]}</b><br>
Query: <b>{$_POST[‘query’]}</b>
<h3>Results</h3><hr>";
if($result == false)
{
echo "<h4>Error: ".mysqli_error($cxn)."</h4>";
}
elseif(@mysqli_num_rows($result) == 0)
{
echo "<h4>Query completed.
No results returned.</h4>";
}
else
{
/* Display results */
echo "<table border=’1’><thead><tr>";
$finfo = mysqli_fetch_fields($result);
foreach($finfo as $field)
{
echo "<th>".$field->name."</th>";
}
echo "</tr></thead>
<tbody>";
for ($i=0;$i < mysqli_num_rows($result);$i++)
{
echo "<tr>";
$row = mysqli_fetch_row($result);
foreach($row as $value)
{
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo"</tbody></table>";
}
/* Display form with only buttons after results */
$query = str_replace("‘","%&%",$_POST[‘query’]);
echo "<hr><br>
<form action=’{$_SERVER[‘PHP_SELF’]}’ method=’POST’>
<input type=’hidden’ name=’query’ value=’$query’>
<input type=’hidden’ name=’database’
value={$_POST[‘database’]}>
<input type=’submit’ name=’queryButton’
value=’New Query’>
<input type=’submit’ name=’queryButton’
value=’Edit Query’>
</form>";
exit();
}
/* Displays form for query input */
if (@$_POST[‘queryButton’] != "Edit Query")
{
$query = " ";
}
else
{
$query = str_replace("%&%","’",$_POST[‘query’]);
}
?>
<form action="<?php echo $_SERVER[‘PHP_SELF’] ?>"
method="POST">
<table>
<tr><td style=’text-align: right; font-weight: bold’>
Type in database name</td>
<td><input type="text" name="database"
value=<?php echo @$_POST[‘database’] ?> ></td>
</tr>
<tr><td style=’text-align: right; font-weight: bold’
valign="top">Type in SQL query</td>
<td><textarea name="query" cols="60"
rows="10"><?php echo $query ?></textarea></td>
</tr>
<tr><td colspan="2" style=’text-align: center’>
<input type="submit" value="Submit Query"></td>
</tr>
</table>
<input type="hidden" name="form" value="yes">
</form>
</body></html>
 
Old 08-24-2010, 03:44 PM   #6
Xyro
LQ Newbie
 
Registered: Aug 2009
Location: Canada
Distribution: Ubuntu 9.04
Posts: 22

Rep: Reputation: 19
As paulsm4 mentioned, phpMyAdmin would not be the cause of your problems. It is simply a web-based front-end to manage your DB.

Try this code and report results:

Code:
<?php
  $user = '<INSERT MYSQL USERNAME HERE>';
  $pass = '<INSERT MYSQL PASSWORD HERE>';
  $host = '<INSERT MYSQL HOSTNAME HERE>';
  $dbnm = '<INSERT MYSQL DATABASE NAME>';

  $link = mysql_connect( $host, $user, $pass ) or die( mysql_error() );
  mysql_select_db( $dbnm ) or die( mysql_error() );
  mysql_close( $link );
  echo "All seems OK\n";
?>
Also, where did you get the 'mysqli_*' commands? Did you mean 'mysql_*' (no 'i')?

PS: These are very good tutorials.

Last edited by Xyro; 08-24-2010 at 03:58 PM. Reason: Submitted too quickly... by accident.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
deb 4.0: how to install apache2, mysql 4.1, php 4 and phpmyadmin tonguim Debian 10 11-09-2007 10:39 AM
Problems with apache/php/mysql/phpmyadmin Using DEBIAN antinull Linux - Server 4 11-06-2007 04:02 PM
install Apache, MySQL, PHP, and phpMyAdmin for offline use? Kandiman Ubuntu 4 03-01-2007 01:35 PM
mysql/php/phpmyadmin/utf8 kenji1903 Linux - Software 6 09-03-2005 06:36 AM
How do I know that apache, MySql, PHP, phpMyAdmin is Installed AskMe Linux - Newbie 7 02-04-2004 05:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration