Hi There,
I am trying to get the information that i have put in the database to be displayed on the screen but i am getting this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\capat\result.php on line 44

The error doesn't seem to go away AT ALLL

this is my code
questions.php

<?php
/**
 * 
 * Install setup page to allow the inital user to be set up
 * 
 */
 
 require_once("include/inc_global.php");




 //build the page
 $UI->page_title = 'Web-PA Admin Setup';
 $UI->menu_selected = '';
 $UI->breadcrumbs = array	('Admin setup'	=> null ,);


 $UI->head();

 $UI->body();
 $UI->content_start();
 
 // see if any of the settings have been sent
 $question1 = (string) fetch_POST('question1', null);
 $question2 = (string) fetch_POST('question2', null);
 $question3 = (string) fetch_POST('question3', null);
 $question4 = (string) fetch_POST('question4', null);
 $question5 = (string) fetch_POST('question5', null);
 $username = (string) fetch_POST('username', null);
 $password = (string) fetch_POST('password', null);
 
 //if we have at least the username and the password then we can say that we are processing
 if ($username and $password){
 	
 	// Sanitize the username/password data
	$username = substr($username,0,32);
	$password = substr($password,0,32);
	
	//hash the password
	//$password = md5($password);
	
	//add this information to the database
	$sql = 'INSERT INTO questions(question1, question2, question3, question4, question5, username, password)
		    VALUES ("'.$question1.'", "'.$question2.'", "'.$question3.'", "'.$question4.'", "'.$question5.'", "'.$username.'", "'.$password.'");';
	$DB->_process_query($sql);
?>	
	<div class="content_box">
		<p> Your details have been set up on the server and a new Administrator account has been created.</p>
		<p> To ensure that no other administrator accounts can be created we recomend that you remove this file from the server</p>
	</div>
<?php	
 }else{ 

?>

<div class="content_box">
	<p>You can enter your details as required below.</p>
	<p>If you are intending to use the Database Authentication then you will need to enter a passsword. If you are using the LDAP 
	Authentication then you still need to enter all the information, but the password is not required. </p>
	<form action="result.php" method="post" name="login_form" style="margin-bottom: 2em;">
		<div style="width: 300px;">
			<table class="form" cellpadding="2" cellspacing="1" width="100%">
				<tr>
					<th><label for="question1">Question 1</label></th>
					<td><input type="text" name="question1" id="question1" maxlength="30" size="30" value=""/></td>
				</tr>
				<tr>
					<th><label for="question2">Question 2</label></th>
					<td><input type="text" name="question2" id="question2" maxlength="30" size="30" value="" /></td>
				</tr>
				<tr>
					<th><label for="question3">Question 3</label></th>
					<td><input type="text" name="question3" id="question3" maxlength="30" size="30" value="" /></td>
				</tr>
				<tr>
					<th><label for="question4">Question 4</label></th>
					<td><input type="text" name="question4" id="question4" maxlength="30" size="30" value="" /></td>
				</tr>
				<tr>
					<th><label for="question5">Question 5</label></th>
					<td><input type="text" name="question5" id="question5" maxlength="30" size="30" value="" /></td>
				</tr>
				<tr>
					<th><label for="username"></label></th>
					<td><input type="hidden" value="none"name="username" id="username" maxlength="16" size="10" value="" /></td>
				</tr>
				<tr>
					<th><label for="password"></label></th>
					<td><input type="hidden" value="none" name="password" id="password" maxlength="16" size="10" value="" /></td>
				</tr>
			</table>

			<div class="form_button_bar">
				<input class="safe_button" type="submit" name="submit" value="Set up" />
			</div>
	</div>
	</form>
</div>
<?php
 }
 
 $UI->content_end(false);
?>

and result.php

<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="pa"; // Database name
$tbl_name="questions"; // Table name
 // Get info from the session

// Connect to server and select databse.
$db_handle= mysql_connect("$host", "$username", "$password")or die("cannot connect");
//$db_found = mysql_select_db("$db_name")or die("cannot select DB");


$db_found = mysql_select_db($db_name, $db_handle);

// Define $myquestion1 and $myquestion2 and $myquestion3 and $myquestion4 and $myquestion5
$myquestion1=$_POST['myquestion1'];
$myquestion2=$_POST['myquestion2'];
$myquestion3=$_POST['myquestion3'];
$myquestion4=$_POST['myquestion4'];
$myquestion5=$_POST['myquestion5'];




//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and //password='$mypassword'";
//$result=mysql_query($sql);


//$sql=("SELECT * FROM $tbl_name WHERE question1='$myquestion1' and question2='$myquestion2' and question3 ='$myquestion3' and question4='$myquestion4' and question5='$myquestion5'");

