MySQL Forums
Forum List  »  PHP

Re: Mysqli vs mysql
Posted by: Hesham Naguib
Date: October 04, 2017 03:07PM

i finally got it to work but when i type SHOW DATABASES in the Type in SQL query as shown in the tutorial i get an error saying mysql_num_fields() expects parameter 1 to be resource, object given in C:\wamp64\www\mysql_send.php on line 39

this is the code and i wrote exactly as shown in the book can someone tell me where is problem?



<?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="root";
$password="safefromharm1!";

/* 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>";
for($i = 0;$i < mysql_num_fields($result);$i++)

{
echo "<th>".mysql_field_name ($result,$i) .
"</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
{

Options: ReplyQuote


Subject
Written By
Posted
October 04, 2017 02:16PM
Re: Mysqli vs mysql
October 04, 2017 03:07PM
October 04, 2017 07:04PM
November 01, 2017 02:54PM
October 31, 2017 03:01AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.