PHP and MySQL for Dummies

Have some problems with the code that they gave me. Can anyone please let me know what is going wrong?

Here is the code:

[php]

<?php /*Program: mysql_send.php *Desc: PHP program that sends an SQL query to the * MySQL server and displays the results. */ echo " SQL Query Sender "; 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'])) { mysql_connect($host,$user,$password); mysql_select_db($_POST['database']); $result = mysql_query($_POST['query']); echo "Database Selected: {$_POST['database']}
Query: {$_POST['query']}

Results


"; if($result == false) { echo "

Error: ".mysql_error($cxn)."

"; } elseif(@mysql_num_rows($result) == 0) { echo "

Query completed. No results returned.

"; } else { /* Display results */ echo ""; for($i = 0;$i < mysql_num_fields($result);$i++) { echo ""; } echo ""; for ($i=0;$i < mysql_num_rows($result);$i++) { echo ""; $row = mysql_fetch_row($result); foreach($row as $value) { echo ""; } echo ""; } echo "
".mysql_field_name($result,$i). "
".$value."
"; } /* Display form with only buttons after results */ $query = str_replace("'","%&%",$_POST['query']); echo "

"; exit(); } /* Displays form for query input */ if (@$_POST['queryButton'] != "Edit Query") { $query = " "; } else { $query = str_replace("%&%","'",$_POST['query']); } ?>
Type in database name >
Type in SQL query <?php echo $query ?>

[/php]

Here is the error msg that I am getting:

Database Selected: Query: hi Results

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource (( took out something for Secirty)) on line 28

Error:


**Mod Edit - Added PHP tags for better readability.

where does the $cxn come form?
it’s neither defined nor needed.

use mysql_error() insted of mysql_error($cxn)

Now I am having the problem were the “Query was empty” I do not understand why it is not passing the Query back to the DB.

If someone can help please do.

Here is the error. Code is on first post. Min the $cxn.

Database Selected: test1_db
Query: Hello
Results


Error: Query was empty


have u tryed a real query?

i don’t know in witch cases mysql returns “Query was empty”, but “Hello” isn’t a vaild MySQL-query at all.

Yes I did use a real Query and I get Error: Query was empty it is from the code…

if($result == false) { echo "

Error: ".mysql_error()."

"; } elseif(@mysql_num_rows($result) == 0) { echo "

Query completed. No results returned.

"; } else {

</end code section>

From what I see the query is not being passed from the form to the db so that it can give a answer.

Please help make sure that the code is passing eveything correctly.

Thank you!
Flintar

Having some passing problems…hope someone can help!

The problem is when I enter in a Query in the textarea it does not pass it to the DB. It give a error of “Query was empty” even if I enter in a true Query for it. If anyone can help please do! I don’t understand the
$_POST[‘query’] and what it is userd for? or how it is passed…

Please help if you can!
NOOB Flintar

here is the code if you need it.

<?php /*Program: mysql_send.php *Desc: PHP program that sends an SQL query to the * MySQL server and displays the results. */ echo " SQL Query Sender "; 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’]))
{
mysql_connect($host,$user,$password);
mysql_select_db($_POST[‘database’]);
$result = mysql_query($_POST[‘query’]);
echo "Database Selected: {$_POST[‘database’]}

Query: {$_POST[‘query’]}

Results


"; if($result == false) { echo "

Error: ".mysql_error()."

"; } elseif(@mysql_num_rows($result) == 0) { echo "

Query completed. No results returned.

"; } else { /* Display results */ echo ""; for($i = 0;$i < mysql_num_fields($result);$i++) { echo ""; } echo ""; for ($i=0;$i < mysql_num_rows($result);$i++) { echo ""; $row = mysql_fetch_row($result); foreach($row as $value) { echo ""; } echo ""; } echo "
".mysql_field_name($result,$i). "
".$value."
"; } /* Display form with only buttons after results */ $query = str_replace("'","%&%",$_POST['query']); echo "

"; exit(); }

/* Displays form for query input */
if (@$_POST[‘queryButton’] != “Edit Query”)
{
$query = " “;
}
else
{
$query = str_replace(”%&%","’",$_POST[‘query’]);
}
?>

Type in database name >
Type in SQL query <?php echo $query ?>

i still cant find any errors.

can u put error_reporting(E_ALL); at the beginning of ur script and tell us if there is a Notice diplayed?

Or try echoing the value of the variable that is passed as a parameter to mysql_query()?

Sponsor our Newsletter | Privacy Policy | Terms of Service