//$result=mysql_query($sql);


// Mysql_num_row is counting table row
//$count=mysql_num_rows($result);
// If result matched $myquestion1 and $myquestion2 and $myquestion3 and $myquestion4 and $myquestion5, table row must be 1 row

//if ($db_found) {

$sql = "SELECT * FROM tb_questions";
$result = mysql_query($sql);
$rowdetail = mysql_fetch_array($result);
//while ($db_field = mysql_fetch_array($result)) {
//echo $db_field['question1'] . "<BR>";
//echo $db_field['question2'] . "<BR>";
//print $db_field['question3'] . "<BR>";
//print $db_field['question4'] . "<BR>";
//print $db_field['question5'] . "<BR>";
print "<p>Name: ".$rowdetail['firstname']." ".$rowdetail['surname']."<br />\n";  
print "Address: ".$rowdetail['address']."<br />\n";  
print "<input name= password id=password maxlength=316 size=110  />cxvbcv</td>"
				
//print "Postcode: ".$rowdetail['postcode']."\n";  



//ob_end_flush();
?>

Please help me out!!!!

Dani AI

Generated

This warning means that mysql_fetch_array was handed a boolean false rather than a valid result set — in other words the preceding mysql_query failed. The thread already contains useful clues: flagged the SQL string formatting and noted problems with how POST data is read. The real problems observable from the posted files are (a) the SELECT targets a different table name than the one used for INSERT, (b) the script reads POST keys that don’t match the form, and (c) there is no error checking to reveal the actual MySQL error.

Checklist to fix and diagnose (perform these in order):

  • Turn on PHP errors during development (error_reporting(E_ALL); ini_set('display_errors', 1);).
  • Confirm the DB connection and selection succeed and log any error from the DB client.
  • Verify the table name used in the SELECT matches the table that contains the data (the mismatch will make mysql_query return false).
  • Verify form input names match the keys read from $_POST; dump $_POST to confirm what was submitted.
  • Always check mysql_query/mysqli->query return values and print mysql_error()/$mysqli->error alongside the SQL being run so failures are obvious.
  • Remove or tidy stray characters in SQL strings; the extra semicolon inside a quoted SQL string is usually harmless but should be cleaned up.

A minimal, safer example using mysqli (procedural or OO style is fine) shows the error checks that are currently missing:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$mysqli = new mysqli('localhost', 'root', '', 'pa');
if ($mysqli->connect_error) {
    die('Connect error: ' . $mysqli->connect_error);
}

$sql = "SELECT question1, question2 FROM questions"; // ensure this table name is correct
if (!($res = $mysqli->query($sql))) {
    die('Query error: ' . $mysqli->error . ' — SQL: ' . $sql);
}

while ($row = $res->fetch_assoc()) {
    echo htmlspecialchars($row['question1']) . "<br>\n";
    echo htmlspecialchars($row['question2']) . "<br>\n";
}

$res->free();
$mysqli->close();
?>

Final notes: switch from the deprecated mysql_* API to mysqli or PDO and use prepared statements for inserts/reads. Also sanitize output with htmlspecialchars when echoing database values. These steps will both reveal the exact failure that produced the warning and make the code safer and more maintainable.

Recommended Answers

All 3 Replies

on lines 44 and 45 of questions.php

$sql = 'INSERT INTO questions(question1, question2, question3, question4, question5, username, password) VALUES ("'.$question1.'", "'.$question2.'", "'.$question3.'", "'.$question4.'", "'.$question5.'", "'.$username.'", "'.$password.'");';

Is this insert working? The reason I ask is because you have );'; at the end. Try getting rid of that extra semicolon and end it with this )';

$sql = 'INSERT INTO questions(question1, question2, question3, question4, question5, username, password) VALUES ("'.$question1.'", "'.$question2.'", "'.$question3.'", "'.$question4.'", "'.$question5.'", "'.$username.'", "'.$password.'")';

Hi,
Try removing the

ob_start();

at the top of your code.
Also, shouldn't your variables be this:

$myquestion1=$_POST['question1'];
$myquestion2=$_POST['question2'];
$myquestion3=$_POST['question3'];
$myquestion4=$_POST['question4'];
$myquestion5=$_POST['question5'];

intead of

$myquestion1=$_POST['myquestion1'];$myquestion2=$_POST['myquestion2'];$myquestion3=$_POST['myquestion3'];$myquestion4=$_POST['myquestion4'];$myquestion5=$_POST['myquestion5'];

I tried all that but still it didn't work. I'll try another method and see how far i get on with that..Thanks a lot for your help y'all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.