mysql_send.php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hocky_nutty
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2008 11:20 am

mysql_send.php

Post by hocky_nutty »

A lot of people seem to have problems with this program provided in the book of PHP and MySQL for Dummies 3rd Edition. It is called mysql_send.php . There is only one site that I found it to be working fine. It can be found here: http://www.stevemarth.com/tests/mysql_send.php . But when I try sending a query, the page just seems to refresh. I think it has to do with there is no action, but I am just going by what the book said to do. Here is the 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='localhost';
$user='roo';
$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>

_______________________________________________________________________________________________________
Ok, here is what I get http://www.kyleboss.com/testing/mysql_send.php
Can someone point me to the right direction?
Thanks
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: mysql_send.php

Post by dyluck »

I noticed a more common query command would be removing the "i" from mysql commands (mysqli_connect() to mysql_connect() ect. I am not too keen on using the i... anyway, I think this depends on your version of php and stuff... give it a try and let me know if it works :)
hocky_nutty
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2008 11:20 am

Re: mysql_send.php

Post by hocky_nutty »

Here is what the book says:

Understanding PHP/MySQL functions
PHP can communicate with any version of MySQL. However, PHP needs to
be installed differently, depending on which version of MySQL you’re using.
PHP provides one set of functions (mysql functions) that communicate with
MySQL 4.0 or earlier and a different set of functions (mysqli functions) that
32 Part I: Developing a Web Database Application Using PHP and MySQL
communicate with MySQL 4.1 or later. The mysql functions, which communicate
with earlier versions of MySQL, can also communicate with the later versions
of MySQL, but you may not be able to use some of the newer, advanced
features that were added to MySQL in the later versions. The mysqli functions,
which can take advantage of all the MySQL features, are available only with
PHP 5 or later.
The programs in this book, including the test programs in this section, use
MySQL 5.0 and the mysqli functions. If you’re using PHP 4, you need to
change the programs to use the mysql functions, rather than the mysqli
functions. The functions are similar, but some have slight changes in syntax.
Chapter 8 provides a table (Table 8-1) showing the differences between the
functions used in this book. Versions of the programs that will run with PHP 4
are available for download at my Web site (janet.valade.com).


I have MySQL 5. But I DID try it, to no avail :(
Thanks for trying though.
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: mysql_send.php

Post by dyluck »

Ok here is what I do in this circumstance. It will help you self solve issues.
When i get a series of code that someone else wrote, it is more difficult to understand what they are trying to do step by step. I be someone that used the code and got it to work on another site, reverse engineered the code and figured out what each step is doing.
ok....

I start at the top and echo variables and put a exit;

also... change $_POST['query'] = stripslashes($_POST['query'];
to $postquery = $_POST['query'];

then right after

echo $postquery;
exit;

run your form... are you getting the query displayed on your screen?
Yes - then continue to drill down, test another variable.... then and test variables in logic statements
Keep putting exit; at the end
If you are checking multipule variables at the same time, then separate the echos with spaces.

echo $variable1;
echo ' ';
echo $variable2;
echo ' ';
etc....
and redo your post.
make sure all your variables are getting the information required so echo them and make sure they are correct.
You will stumble across issues as you drill down.. you will find maybe some statements aren't receiving the right information from functions above...

i have had to do this massive ammounts of time. I usually find that my syntax is off somewhere or a statement isn't quite right...

Good luck!
Post Reply